0

我一直在尝试在 Eclipse 中制作我的第一个 android 应用程序(一个简单的温度转换器),但是当我单击手机上的按钮时,应用程序崩溃了。这是完整的java代码

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
String number;
int number2;
int output;
boolean F;
public void onBtnClicked(View view){

    EditText mEdit = (EditText)findViewById(R.id.editText1);
    TextView myTextView = (TextView) findViewById(R.id.label);

number = mEdit.getText().toString();
number2 = Integer.parseInt(number);

if(F=true){
output=number2*9/5+32;
}
else{
output=number2-32*5/9;
}

myTextView.setText(output);
} 

public void onRadioButtonClicked(View view) {

    boolean checked = ((RadioButton) view).isChecked();

    switch(view.getId()) {
        case R.id.radio0:
            if (checked)
                F = true;
            break;
        case R.id.radio1:
            if (checked)
                F = false;
            break;
    }
}
}

单击按钮时的 LogCat

04-13 20:19:50.423: E/AndroidRuntime(25200): FATAL EXCEPTION: main
04-13 20:19:50.423: E/AndroidRuntime(25200): java.lang.IllegalStateException: Could not execute method of the activity
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.view.View$1.onClick(View.java:3674)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.view.View.performClick(View.java:4198)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.view.View$PerformClick.run(View.java:17158)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.os.Handler.handleCallback(Handler.java:615)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.os.Looper.loop(Looper.java:137)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.app.ActivityThread.main(ActivityThread.java:4918)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at dalvik.system.NativeStart.main(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200): Caused by: java.lang.reflect.InvocationTargetException
04-13 20:19:50.423: E/AndroidRuntime(25200):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.view.View$1.onClick(View.java:3669)
04-13 20:19:50.423: E/AndroidRuntime(25200):    ... 11 more
04-13 20:19:50.423: E/AndroidRuntime(25200): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x59
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.content.res.Resources.getText(Resources.java:242)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.widget.TextView.setText(TextView.java:3773)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at  com.example.myfirstapp.MainActivity.onBtnClicked(MainActivity.java:43)
04-13 20:19:50.423: E/AndroidRuntime(25200):    ... 14 more
04-13 20:19:50.453: E/android.os.Debug(718): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

最后是按钮的xml

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:onClick="onBtnClicked"
    android:text="Calculate" />

我不确定如何解决这个问题,所以希望有人可以提供帮助。谢谢。

4

4 回答 4

7

首先初始化您的按钮,然后将 onclicklistener 设置为它们

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //initialize views here 
    EditText mEdit = (EditText) findViewById(R.id.editText1);
    TextView myTextView = (TextView) findViewById(R.id.label);
    Button yourButton = (Button) findViewByid(R.id.youridforbutton);
    //set onclicklistener for your button
    yourbutton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number = mEdit.getText().toString();
                number2 = Integer.parseInt(number);

                if (F = true) {
                    output = number2 * 9 / 5 + 32;
                } else {
                    output = number2 - 32 * 5 / 9;
                }

                myTextView.setText("" + output);
            }
        });

}

同样设置其他按钮也

于 2013-04-14T00:55:35.357 回答
3

您需要将输出转换为字符串

myTextView.setText(String.valueOf(output));

setText 方法重载,当您将整数传递给它时,它期望它是资源的 id。

于 2013-04-14T00:49:31.420 回答
1

您不能将整数设置为 TextViews。您必须将它们转换为字符串。

myTextView.setText(String.valueOf(output));
于 2013-04-14T00:49:37.647 回答
0

您需要告诉点击侦听器侦听特定按钮。所以,换句话说,在按钮上设置一个 OnItemClickListener。如下所示:

Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    // do the "on click" action here
  }
});

希望这可以帮助。如果没有,那么请评论进一步的问题。

于 2013-04-14T00:42:09.873 回答