2014-01-20 70 views
1

我創建一個列表視圖,並希望以顯示所選單選按鈕的文字作爲其子項的項目。只需使用Arraylist進行測試,而不是使用簡單的適配器。這不是我現在關心的問題,而是ListView的click事件,它顯示了NullPointerException。提到了幾個較早的帖子,但沒有幫助。空指針異常而選擇的ListView

WForecast.java

public class WForecastHome extends Activity { 

    ListView list; 
    Button search; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.weatherforecast_home); 

     search = (Button)findViewById(R.id.BTSearch); 
     list = (ListView) findViewById(R.id.listView1); 

     String[] s = new String[]{"Temparature format", 
       "Update interval"}; 

     search.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
     }); 

     ArrayAdapter<String> aa = new ArrayAdapter<String> (WForecastHome.this, android.R.layout.simple_expandable_list_item_1 , s); 
     list.setAdapter(aa); 

     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       // TODO Auto-generated method stub 

       Dialog dialog = new Dialog(WForecastHome.this); 
       dialog.setTitle("Select temprature format"); 
       dialog.setContentView(R.layout.dialog); 
       dialog.show(); 

       RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1); 
       final RadioButton celcius = (RadioButton) findViewById(R.id.celcius); 
       final RadioButton fahrenheit = (RadioButton) findViewById(R.id.fahrenheit); 

       rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(RadioGroup group, int checkedId) { 
         // TODO Auto-generated method stub 
         switch(checkedId) 
         { 
         case R.id.celcius : 
          celcius.getText(); 
          break; 

         case R.id.fahrenheit : 
          fahrenheit.getText(); 
         } 
        } 
       }); 

      } 
     }); 
    } 

} 

logcat的錯誤

01-20 22:16:17.648: D/ddm-heap(333): Got feature list request 
01-20 22:16:17.788: W/ResourceType(333): Skipping entry 0x7f020000 in package table 0 because it is not complex! 
01-20 22:16:21.528: W/ResourceType(333): Skipping entry 0x7f020000 in package table 0 because it is not complex! 
01-20 22:16:23.208: D/AndroidRuntime(333): Shutting down VM 
01-20 22:16:23.208: W/dalvikvm(333): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) 
01-20 22:16:23.208: E/AndroidRuntime(333): Uncaught handler: thread main exiting due to uncaught exception 
01-20 22:16:23.218: E/AndroidRuntime(333): java.lang.NullPointerException 
01-20 22:16:23.218: E/AndroidRuntime(333): at com.nsa.WForecastHome$2.onItemClick(WForecastHome.java:61) 
01-20 22:16:23.218: E/AndroidRuntime(333): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
01-20 22:16:23.218: E/AndroidRuntime(333): at android.widget.ListView.performItemClick(ListView.java:3246) 
01-20 22:16:23.218: E/AndroidRuntime(333): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1635) 

請求幫助我識別錯誤,什麼導致它。 非常感謝。

回答

1

更改爲

RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.radioGroup1); 
final RadioButton celcius = (RadioButton) dialog.findViewById(R.id.celcius); 
final RadioButton fahrenheit = (RadioButton)dialog. findViewById(R.id.fahrenheit); 

我猜你在dialog.xml的意見。因此按上述初始化您的觀點。

findViewById查找與當前膨脹佈局ID的圖。在你的情況下,你將自定義佈局設置爲對話框。因此,使用該對話框對象初始化你的意見

+0

是這樣造成的錯誤?如果是,如何?對不起,沒有得到邏輯。 – Saggy

+0

@Saggy顯示你的dialog.xml。如果你在dialog.xml中有單選按鈕,那麼你應該按照我的文章中的建議初始化它們。你沒有得到什麼邏輯?它的NUllPointerExcpetion指示第61行的內容爲null。什麼是61行代碼中的 – Raghunandan

+0

是的,我各方面的意見,包括只dialog.xml單選按鈕。我沒有得到的觀點是爲什麼我們需要在dialog.findViewById(R.id.radioGroup1)中添加「dialog」。並進一步。我沒有早些使用這種方式,所有的程序都在運行。艱難的這解決了我的問題。 61 – Saggy