2014-02-15 78 views
0

我是android新手,正在開發真正簡單的應用程序。我已經在String.xml中聲明瞭一些字符串數組,並在文本視圖中逐個顯示這些字符串。用戶可以按「Next」和「Previous」按鈕更改字符串。當應用程序被啓動 Android應用程序崩潰問題

  • 當所有的字符串都結束了再下按鈕被按下時,應用程序崩潰是沒有得到顯示

    1. 第一個字符串:

      問題我面對的問題。

    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

    請發表您的XML – user2450263

    +0

    發佈logcat的輸出 – Srikanth

    +0

    @GopalRao - 對於爲什麼不顯示,以防顏色,背景是相同的(?) – user2450263

    回答

    1
    1. 設置文本之前,增加計數器,所以allcounter指數被認爲是1 instea d爲0.
    2. 沒有錯誤日誌很難說出什麼問題。使用logcat。
    +0

    你能告訴,假設有10個字符串顯示,然後第10個字符串用戶按下,那麼如何顯示應用的第一個字符串? –

    +0

    是這就是容易, choosenext.setOnClickListener(新View.OnClickListener(){ @Override 公共無效的onClick(視圖v){// TODO自動生成方法存根 計數器++; 如果(計數器== 10){ counter; 0;} display.setText(allquotes [counter]); } });如假設有 – timeshift117

    0
    1. 當應用程序啓動時,不顯示第一個字符串。你可以在某處添加display.setText(allquotes[0]);

    2. 添加檢查if (counter < allquotes.length) display.setText(allquotes[counter]);

    0

    好,崩潰是IndexOutOfBounds問題。 onClock增加計數器。如果counter> = allquotes.length,應用程序將訪問數組中的索引並崩潰。更改的onClick到

      if(counter < allquotes.length-1){ 
           counter++; 
          } 
          display.setText(allquotes[counter]); 
    
    0

    因爲當所有的字符串完成後,仍然下一步按鈕被按下時,應用程序崩潰,

    計數器不斷遞增並達到一個點那裏是String數組中的位置沒有價值。

    當您啓動應用程序並直接按下上一個按鈕時會發生什麼?然後計數器變成(-1)。需要根據陣列的大小處理計數器的最小值和最大值。

    0

    應用程序因數組越界而崩潰,因爲當計數器值大於數組大小時,您嘗試訪問allquotes [Counter]。

    限制計數器或在達到陣列大小時將其重置爲零將解決您的問題。

    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]); 
    
          } 
         }); 
    
    +0

    ,有10個字符串顯示,然後在第10個字符串用戶按下,如何顯示應用程序的第一個字符串? –

    +0

    @bhanukaushik查看編輯答案... –