我正在创建一个列表视图,并希望将所选单选按钮的文本显示为其子项。只需使用Arraylist进行测试即可,目前还没有使用简单的适配器。这不是我现在关心的问题,但它是显示NullPointerException的ListView的click事件。也参考了一些较早的文章,但没有帮助。

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)


要求帮助我确定错误及其原因。
万分感谢。

最佳答案

改成

 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的视图。您需要将自定义布局设置为对话框。因此,使用对话框对象初始化您的视图

10-08 12:09