2013-04-24 31 views
10

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.

+0

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

+0

Â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

Trả lời

38

Calling findViewById() sẽ tìm kiếm lượt xem trong vòng bố trí Hoạt động của bạn và không quan điểm của hộp thoại của bạn. Bạn cần gọi số findViewById() trên số View cụ thể mà bạn đặt làm bố cục của hộp thoại.

Hãy thử

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. 
    View dialogView = inflater.inflate(R.layout.search_dialog, null); 
    searchDialog.setView(dialogView); 
    EditText tagText = (EdiText) dialogView.findViewById(R.id.tagField); 
    searchDialog.setPositiveButton(...) ... 
    AlertDialog myAlert = searchDialog.create(); //returns an AlertDialog from a Builder. 
    myAlert.show(); 
} 

Thông báo này như thế nào tôi lạm phát quan điểm và lưu trữ nó trong một View tên dialogView. Sau đó, để tìm số EditText có tên tagField, tôi đang sử dụng dialogView.findViewById(R.id.tagField);

+0

Câu trả lời đúng. Cảm ơn các bạn. – Fer

1

Các TextView với id text123 phải được khai báo bên trong Layout bạn thiết lập với setContentView

+0

Thực ra, tôi nghĩ 'text123' có nghĩa là một ví dụ bởi vì OP có' EditText tagText = (EdiText) findViewById (R.id.tagField); // SILL RETURN NULL' trong phần "chính" của chúng. Nhưng câu trả lời này đã làm tôi cười khúc khích. +1. – TronicZomB

+0

@TronicZomB Tôi đọc văn bản EditText này = (EditText) findViewById (R.id.text123); // trả về null, LUÔN LUÔN – Blackbelt

+0

Tôi biết, tôi ít nhất hy vọng rằng OP đã sử dụng nó như một ví dụ, là tôi đoán những gì tôi đã cố gắng để nói. Vì trong các phần khác của mã được đăng, có vẻ như chúng đang tham chiếu các mục đúng cách. Nhưng nếu họ đang cố gắng giới thiệu một 'EditText', một' văn bản123', không nằm trong bố cục của hộp thoại thì bạn hoàn toàn chính xác! – TronicZomB

0

Vấn đề của bạn là bạn đang cố gắng làm .show() trên AlertDialog Builder, không phải là AlertDialog riêng của mình.

Hãy thử đoạn mã sau:

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(...) ... 
    AlertDialog myAlert = searchDialog.create(); //returns an AlertDialog from a Builder. 
    myAlert.show(); 
} 
+1

Thực ra, có thể gọi 'show()' trên một 'Builder', nó chỉ tạo ra' AlertDialog' và hiển thị nó. http://developer.android.com/reference/android/app/AlertDialog.Builder.html – zbr

+0

@Zabri Oh ok. Tôi không biết điều đó. Cảm ơn! – TronicZomB

+1

Cách tiếp cận tốt. Giải pháp là tạo một hộp thoại Shell, khởi tạo nó với Builder, setContentView trên hộp thoại, setView trên Builder, sau đó findViewById trên cá thể hộp thoại. – Fer

0

Tôi gặp sự cố tương tự khi lập trình ứng dụng đa ngôn ngữ. Cuối cùng tôi phát hiện ra rằng tôi quên cập nhật nhãn trong các tệp [xml] hoạt động của bố cục.

Tôi đã có 3 trong số họ mỗi hoạt động:

<activity_name>.xml 
<activity_name>.xml(land) 
<activity_name>.xml(iw) 

tôi cập nhật chỉ là người đầu tiên và quên cập nhật khác như sau:

Cả ba đã có một TextView duy nhất với id của:

<TextView 
    android:id="@+id/loading_textbox" 
    .../> 

Sau đó, tôi đã thay đổi tên của id đó xem văn bản - chỉ trong file đầu tiên - để:

<TextView 
    android:id="@+id/status_textbox" 
    .../> 

Và, tất-nhiên, trong mã Java của hoạt động (sử dụng cả ba ...):

TextView tv = findViewByID(R.id.status_textbox); 

này làm việc cho (tiếng Anh <activity_name>.xml) phiên bản thông thường.

Nhưng khi tôi đã đi ngôn ngữ IW (tiếng Hebrew <activity_name>xml(iw)) tv có giá trị null và tôi thậm chí có một số nghiền nát.

Khi tôi thay đổi id xem nội dung của các tập tin khác để "@+id/status_textbox" tất cả mọi thứ đã làm việc như một nét duyên dáng ...

Vì vậy, chỉ cần chắc chắn rằng tất cả các ID của bạn được cập nhật và hạch toán vào tất cả các bố trí và ngôn ngữ của bạn.

này giải quyết vấn đề của tôi anyway ..