0

Can anyone help me with this and tell me where I do the mistake... When I try to do anything with the radiobutton Eclipse throws me this exception

threadid=1: thread exiting with uncaught exception (group=0x40015560)
FATAL EXCEPTION: main
java.lang.NullPointerException
cz.nasdaq.RbtnActivity$1.onClick(RbtnActivity.java:36)
android.view.View.performClick(View.java:2485)
android.view.View$PerformClick.run(View.java:9080)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:123)
.
.
.

with this code

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button)findViewById(R.id.btn1);        
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                final Dialog dialog = new Dialog(RbtnActivity.this);
                dialog.setContentView(R.layout.dlg);
                dialog.show();

                RadioButton drb0=(RadioButton)findViewById(R.id.DialogRb0);
                drb0.setChecked(true);
4

1 回答 1

2
RadioButton drb0=(RadioButton)findViewById(R.id.DialogRb0);
drb0.setChecked(true);

应该

RadioButton drb0=(RadioButton)dialog.findViewById(R.id.DialogRb0);
drb0.setChecked(true);

注意对话框.findViewById(R.id.DialogRb0);

当您应该在对话框的布局中搜索时,您正在主布局中搜索对话框 RadioButton。当您搜索它时,您的变量 drb0 为空,并且当您对其调用 setChecked(true) 时将导致空指针异常。

于 2012-04-20T16:41:32.797 回答