我是android新手,正在開發真正簡單的應用程序。我已經在String.xml
中聲明瞭一些字符串數組,並在文本視圖中逐個顯示這些字符串。用戶可以按「Next」和「Previous」按鈕更改字符串。當應用程序被啓動 Android應用程序崩潰問題
- 第一個字符串:
問題我面對的問題。
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>
請發表您的XML – user2450263
發佈logcat的輸出 – Srikanth
@GopalRao - 對於爲什麼不顯示,以防顏色,背景是相同的(?) – user2450263