我是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按钮,消息通过意图发送到第二个活动,从executa调用。感谢任何建议! 编辑: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
答案 0 :(得分:0)
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);
radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
count = checkedId;
}
});
答案 1 :(得分:0)
感谢您的投入!我的应用程序的错误似乎是我使用"Integer.getInt
“而不是”Integer.parseInt"
。此外,当我使用收音机监听器和按钮时,它给出了正确的答案