1

我有一个应用程序,我需要能够从一个活动切换到下一个活动,而且在第一个活动上,有一个温度转换器,它使用一个按钮来计算答案。这个按钮曾经可以工作,但由于我更改了 oncreate 以允许在活动之间切换工作,它已停止工作,现在只是关闭应用程序。

public class MainActivity extends Activity {
    private EditText text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), SecondScreen.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }

    // This method is called at button click because we assigned the name to the
    // "OnClick property" of the button
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.button1:
            RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
            RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
            if (text.getText().length() == 0) {
                Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show();
                return;
            }
            float inputValue = Float.parseFloat(text.getText().toString());
            if (celsiusButton.isChecked()) {
                text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
                celsiusButton.setChecked(false);
                fahrenheitButton.setChecked(true);
            }
            else {
                text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
                fahrenheitButton.setChecked(false);
                celsiusButton.setChecked(true);
            }
            break;
        }
    }

    // Converts to celsius
    private float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
    }

    // Converts to fahrenheit
    private float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
    }
 }

logcats 是空的,但这是在另一部分

 [2013-04-22 14:45:24 - ddms] Can't bind to local 8888 for debugger
   [2013-04-22 15:04:58 - Unexpected error while launching logcat. Try reselecting the                               device.] device not found
   com.android.ddmlib.AdbCommandRejectedException: device not found
   at com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:752)
   at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:373)
   at com.android.ddmlib.Device.executeShellCommand(Device.java:462)
   at com.android.ddmuilib.logcat.LogCatReceiver$1.run(LogCatReceiver.java:110)
   at java.lang.Thread.run(Unknown Source)
   [2013-04-22 15:07:18 - ddms] Can't bind to local 8888 for debugger

   4-22 15:26:56.392: W/Trace(909): Unexpected value from nativeGetEnabledTags: 0
   04-22 15:26:56.397: W/Trace(909): Unexpected value from nativeGetEnabledTags: 0
   04-22 15:26:56.417: D/AndroidRuntime(909): Shutting down VM
   04-22 15:26:56.417: W/dalvikvm(909): threadid=1: thread exiting with uncaught              exception (group=0x40a70930)
   04-22 15:26:56.447: E/AndroidRuntime(909): FATAL EXCEPTION: main
   04-22 15:26:56.447: E/AndroidRuntime(909): java.lang.IllegalStateException: Could not execute method of the activity
   04-22 15:26:56.447: E/AndroidRuntime(909): at android.view.View$1.onClick(View.java:3597)
   04-22 15:26:56.447: E/AndroidRuntime(909): at android.view.View.performClick(View.java:4202)
   04-22 15:26:56.447: E/AndroidRuntime(909): at android.view.View$PerformClick.run(View.java:17340)
  04-22 15:26:56.447: E/AndroidRuntime(909): at  android.os.Handler.handleCallback(Handler.java:725)
  04-22 15:26:56.447: E/AndroidRuntime(909): at android.os.Handler.dispatchMessage(Handler.java:92)
  04-22 15:26:56.447: E/AndroidRuntime(909): at android.os.Looper.loop(Looper.java:137)
  04-22 15:26:56.447: E/AndroidRuntime(909): at android.app.ActivityThread.main(ActivityThread.java:5039)
  04-22 15:26:56.447: E/AndroidRuntime(909): at java.lang.reflect.Method.invokeNative(Native Method)
  04-22 15:26:56.447: E/AndroidRuntime(909): at java.lang.reflect.Method.invoke(Method.java:511)
  04-22 15:26:56.447: E/AndroidRuntime(909): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  04-22 15:26:56.447: E/AndroidRuntime(909): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   04-22 15:26:56.447: E/AndroidRuntime(909): at dalvik.system.NativeStart.main(Native Method)
   04-22 15:26:56.447: E/AndroidRuntime(909): Caused by: java.lang.reflect.InvocationTargetException
   04-22 15:26:56.447: E/AndroidRuntime(909): at java.lang.reflect.Method.invokeNative(Native Method)
   04-22 15:26:56.447: E/AndroidRuntime(909): at java.lang.reflect.Method.invoke(Method.java:511)
   04-22 15:26:56.447: E/AndroidRuntime(909): at android.view.View$1.onClick(View.java:3592)
   04-22 15:26:56.447: E/AndroidRuntime(909): ... 11 more
   04-22 15:26:56.447: E/AndroidRuntime(909): Caused by: java.lang.NullPointerException
   04-22 15:26:56.447: E/AndroidRuntime(909): at com.example.assignment2project.MainActivity.tempConverterClick(MainActivity.java:42)
   04-22 15:26:56.447: E/AndroidRuntime(909): ... 14 more
4

2 回答 2

1

我认为存在冲突,您似乎没有覆盖该onClick方法,因此我假设您已将onClickxml 中的属性定义为onClick,而不是通用的 click 方法,让我们尝试以下操作:

在 xml 中,将 定义onClick为如下所示:tempConverterClick

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="tempConverterClick"
    android:text="@string/my_button_txt" />

现在在 Java 方面,让我们首先测试一下Toast

public void tempConverterClick(View view) {
    Toast.makeText(this, "Temp Converter click worked!", Toast.LENGTH_LONG).show();
}

如果可行,那么让我们在这个新的点击处理程序中尝试您的初始代码:

public void tempConverterClick(View view) {
    switch (view.getId()) {
    case R.id.button1:
        RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
        RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
        if (text.getText().length() == 0) {
            Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show();
            return;
        }
        float inputValue = Float.parseFloat(text.getText().toString());
        if (celsiusButton.isChecked()) {
            text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
            celsiusButton.setChecked(false);
            fahrenheitButton.setChecked(true);
        }
        else {
            text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
            fahrenheitButton.setChecked(false);
            celsiusButton.setChecked(true);
        }
        break;
    }
}

为避免将来的混淆以及试图提供帮助的人的混淆,我强烈建议您更改命名约定。创建与动作、目的等相关的 ID。拥有R.id.Button01并且R.id.button1非常令人困惑


编辑

看来,当您更改时,onCreate您可能已经删除了EditText实例化。

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

    // Add this line with the appropriate ID reference
    text = (EditText)findViewById(R.id.your_edit_text_id);

    Button next = (Button) findViewById(R.id.Button01);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), SecondScreen.class);
            startActivityForResult(myIntent, 0);
        }
    });
}
于 2013-04-22T14:33:37.083 回答
0

在 oncreate 方法之外定义您的 switch case 方法,并在 oncreate 的 onclick 方法中使用您的参数调用它。它就像 checkbuttonclick(buttonid);

你的checkbuttonclick(int id)在哪里。将包含 switch case 的方法定义。

于 2013-04-22T14:14:41.073 回答