我是 android 编程和构建我的第一个应用程序的新手。它应该根据 RadioGroup 中选择的内容显示关于两个数字的消息。我的问题是我在单击“查找”按钮时不断出错,与onClick有关。
public void onCheckedChanged(RadioGroup radio, int checkedId) {
count = checkedId;
}
public void executa(View v){
RadioButton radioButton1=(RadioButton) findViewById(R.id.radioButton1);
RadioButton radioButton2=(RadioButton) findViewById(R.id.radioButton2);
RadioButton radioButton3=(RadioButton) findViewById(R.id.radioButton3);
RadioButton radioButton4=(RadioButton) findViewById(R.id.radioButton4);
Button button=(Button) findViewById(R.id.button);
RadioGroup radio=(RadioGroup) findViewById(R.id.radio);
if (v.getId() == R.id.button){
count = radio.getCheckedRadioButtonId();
EditText editText1 = (EditText) findViewById(R.id.Text1);
int nr1=Integer.getInteger(editText1.getText().toString());
EditText editText2 = (EditText) findViewById(R.id.Text2);
int nr2=Integer.getInteger(editText2.getText().toString());
pozitive=0;
negative=0;
if (nr1==nr2) max=min=-1; else
if (nr1>nr2){ max=nr1;
min=nr2;}
else { max=nr2;
min=nr1;}
if (nr1>0) pozitive++;
else negative--;
if (nr2>0) pozitive++;
else negative--;
Intent intent=new Intent(this,Raspuns.class);
switch(count){
case 1:intent.putExtra(Valoare, max);
break;
case 2:intent.putExtra(Valoare, min);
break;
case 3:intent.putExtra(Valoare, pozitive);
break;
case 4:intent.putExtra(Valoare, negative);
break;
}
startActivity(intent);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return false;
}
}
此外,消息是通过意图发送到第二个活动的,从执行程序调用,通过单击 afla 按钮。感谢您的任何建议!编辑:logcat:
12-02 00:12:11.788: E/AndroidRuntime(3524): FATAL EXCEPTION: main
12-02 00:12:11.788: E/AndroidRuntime(3524): java.lang.IllegalStateException: Could not execute method of the activity
12-02 00:12:11.788: E/AndroidRuntime(3524): at android.view.View$1.onClick(View.java:3044)
12-02 00:12:11.788: E/AndroidRuntime(3524): at android.view.View.performClick(View.java:3511)
12-02 00:12:11.788: E/AndroidRuntime(3524): at android.view.View$PerformClick.run(View.java:14109)
12-02 00:12:11.788: E/AndroidRuntime(3524): at android.os.Handler.handleCallback(Handler.java:605)
12-02 00:12:11.788: E/AndroidRuntime(3524): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 00:12:11.788: E/AndroidRuntime(3524): at android.os.Looper.loop(Looper.java:137)
12-02 00:12:11.788: E/AndroidRuntime(3524): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-02 00:12:11.788: E/AndroidRuntime(3524): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 00:12:11.788: E/AndroidRuntime(3524): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 00:12:11.788: E/AndroidRuntime(3524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-02 00:12:11.788: E/AndroidRuntime(3524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-02 00:12:11.788: E/AndroidRuntime(3524): at dalvik.system.NativeStart.main(Native Method)
12-02 00:12:11.788: E/AndroidRuntime(3524): Caused by: java.lang.reflect.InvocationTargetException
12-02 00:12:11.788: E/AndroidRuntime(3524): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 00:12:11.788: E/AndroidRuntime(3524): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 00:12:11.788: E/AndroidRuntime(3524): at android.view.View$1.onClick(View.java:3039)
12-02 00:12:11.788: E/AndroidRuntime(3524): ... 11 more
12-02 00:12:11.788: E/AndroidRuntime(3524): Caused by: java.lang.NullPointerException
12-02 00:12:11.788: E/AndroidRuntime(3524): at com.example.myfirstrealandroidapp.MainActivity.executa(MainActivity.java:48)
12-02 00:12:11.788: E/AndroidRuntime(3524): ... 14 more