对话框上的设置按钮崩溃

时间:2014-03-13 06:20:32

标签: android button dialog crash

每当我在对话框中声明按钮时,当我启动对话时,应用程序崩溃。 如果我删除btn2.OnClickListener及其中的所有内容,对话就会启动。 有人有任何想法吗?

package com.uia.examenno1;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
Dialog d1;
Integer r1, r2, vol, vot;
EditText ed1;
TextView txt1, txt2, txt3, txt4;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn1= (Button) findViewById(R.id.button1);
    Button btn3= (Button) findViewById(R.id.button3);
    Button btn4= (Button) findViewById(R.id.button4);
    Button btn5= (Button) findViewById(R.id.button5);
    final Button btn2 = (Button) findViewById(R.id.button2);
    final TextView txt1= (TextView) findViewById(R.id.textView1);
    final TextView txt2= (TextView) findViewById(R.id.textView2);
    final TextView txt3= (TextView) findViewById(R.id.textView3);
    final TextView txt4=(TextView) findViewById(R.id.textView4);
    final int r1= Integer.valueOf(1000);
    final int r2 =Integer.valueOf(1000);
    final int vol=Integer.valueOf(10);
    final EditText ed1 = (EditText) findViewById(R.id.editText1);
    final Dialog d1 = new Dialog(MainActivity.this);




btn1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) {
        d1.setContentView(R.layout.valor);
        d1.setTitle("Valor de R1");
        d1.show();
        btn2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                 //TODO Auto-generated method stub
                try{
                int r1 =Integer.parseInt(ed1.getText().toString());
                txt1.setText(ed1.getText());}
                catch(NumberFormatException nfe)
                {
                    nfe.printStackTrace();    
                }
                d1.dismiss();
            };
        });
    }
});
btn3.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) {
        d1.setContentView(R.layout.valor);
        d1.setTitle("Valor de R2");
        d1.show();
        btn2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int r2=Integer.parseInt(ed1.getText().toString());
                txt2.setText(String.valueOf(r2));
                d1.dismiss();
            };
        });
    }
});
btn4.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) {
        d1.setContentView(R.layout.valor);
        d1.setTitle("Valor de V");
        d1.show();
        btn2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int vol=Integer.parseInt(ed1.getText().toString());
                txt3.setText(String.valueOf(vol));
                d1.dismiss();
            };
        });
    }
});
btn5.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        vot=(r2/(r1+r2))*vol;
        txt4.setText("Resultado: " +String.valueOf(vot));
    };
});}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

LOG:

03-12 23:23:28.096: D/dalvikvm(17866): GC_FOR_ALLOC freed 1398K, 19% free 12719K/15612K, paused 10ms, total 10ms

03-12 23:23:29.921: D/AndroidRuntime(17866): Shutting down VM

03-12 23:23:29.921: W/dalvikvm(17866): threadid=1: thread exiting with uncaught exception (group=0x41d11700)

03-12 23:23:29.921: E/AndroidRuntime(17866): FATAL EXCEPTION: main

03-12 23:23:29.921: E/AndroidRuntime(17866): java.lang.NullPointerException

03-12 23:23:29.921: E/AndroidRuntime(17866):    at 

com.uia.examenno1.MainActivity$1.onClick(MainActivity.java:49)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at android.view.View.performClick(View.java:4489)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at android.view.View$PerformClick.run(View.java:18803)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at android.os.Handler.handleCallback(Handler.java:730)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at android.os.Handler.dispatchMessage(Handler.java:92)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at android.os.Looper.loop(Looper.java:137)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at android.app.ActivityThread.main(ActivityThread.java:5493)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at java.lang.reflect.Method.invokeNative(Native Method)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at java.lang.reflect.Method.invoke(Method.java:525)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)

03-12 23:23:29.921: E/AndroidRuntime(17866):    at dalvik.system.NativeStart.main(Native Method)

03-12 23:23:32.351: I/Process(17866): Sending signal. PID: 17866 SIG: 9

2 个答案:

答案 0 :(得分:2)

您需要在显示对话框后定义btn2

试试这个

btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            d1.setContentView(R.layout.valor);
            d1.setTitle("Valor de R1");
            d1.show();

            Button btn2 = (Button) d1.findViewById(R.id.button2);
            btn2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    try {
                        int r1 = Integer.parseInt(ed1.getText().toString());
                        txt1.setText(ed1.getText());
                    } catch (NumberFormatException nfe) {
                        nfe.printStackTrace();
                    }
                    d1.dismiss();
                };
            });
        }
    });

答案 1 :(得分:0)

您应该在对话框中定义按钮,如下所示:

Button btn2 = (Button) layout.findViewById(R.id.btn2);

对话框布局的布局为View

layout = inflater.inflate(R.layout.dialog, null);