deflate view and inflate another

2019-06-11 23:07发布

问题:

on the press of my "next" button I have a speech bubble run through a string array. After all the items finish displaying and the user clicks the "next" button once more I would like to deflate the current child view and inflate a new view. Right now it crashes after the string array finishes displaying on multiple clickings of the "next" button. How can I get this to work?

    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:

Ah, yes, I see it now. You can't do this:

 myString[-1]

The index -1 does not exist in arrays. What are you trying to do? Perhaps myString[count - 1]?