Tôi từ bỏ ... thậm chí không giáo sư của tôi biết tại sao điều này lại xảy ra.findViewById() trả về null - đã thử mọi thứ
Vấn đề là: Không có vấn đề ở đâu hoặc làm cách nào tôi gọi cho các thành phần của bố cục này, chúng luôn trả về giá trị rỗng.
LAYOUT: search_layout. 2 EdiText và 2 TextView trẻ em.
RUNTIME: setView(inflater.inflate(R.layout.search_layout, null)) // works fine, it displays the layout inside the dialog, yet, children are always returned as null by findViewById(R.id.some_search_layout_children)
EXCEPTION: Phân tích ngoại lệ của XML, như không tìm thấy tài nguyên, do đó null.
TRIED: Làm sạch dự án của tôi khoảng 1024 lần, cố gắng triển khai lớp khác cho hộp thoại của tôi, kết quả tương tự, được gọi là findViewById là thành viên hoạt động chính của tôi, bên trong phương thức initSearch() và bên trong triển khai ẩn danh OnClickListener cho hộp thoại của tôi, KẾT QUẢ KẾT QUẢ. Đập trẻ vào các quan điểm độc lập và lập trình gọi chúng:
TextView text = (TextView) findResourceById(R.id.new_independant_textview); // same result
Tôi nghĩ rằng tôi đã thử mọi thứ.
Đây là Mã của tôi:
public class Xyz extends Activity {
public void onCreate(...) { // some listener will trigger initSearch() }
private void initSearch() {
AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
searchDialog.setTitle("Search Photos");
searchDialog.setMessage("Specify tag and value...");
// R.layout.search_dialog is my custom layour, it displays fine, it works.
searchDialog.setView(inflater.inflate(R.layout.search_dialog, null));
EditText tagText = (EdiText) findViewById(R.id.tagField); // WILL RETURN NULL
searchDialog.setPositiveButton(...) ...
searchDialog.show();
}
Bây giờ, khi tôi làm:
EditText text = (EditText) findViewById(R.id.tagField); // returns null, ALWAYS
// no matter HOW, WHERE, global, local final, etc, it just returns null.
Đây là XML của bố trí tùy chỉnh của tôi:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tagText"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/tag" />
<EditText
android:id="@+id/tagField"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<TextView
android:id="@+id/valueText"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/value" />
<EditText
android:id="@+id/valueField"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</LinearLayout>
Đây là R. tôi tệp java:
public static final class id {
public static final int action_settings=0x7f0a0011;
public static final int add_album=0x7f0a0001;
public static final int add_photo=0x7f0a000d;
public static final int albums_list=0x7f0a0003;
public static final int delete_album=0x7f0a000b;
public static final int exit_finder=0x7f0a000f;
public static final int new_directory=0x7f0a000e;
public static final int open_album=0x7f0a000a;
public static final int photos_grid=0x7f0a0000;
public static final int rename_album=0x7f0a000c;
public static final int search_dialog=0x7f0a0004;
public static final int search_icon=0x7f0a0002;
public static final int splash_rutgers=0x7f0a0009;
public static final int tagField=0x7f0a0006; // problematic
public static final int tagText=0x7f0a0005;/problematic
public static final int terminate_app=0x7f0a0010;
public static final int valueField=0x7f0a0008; // problematic
public static final int valueText=0x7f0a0007; // problematic
}
Cảm ơn sự giúp đỡ của bạn, vui vẻ săn bắn.
bạn wana tìm xem trong hộp thoại gọi findViewById từ hoạt động khác ... chúc may mắn ... – Selvin
Âm thanh thú vị, nhưng tôi không hiểu tại sao fuly Tôi cần một hoạt động mới. Sau đó, tại sao R.layot.search_dialog trả về đúng lớp nhưng con của nó là null? Không có sự nhất quán ở đó. – Fer