收缩视图并膨胀另一个

时间:2011-11-27 02:16:38

标签: android android-layout android-widget

按下我的" next"按钮我有一个通过字符串数组的语音气泡。所有项目完成显示后,用户点击" next"再次按钮我想放气当前的子视图并膨胀一个新的视图。现在它在字符串数组完成显示" next"的多次点击后崩溃了。按钮。 我怎样才能让它发挥作用?

    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);
        };
        };
    }

1 个答案:

答案 0 :(得分:2)

啊,是的,我现在看到了。你不能这样做:

 myString[-1]

数组中不存在索引-1。你想做什么?也许是myString[count - 1]