2011-11-27 113 views
0

按下我的「下一步」按鈕我有一個語音氣泡運行通過一個字符串數組。所有項目完成顯示後,用戶再次單擊「下一步」按鈕,我想縮小當前的子視圖並增加新的視圖。現在,當字符串數組在「下一個」按鈕的多次點擊完成顯示後,它會崩潰。 我怎樣才能使這個工作?放氣視圖並膨脹另一個

package com.jibushi; 

    import android.app.Activity; 
    import android.content.res.Resources; 
    import android.os.Bundle; 
    import android.os.Handler; 
    import android.os.Message; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class LessonsShell extends Activity{ 
    private static final int MESSAGE_SHOW_POPUP = 1; 
    private static final int MESSAGE_SHOW_POPUP2 = 1; 
    private static final long TIME_DELAY = 1000;//1 seconds 
    private static final long TIME_DELAY2 = 500; 
    private View view; 
    private View view2; 

    private int count = 0; 
    private TextView lessonsDialog; 
    private String[] myIntroString; 

    private Handler handler = new Handler() { 
     public void handleMessage(Message msg) { 
      switch(msg.what) { 
      case MESSAGE_SHOW_POPUP: 
       view(); 
       break; 
      } 
     }; 
    }; 

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

    setContentView(R.layout.lessons); 
    //this will send a message to the handler to display the popup after 1 seconds. 
    handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY); 

    } 

    private void view() { 
    // TODO Auto-generated method stub 
    ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg); 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_dialog, null); 
    parent.addView(view); 

    lessonsDialog = (TextView) findViewById(R.id.lessonsDialog); 

    Resources res = getResources(); 
    myIntroString = res.getStringArray(R.array.lessons_dialog_array); 

    Button nextButton = (Button) findViewById(R.id.next_button); 
    nextButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      if (count < myIntroString.length) { 
       lessonsDialog.setText(myIntroString[count]); 
       count++; 
      } else { 
       if (myIntroString[-1] != null) { 
        handler2.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP2, TIME_DELAY2); 
       } 
      } 
     } 
    }); 

    } 

    private Handler handler2 = new Handler() { 
      public void handleMessage(Message msg) { 
       switch(msg.what) { 
       case MESSAGE_SHOW_POPUP2: 
        view2(); 
        break; 
       } 
      } 

     private void view2() { 
      // TODO Auto-generated method stub 
      ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg); 
      view2 = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_start, null); 
      parent.addView(view2); 
      parent.removeView(view); 
     }; 
     }; 
    } 
+0

日誌上的錯誤是什麼? – dmon

+0

11-27 11:35:14.999:WARN/dalvikvm(315):threadid = 3:線程以未捕獲的異常退出(group = 0x4001b188) 11-27 11:35:15.008:ERROR/AndroidRuntime(315) :由於未捕獲異常導致主線程退出 11-27 11:35:15.029:ERROR/AndroidRuntime(315):java.lang.ArrayIndexOutOfBoundsException 11-27 11:35:15.029:ERROR/AndroidRuntime(315):at com。 jibushi.LessonsShell $ 3.onClick(LessonsShell.java:67) 11-27 11:35:15.029:ERROR/AndroidRuntime(315):at android.view.View.performClick(View.java:2364) – kishfoo

+0

Here is full活動。謝謝! – kishfoo

回答

2

啊,是的,我現在看到它。你不能這樣做:

myString[-1] 

索引-1在數組中不存在。你想做什麼?也許myString[count - 1]

+0

嗚呼!有效!謝謝! – kishfoo

+1

你應該接受答案,如果它的工作:) – dmon

+0

對不起,新的這一點。 – kishfoo