2011-05-09 18 views
5

मैं कोड:findViewById रिटर्न अशक्त

public class HelloWorld extends Activity { 

private Button buttonOk; 
private Button buttonCancel; 

private OnClickListener buttonOkListener = new OnClickListener() { 
    public void onClick(View v){ 
     EditText editText = (EditText)findViewById(R.id.input); 

     CharSequence textFromInput = (CharSequence) editText.getText(); 
     Context context = getApplicationContext(); 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context,textFromInput,duration); 
     toast.show(); 
    } 
}; 

private OnClickListener buttonCancelListener = new OnClickListener() { 
    public void onClick(View v){ 
     EditText editText = (EditText)findViewById(R.id.input); 

     CharSequence textFromInput = (CharSequence) editText.getText(); 
     Context context = getApplicationContext(); 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context,textFromInput,duration); 
     toast.show(); 
    } 
}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    // ToDo add your GUI initialization code here 

    setContentView(R.layout.main); 

    buttonOk = (Button)findViewById(R.id.buttonOk); 
    buttonCancel = (Button)findViewById(R.id.buttonCancel); 

    buttonOk.setOnClickListener(buttonOkListener); 
    buttonCancel.setOnClickListener(buttonCancelListener); 
} 
} 

और लेआउट फ़ाइल: buttonCancel.setOnClickListener (buttonCancelListener) के साथ

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Wpisz swoje imię:"/> 
<EditText 
    android:id="@+id/input" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:drawable/editbox_background" 
    android:layout_below="@id/label"/> 
<Button 
    android:id="@+id/buttonOk" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/input" 
    android:layout_alignParentRight="true" 
    android:layout_marginLeft="10dip" 
    android:text="OK" /> 
<Button 
    android:id="@+id/buttonCancel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/buttonOk" 
    android:layout_alignTop="@id/buttonOk" 
    android:text="Anuluj" /> 
</RelativeLayout> 

रेखा; एक अपवाद फेंकता है क्योंकि बटन कैंसल शून्य है। क्या मैं गलत हूं?

+0

मुझे एक ही समस्या का सामना करना पड़ रहा है। भले ही कोड में सबकुछ सही लगता है, FindViewById बटनों में से एक के साथ शून्य हो जाता है। – castle1971

उत्तर

10

आपके कोड के साथ कोई समस्या नहीं है, सही सब कुछ सामान्य के अनुसार काम करना चाहिए।

findViewById विधि के माध्यम से नल का सामना करने की सबसे आम गलती तब होती है जब आप setContentView को कॉल करना भूल जाते हैं या इसे गलत लेआउट के लिए बुलाते हैं।

मैं आपके प्रोजेक्ट की सफाई करने का सुझाव देता हूं और पुनः प्रयास करता हूं !!!!

1

मैं 100% निश्चित नहीं हूं लेकिन आप क्लास प्रारंभिकरण में findviewbyid को कॉल कर रहे हैं। मुझे लगता है कि इस कोड को ऑनक्रेट विधि से पहले बुलाया जाता है ताकि दृश्य नहीं मिल सके। श्रोताओं को शुरुआती विधि में शुरू करना चाहिए।

2

मुझे एक ही समस्या हो रही थी, लेकिन मेरी परियोजना को साफ करने और इसे फिर से चलाने के बाद यह पूरी तरह से काम करता है।

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