2013-04-24 8 views
10

मैं हार मानता हूं ... यहां तक ​​कि मेरे प्रोफेसर भी नहीं जानते कि यह क्यों हो रहा है।findViewById() शून्य लौटाता है -

समस्या यह है: कोई फर्क नहीं पड़ता कि मैं इस लेआउट के घटकों के लिए कहां या कैसे कॉल करता हूं, वे हमेशा शून्य लौटते हैं।

लेआउट: search_layout। 2 EdiText और 2 TextView बच्चे।

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) 

अपवाद:, एक्सएमएल के अपवाद के पार्स के रूप में संसाधन, नहीं मिला इसलिए अशक्त।

कोशिश की थी: चारों ओर 1024 बार अपने प्रोजेक्ट साफ़ किया, मेरे संवाद बॉक्स में, एक ही परिणाम,, विधि initSearch (अंदर findViewById मेरी मुख्य गतिविधि के एक सदस्य के रूप में बुलाया के लिए एक और वर्ग) को लागू करने और के लिए OnClickListener की एक अनाम कार्यान्वयन के अंदर करने की कोशिश की मेरा संवाद, एक ही परिणाम। स्वतंत्र विचारों में बच्चों को तोड़ दिया और प्रोग्राम के रूप में उन्हें बुलाया:

TextView text = (TextView) findResourceById(R.id.new_independant_textview); // same result 

मुझे लगता है कि मैं सब कुछ करने की कोशिश की।

यह मेरा कोड है:

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(); 
    } 

अब, जब मैं करता हूँ:

<?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> 

यह मेरा आर है:

EditText text = (EditText) findViewById(R.id.tagField); // returns null, ALWAYS 
// no matter HOW, WHERE, global, local final, etc, it just returns null. 

यहाँ मेरी कस्टम लेआउट की एक्सएमएल है जावा फ़ाइल:

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 
} 

आपकी मदद के लिए धन्यवाद, खुश शिकार।

+0

आप संवाद किसी अन्य गतिविधि से findViewById बुला ... अच्छी किस्मत में देखने लगता है wana ... – Selvin

+0

दिलचस्प लगता है, लेकिन मैं fuly समझ नहीं कर रहा हूँ क्यों मैं एक नई गतिविधि की जरूरत है। फिर क्यों R.layot.search_dialog सही परत लौटाता है फिर भी यह बच्चे शून्य हैं? वहां कोई स्थिरता नहीं है। – Fer

उत्तर

38

findViewById() करके अपने गतिविधि के लेआउट और नहीं अपने संवाद के दृश्य के भीतर विचारों के लिए खोज करेंगे:

निम्नलिखित कोड का प्रयास करें। आपको findViewById() को विशिष्ट View पर कॉल करने की आवश्यकता है जिसे आपने अपने संवाद के लेआउट के रूप में सेट किया है।

इस

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(); 
} 

सूचना कैसे मैं दृश्य बढ़ा-चढ़ाकर कर रहा हूँ और एक ViewdialogView नामित में भंडारण की कोशिश करो। फिर, EditText नाम tagField नामित करने के लिए, मैं dialogView.findViewById(R.id.tagField);

+0

सही उत्तर। धन्यवाद दोस्तों। – Fer

1

TextView आईडी text123 के साथ अंदर Layout आपकी समस्या आप setContentView

+0

असल में, मुझे लगता है कि 'text123' एक उदाहरण होने के लिए था क्योंकि ओपी में' एडिटटेक्स्ट टैगटेक्स्ट = (एडीटेक्स्ट) है। व्यूबीआईआईडी (आर.आईडी.एफ़फिल्ड); // अपने "मुख्य" कोड खंड में शून्य वापस लौटाएंगे। लेकिन इस जवाब ने मुझे चकित कर दिया। +1। – TronicZomB

+0

@TronicZomB मैंने इस एडिटटेक्स्ट टेक्स्ट = (एडिटटेक्स्ट) को देखा है ViewById (R.id.text123); // रिटर्न शून्य, हमेशा – Blackbelt

+0

हाँ मुझे पता है, मुझे कम से कम उम्मीद है कि ओपी एक उदाहरण के रूप में इसका उपयोग कर रहा था, क्या मुझे लगता है कि मैं क्या कहने की कोशिश कर रहा था। चूंकि पोस्ट कोड के अन्य हिस्सों में ऐसा लगता है कि वे आइटम को सही तरीके से संदर्भित कर रहे हैं। लेकिन अगर वे 'एडिटटेक्स्ट' को फिर से शुरू करने का प्रयास कर रहे हैं, तो एक 'text123', जो संवाद के लेआउट के भीतर नहीं है, तो आप पूरी तरह से सही हैं! – TronicZomB

0

के साथ सेट आप एक AlertDialog बिल्डर, नहीं AlertDialog पर ही .show() करने के लिए कोशिश कर रहे हैं घोषित किया जाना है।

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

असल में, 'बिल्डर' पर 'शो()' को कॉल करना संभव है, यह सिर्फ 'अलर्टडिअलॉग' बनाता है और इसे दिखाता है। http://developer.android।कॉम/संदर्भ/एंड्रॉइड/ऐप/AlertDialog.Builder.html – zbr

+0

@Zabri ओह ठीक है। मैं नहीं जानता था कि। धन्यवाद! – TronicZomB

+1

हालांकि अच्छा दृष्टिकोण। समाधान एक खोल संवाद बनाना है, इसे बिल्डर के साथ तुरंत चालू करें, संवाद पर सेटकंटेंट व्यू, बिल्डर पर setView, फिर संवाद उदाहरण परViewById खोजें। – Fer

0

का उपयोग कर रहा हूं, मुझे बहुभाषी ऐप प्रोग्रामिंग करते समय भी वही समस्या थी। आखिरकार मुझे पता चला कि मैं कुछ लेआउट की गतिविधि [xml] फ़ाइलों में लेबल अपडेट करना भूल गया था।

मैं गतिविधि प्रति उनमें से 3 था:

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

मैं केवल प्रथम अद्यतन और इस प्रकार अन्य अपडेट करने के लिए भूल गया:

तीनों की आईडी के साथ एक एकल TextView था:

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

तो मुझे लगता है कि पाठ को देखने के आईडी का नाम बदल दिया गया है - केवल पहले फाइल में - करने के लिए:

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

और, पाठ्यक्रम, गतिविधि के जावा कोड में (कि तीनों का उपयोग करता है ...):

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

यह नियमित रूप से (अंग्रेजी <activity_name>.xml) संस्करण के लिए काम किया।

लेकिन जब मैं आईडब्ल्यू भाषा (हिब्रू <activity_name>xml(iw)) चला गया तो tv शून्य मूल्य मिला और मुझे कुछ क्रश भी मिला।

जब मैं "@+id/status_textbox" सब कुछ करने के लिए अन्य फ़ाइलों के पाठ दृश्य आईडी बदल एक आकर्षण की तरह काम किया ...

तो, बस सुनिश्चित करें कि आपके सभी आईडी पहले से अपडेट कर रहे हैं और अपने सभी लेआउट और भाषाओं में के लिए जिम्मेदार है।

यह वैसे भी मेरी समस्या हल ..

संबंधित मुद्दे