所以看来错误是在对话框内的按钮,不知道如何修复它
我有一个任务是创建一个程序,从对话框中的EditText中捕获值,然后将它们用于一个简单的等式...我必须说我是非常新的android编程,所以不知道多少程序当我运行它时不断崩溃,我不知道为什么并且无法自己检测到任何问题,我将添加代码,如果有人可以检测到任何(或许多)问题,请指出它们,谢谢。
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 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);
btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
final Dialog d1 = new Dialog(MainActivity.this);
d1.setContentView(R.layout.valor);
d1.setTitle("Valor de R1");
final EditText ed1 = (EditText) findViewById(R.id.editText1);
d1.show();
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int r1=Integer.parseInt(ed1.getText().toString());
txt1.setText(String.valueOf(r1));}
catch(NumberFormatException nfe)
{
nfe.printStackTrace();
}
txt1.setText(String.valueOf(r1));
d1.dismiss();
};
});
}
});
**NOTE: THIS SAME SEQUENCE IS REPEATED TWICE MORE FOR THE 2 OTHER VARIABLES **
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));
};
});}
logcat的:
03-12 21:06:12.781: E/ViewRootImpl(29660): sendUserActionEvent() mView == null
03-12 21:06:17.966: D/AndroidRuntime(29660): Shutting down VM
03-12 21:06:17.966: W/dalvikvm(29660): threadid=1: thread exiting with uncaught exception (group=0x41d11700)
03-12 21:06:17.971: E/AndroidRuntime(29660): FATAL EXCEPTION: main
03-12 21:06:17.971: E/AndroidRuntime(29660): java.lang.NullPointerException
03-12 21:06:17.971: E/AndroidRuntime(29660): at com.uia.examenno1.MainActivity$1.onClick(MainActivity.java:49)
03-12 21:06:17.971: E/AndroidRuntime(29660): at android.view.View.performClick(View.java:4489)
03-12 21:06:17.971: E/AndroidRuntime(29660): at android.view.View$PerformClick.run(View.java:18803)
03-12 21:06:17.971: E/AndroidRuntime(29660): at android.os.Handler.handleCallback(Handler.java:730)
03-12 21:06:17.971: E/AndroidRuntime(29660): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 21:06:17.971: E/AndroidRuntime(29660): at android.os.Looper.loop(Looper.java:137)
03-12 21:06:17.971: E/AndroidRuntime(29660): at android.app.ActivityThread.main(ActivityThread.java:5493)
03-12 21:06:17.971: E/AndroidRuntime(29660): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 21:06:17.971: E/AndroidRuntime(29660): at java.lang.reflect.Method.invoke(Method.java:525)
03-12 21:06:17.971: E/AndroidRuntime(29660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-12 21:06:17.971: E/AndroidRuntime(29660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-12 21:06:17.971: E/AndroidRuntime(29660): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
由于您没有处理任何异常,因此它会崩溃,因为可能无法将txt1.toString()解析为Integer。你应该改为使用txt1.getText()。toString()。如果这不是导致问题,请附上您的日志,您可以考虑阅读重构书籍:
try {
Integer.parseInt(txt1.getText().toString());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
答案 1 :(得分:0)
您的d1.setContentView
方法不信任。
尝试使用android.r.layout.simple_list_item_1