gpt4 book ai didi

java - Android应用程序崩溃问题

转载 作者:行者123 更新时间:2023-12-02 06:12:08 24 4
gpt4 key购买 nike

我是 Android 新手,正在开发真正简单的应用程序。我在 String.xml 中声明了一些字符串数组,并在 TextView 上一一显示这些字符串。用户可以在按“下一个”和“上一个”按钮时更改字符串。

我面临的问题:

  1. 启动应用程序时未显示第一个字符串
  2. 当所有字符串完成并按下下一个按钮时,应用程序崩溃。

MainActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final String allquotes[] = getResources()
.getStringArray(R.array.quotes);
counter = 0;
display = (TextView) findViewById(R.id.tvdisplay);
choosenext = (Button) findViewById(R.id.bnext);

choosenext.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText(allquotes[counter]);

}
});

chooseprevious = (Button)findViewById(R.id.bprevious);
chooseprevious.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
display.setText(allquotes[counter]);

}
});


}


}

日志猫

02-15 12:52:37.020: W/KeyCharacterMap(405): No keyboard for id 0
02-15 12:52:37.020: W/KeyCharacterMap(405): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-15 12:52:47.509: D/dalvikvm(405): GC_EXTERNAL_ALLOC freed 54K, 52% free 2586K/5379K, external 2237K/2793K, paused 382ms
02-15 12:52:59.420: D/AndroidRuntime(405): Shutting down VM
02-15 12:52:59.420: W/dalvikvm(405): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-15 12:52:59.431: E/AndroidRuntime(405): FATAL EXCEPTION: main
02-15 12:52:59.431: E/AndroidRuntime(405): java.lang.ArrayIndexOutOfBoundsException
02-15 12:52:59.431: E/AndroidRuntime(405): at com.example.zzzz.MainActivity$1.onClick(MainActivity.java:34)
02-15 12:52:59.431: E/AndroidRuntime(405): at android.view.View.performClick(View.java:2485)
02-15 12:52:59.431: E/AndroidRuntime(405): at android.view.View$PerformClick.run(View.java:9080)
02-15 12:52:59.431: E/AndroidRuntime(405): at android.os.Handler.handleCallback(Handler.java:587)
02-15 12:52:59.431: E/AndroidRuntime(405): at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 12:52:59.431: E/AndroidRuntime(405): at android.os.Looper.loop(Looper.java:123)
02-15 12:52:59.431: E/AndroidRuntime(405): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-15 12:52:59.431: E/AndroidRuntime(405): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 12:52:59.431: E/AndroidRuntime(405): at java.lang.reflect.Method.invoke(Method.java:507)
02-15 12:52:59.431: E/AndroidRuntime(405): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-15 12:52:59.431: E/AndroidRuntime(405): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-15 12:52:59.431: E/AndroidRuntime(405): at dalvik.system.NativeStart.main(Native Method)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:background="@drawable/background"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/tvdisplay"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="bottom"
android:orientation="horizontal" >

<Button
android:id="@+id/bprevious"
android:layout_width="154dp"
android:layout_height="wrap_content"
android:text="@string/previous" />

<Button
android:id="@+id/bnext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/next" />

</LinearLayout>

</LinearLayout>

最佳答案

  1. 在设置文本之前递增计数器,因此引用的 allcounter 索引为 1 而不是 0。
  2. 如果没有错误日志,很难说出问题所在。使用 logcat。

关于java - Android应用程序崩溃问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794614/

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