选择ListView项时的空指针异常

时间:2014-01-20 17:17:02

标签: android android-listview nullpointerexception

我正在创建一个列表视图,并希望将所选单选按钮的文本显示为它的子项。只需使用Arraylist进行测试,而不是使用简单的适配器。这不是我现在关注的问题,而是ListView的click事件,它显示了NullPointerException。之前的帖子也很少提及,但没有帮助。

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 个答案:

答案 0 :(得分: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为id的视图。在您的情况下,您将自定义布局设置为对话框。因此,使用对话框对象初始化视图