我是Android的新手,正在开发真正简单的应用程序。我已在String.xml
中声明了一些字符串数组,并逐个在文本视图中显示这些字符串。用户可以在按下“下一步”和“上一步”按钮时更改字符串。
我面临的问题:
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]);
}
});
}
}
logcat的
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>
答案 0 :(得分:1)
答案 1 :(得分:1)
试试这个......
choosenext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
if (counter >= allquotes.length || counter < 0) {
counter = 0;
}
display.setText(allquotes[counter]);
}
});
chooseprevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
if (counter < 0 || counter >= allquotes.length) {
counter = 0;
}
display.setText(allquotes[counter]);
}
});
答案 2 :(得分:0)
启动应用时,您不会显示第一个字符串。您可以在某处添加display.setText(allquotes[0]);
。
添加支票if (counter < allquotes.length) display.setText(allquotes[counter]);
答案 3 :(得分:0)
嗯,崩溃是一个IndexOutOfBounds问题。 onClock增加了反击。如果计数器&gt; = allquotes.length,应用程序将访问数组中的索引并崩溃。将onClick更改为
if(counter < allquotes.length-1){
counter++;
}
display.setText(allquotes[counter]);
答案 4 :(得分:0)
对于完成所有字符串并按下下一个按钮后,应用程序崩溃,
计数器保持递增并到达String数组中该位置没有值的点。
启动应用并直接按下上一个按钮会发生什么?然后计数器变为(-1)。需要根据数组的大小处理计数器的最小值和最大值。
答案 5 :(得分:0)
当计数器值大于数组大小时,尝试访问allquotes [Counter]时,应用程序因数组超出范围而崩溃。
限制计数器或在达到阵列大小时将其重置为零将解决您的问题。