Ingo K.
Ingo K.

Reputation: 79

TextView in PopUpwindow cause NullPointer Exception

got a problem with changing the text of a text view in a PopUp Window.

Simple Version of the code looks like this:

public class Activity extends Activity {


View popupView;
PopupWindow pw_info;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_menue);



    // Layout components
    tv_function = (TextView) findViewById(R.id.function);
    tv_result = (TextView) findViewById(R.id.tv_result);
    tv_total = (TextView) findViewById(R.id.tv_total_result);

    });



@Override
protected void onStart(){
    super.onStart();
    pop_up();
}



// PopUp Window for start and end
private void pop_up(){

    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.popup_window, null);
    final PopupWindow pw_popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);


    popupView.post(new Runnable() {
        public void run() {
            pw_popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
            tv_function.setText("start");
            tv_popup = (TextView) findViewById(R.id.tv_popup_text);


            new CountDownTimer(3000, 1000) {

                public void onTick(long l_millisUntilFinished) {
                    // Problem: accessing tv_popup creates NULLPOINTER Exception!
                    tv_popup.setText(String.valueOf(l_millisUntilFinished / 1000));
                }

                public void onFinish() {
                    pw_popupWindow.dismiss();
                                  }

            }.start();


            }
    });

}

I try to change the TextView with every tick of the countdown. My problem is that im not able to change the TextView during the CountDownTimer. This causes NullPointer Exception. Im not really sure about when to define and initialise the tv_popup TextView.

Anybody able to help?

Thanks!

Upvotes: 0

Views: 231

Answers (2)

Nils
Nils

Reputation: 657

You have to initialize your all view with the popup object, so your code should be like

tv_popup = (TextView) popupView.findViewById(R.id.tv_popup_text);

Upvotes: 0

Quick learner
Quick learner

Reputation: 11467

as clearly Seen you are using textview in pop window with a layout

So You need to add the popupView object when you are definifing its ID

 tv_popup = (TextView) popupView.findViewById(R.id.tv_popup_text);

Try This

Upvotes: 2

Related Questions