gpt4 book ai didi

java - 一个 Activity 上的第二个按钮关闭 android 应用程序

转载 作者:行者123 更新时间:2023-11-30 03:38:53 25 4
gpt4 key购买 nike

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

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

最佳答案

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

在 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.Button01R.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);
}
});
}

关于java - 一个 Activity 上的第二个按钮关闭 android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16149056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com