gpt4 book ai didi

android - 卡在执行按钮

转载 作者:行者123 更新时间:2023-11-29 18:15:13 28 4
gpt4 key购买 nike

我是 java 的新手(主要是与嵌入式决斗)

我正在编写一个简单的 android 程序,通过单击按钮将 editText 输入的值传递给另一个 editText。但是,每次我按下按钮时,我的程序都会崩溃。想要任何人评论:)

这是我的主要内容:

package Mushroom.Pickers.Note;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
//import android.widget.Button;
//import android.widget.TableLayout;

public class Mushy extends Activity {
//private TableLayout layout;
//private Button button;

/** Called when the activity is first created. */

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

public void Compute_data(View view) {//Function to perform on click
//Perform calculation
int Hours;
EditText start_time = (EditText)findViewById(R.id.editText26) ;
Hours = Integer.parseInt(start_time.getText().toString());

EditText test_time = (EditText)findViewById(R.id.editText43);
test_time.setText(Hours);

}
}

在我的 xml 中:

<Button
android:id="@+id/Button1"
android:layout_width="134px"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textSize="15sp"
android:text="Calculate"
android:onClick="Compute_data"
android:textColor="#1e90ff" />
</TableRow>
<EditText
android:id="@+id/editText26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:inputType="number"
android:width="350px" />

<EditText
android:id="@+id/editText43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:inputType="number"
android:width="350px" />

布局看起来很好,只是点击按钮时崩溃的问题:(

P/S:这是日志:

12-19 03:03:02.106: W/ResourceType(667): No package identifier when getting value for resource number 0x00000004
12-19 03:03:02.106: D/AndroidRuntime(667): Shutting down VM
12-19 03:03:02.106: W/dalvikvm(667): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-19 03:03:02.275: E/AndroidRuntime(667): FATAL EXCEPTION: main
12-19 03:03:02.275: E/AndroidRuntime(667): java.lang.IllegalStateException: Could not execute method of the activity
12-19 03:03:02.275: E/AndroidRuntime(667): at android.view.View$1.onClick(View.java:3039)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.view.View.performClick(View.java:3480)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.view.View$PerformClick.run(View.java:13983)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.os.Handler.handleCallback(Handler.java:605)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.os.Looper.loop(Looper.java:137)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-19 03:03:02.275: E/AndroidRuntime(667): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 03:03:02.275: E/AndroidRuntime(667): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 03:03:02.275: E/AndroidRuntime(667): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 03:03:02.275: E/AndroidRuntime(667): at dalvik.system.NativeStart.main(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667): Caused by: java.lang.reflect.InvocationTargetException
12-19 03:03:02.275: E/AndroidRuntime(667): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.view.View$1.onClick(View.java:3034)
12-19 03:03:02.275: E/AndroidRuntime(667): ... 11 more
12-19 03:03:02.275: E/AndroidRuntime(667): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x4
12-19 03:03:02.275: E/AndroidRuntime(667): at android.content.res.Resources.getText(Resources.java:247)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.widget.TextView.setText(TextView.java:3428)
12-19 03:03:02.275: E/AndroidRuntime(667): at Mushroom.Pickers.Note.Mushy.Compute_data(Mushy.java:29)
12-19 03:03:02.275: E/AndroidRuntime(667): ... 14 more
12-19 03:03:02.475: D/dalvikvm(667): GC_CONCURRENT freed 230K, 4% free 10426K/10759K, paused 6ms+14ms

最佳答案

问题:

test_time.setText(value) 覆盖了 intString 参数的方法。

  • int 参数表示您将传递String 资源id。 (IER.string.any_string)
  • String参数表示你将传递String

您将 Hours 作为 int 而不是 String 传递。

解决方案:

您会将 int 转换为 String 以设置为任何 View 的文本(即 ButtonTextViewEditText)修改您的代码如下:

public void Compute_data(View view) {
//....
//here is rest of your code
EditText test_time = (EditText)findViewById(R.id.editText43);
test_time.setText(String.valueOf(Hours));//<<<------consider this change
}

关于android - 卡在执行按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8556957/

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