我有一个应用程序,我需要能够从一个活动切换到下一个活动,而且在第一个活动上,有一个温度转换器,它使用一个按钮来计算答案。这个按钮曾经可以工作,但由于我更改了 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