的方法,這是我的代碼致命異常:主要java.lang.IllegalStateException:無法執行活動
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
private RadioButton r1;
private RadioButton r2;
private RadioButton r3;
private RadioButton r4;
private RadioButton r5;
private RadioButton r6;
private RadioGroup rg1;
private RadioGroup rg2;
private EditText v;
private EditText v2;
public Button button;
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r1 = (RadioButton) findViewById(R.id.r1);
r2 = (RadioButton) findViewById(R.id.r2);
r3 = (RadioButton) findViewById(R.id.r3);
r4 = (RadioButton) findViewById(R.id.r4);
r5 = (RadioButton) findViewById(R.id.r5);
r6 = (RadioButton) findViewById(R.id.r6);
rg1 = (RadioGroup) findViewById(R.id.rg1);
rg2 = (RadioGroup) findViewById(R.id.rg2);
v = (EditText) findViewById(R.id.v);
v2 = (EditText) findViewById(R.id.v2);
button = (Button) findViewById(R.id.button);
spinner = (Spinner) findViewById(R.id.spinner);
rg1.setOnCheckedChangeListener(this);
rg2.setOnCheckedChangeListener(this);
String[] unidades={"Volumen","Longitud","Masa","Temperatura"};
ArrayAdapter<String> lista = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, unidades);
spinner.setAdapter(lista);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {
switch (i){
case 0:
r1.setText("Litros");
r2.setText("MiliLitros");
r3.setText("Onzas");
r4.setText("Litros");
r5.setText("MiliLitros");
r6.setText("Onzas");
break;
case 1:
r1.setText("cm");
r2.setText("m");
r3.setText("Km");
r4.setText("cm");
r5.setText("m");
r6.setText("Km");
break;
case 2:
r1.setText("g");
r2.setText("Kg");
r3.setText("Lb");
r4.setText("g");
r5.setText("Kg");
r6.setText("Lb");
break;
case 3:
r1.setText("Kelvin");
r2.setText("Farenhaith");
r3.setText("Centigrados");
r4.setText("Kelvin");
r5.setText("Farenhaith");
r6.setText("Centigrados");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void onClick (View view){
String selec=spinner.getSelectedItem().toString();
if (selec.equals("Volumen")) {
if (r1.isChecked() && r5.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
int c = b*1000;
v2.setText(c);
}
if (r1.isChecked() && r6.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = b*33.814;
v2.setText((int)c);
}
if (r2.isChecked() && r4.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
int c = b/1000;
v2.setText(c);
}
if (r2.isChecked() && r6.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = b*0.033814;
v2.setText((int)c);
}
if (r3.isChecked() && r4.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = b/33.814;
v2.setText((int)c);
}
if (r3.isChecked() && r5.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = b/0.033814;
v2.setText((int)c);
}
}
String selec1=spinner.getSelectedItem().toString();
if (selec1.equals("Longitud")) {
if (r1.isChecked() && r5.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
int c = b/100;
v2.setText(c);
}
if (r1.isChecked() && r6.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
int c = b/100000;
v2.setText(c);
}
if (r2.isChecked() && r4.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
int c = b * 100;
v2.setText(c);
}
if (r2.isChecked() && r6.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
int c = b * 1000;
v2.setText(c);
}
if (r3.isChecked() && r4.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
int c = b * 100000;
v2.setText(c);
}
if (r3.isChecked() && r5.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
int c = b/1000;
v2.setText(c);
}
}
String selec2=spinner.getSelectedItem().toString();
if (selec2.equals("Masa")){
if (r1.isChecked() && r5.isChecked()) {
String a= v.getText().toString();
int b = Integer.parseInt(a);
double c = b*0.001;
v2.setText((int) c);
}
if (r1.isChecked() && r6.isChecked()) {
String a= v.getText().toString();
int b = Integer.parseInt(a);
double c = b*0.0022046;
v2.setText((int) c);
}
if (r2.isChecked() && r4.isChecked()){
String a= v.getText().toString();
int b = Integer.parseInt(a);
double c = b/0.001;
v2.setText((int) c);
}
if (r2.isChecked() && r6.isChecked()){
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = b * 2.2046;
v2.setText((int) c);
}
if (r3.isChecked() && r4.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = b/0.0022046;
v2.setText((int) c);
}
if (r3.isChecked() && r4.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = b/2.2046;
v2.setText((int) c);
}
}
String selec3=spinner.getSelectedItem().toString();
if (selec3.equals("Temperatura")){
if (r1.isChecked() && r5.isChecked()) {
String a= v.getText().toString();
int b = Integer.parseInt(a);
double c = (1.8*(b-273))+32;
v2.setText((int)c);
}
if (r1.isChecked() && r6.isChecked()) {
String a= v.getText().toString();
int b = Integer.parseInt(a);
double c = b-273.15;
v2.setText((int) c);
}
if (r2.isChecked() && r4.isChecked()){
String a= v.getText().toString();
int b = Integer.parseInt(a);
double c = (b-32)/1.8;
double d = c+273.15;
v2.setText((int) d);
}
if (r2.isChecked() && r6.isChecked()){
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = (b-32)/1.8;
v2.setText((int) c);
}
if (r3.isChecked() && r4.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = b +273.15;
v2.setText((int) c);
}
if (r3.isChecked() && r4.isChecked()) {
String a = v.getText().toString();
int b = Integer.parseInt(a);
double c = (b*1.8)+32;
v2.setText((int) c);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.conversor, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
}
//這是錯誤:
FATAL EXCEPTION: main
Process: com.example.user.conversor, PID: 13477
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2710
at android.content.res.Resources.getText(Resources.java:244)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.user.conversor.MainActivity.onClick(MainActivity.java:118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
//AND THIS IS DE XML CODE:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Seleccionar"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Origen"
android:id="@+id/textView2"
android:layout_below="@+id/spinner"
android:layout_centerHorizontal="true" />
<RadioGroup
android:id="@+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView2">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r1"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r2"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r3"
android:checked="false" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="41dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Destino"
android:id="@+id/textView4"
android:layout_gravity="center_horizontal"
android:layout_above="@+id/rg2"
android:layout_centerHorizontal="true" />
<RadioGroup
android:id="@+id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="114dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r4"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r5"
android:checked="false"
android:layout_alignParentTop="true"
android:layout_alignEnd="@+id/textView3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r6"
android:checked="false" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convertir"
android:id="@+id/button"
android:onClick="onClick"
android:layout_gravity="center_horizontal"
android:layout_above="@+id/v2"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="147dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/v2"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Valor"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/rg1"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/v"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/textView"
android:layout_alignEnd="@+id/textView" />
</RelativeLayout>
請幫幫我,是什麼問題? –
發佈一個合適的問題,如果你有錯誤發佈stacktrace到它 – XtremeBaumer
一個問題是,你沒有向我們展示堆棧跟蹤...甚至是完整的異常消息。 –