gpt4 book ai didi

java - 尝试进行其他 Activity 时,我简单的 “counter app”崩溃了……为什么?

转载 作者:行者123 更新时间:2023-12-03 17:45:39 25 4
gpt4 key购买 nike

尝试进行其他 Activity 时,应用程序崩溃了...

日志猫

java.lang.RuntimeException: Unable to start activity ComponentInfo{myact.julianhernandez.com.counterapp/myact.julianhernandez.com.counterapp.CounterDisplay}: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:244)
at android.widget.TextView.setText(TextView.java:3888)
at myact.julianhernandez.com.counterapp.CounterDisplay.onCreate(CounterDisplay.java:31)
at android.app.Activity.performCreate(Activity.java:5231)

MainActivity.class代码
    public class MainActivity extends AppCompatActivity {

Button moveActivity;

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

moveActivity = (Button) findViewById(R.id.list1_button);

moveActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent toCounter = new Intent(MainActivity.this , CounterDisplay.class);
startActivity(toCounter);
}
});

CounterDisplay.class代码
public class CounterDisplay extends AppCompatActivity {

private TextView countView;
private Button addButton;
private Button subtractButton;
//int initialCount = 0;



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

countView = (TextView) findViewById(R.id.currentCount);
addButton = (Button) findViewById(R.id.add);
subtractButton = (Button) findViewById(R.id.subtract);



countView.setText(0);

//increment counter by 1
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int currentCount;// = initialCount;
for(currentCount=0;currentCount>=0;currentCount++){
countView.setText(currentCount);
}
}
});
}
}

为什么切换 Activity 时此代码崩溃?

最佳答案

Textview.setText(int)方法采用资源ID,而不是您尝试显示的文本。因为没有匹配您传递的ID的资源字符串(在第31行为0),所以将引发异常。

setText()方法还有其他重载,如果您不想使用资源字符串,则可以传递char []。查看文档以获取更多信息。 http://developer.android.com/reference/android/widget/TextView.html

关于java - 尝试进行其他 Activity 时,我简单的 “counter app”崩溃了……为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34235262/

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