0

ポップアップ ウィンドウのテキスト ビューのテキストを変更する際に問題が発生しました。

シンプル バージョンのコードは次のようになります。

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


            }
    });

}

カウントダウンの目盛りごとに TextView を変更しようとしています。私の問題は、CountDownTimer 中に TextView を変更できないことです。これにより、NullPointer 例外が発生します。tv_popupTextViewをいつ定義して初期化するかについてはよくわかりません。

誰でも助けることができますか?

ありがとう!

4

2 に答える 2

2

はっきりとわかるように、レイアウト付きのポップウィンドウでテキストビューを使用しています

popupViewしたがって、IDを定義するときにオブジェクトを追加する必要があります

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

これを試して

于 2016-02-26T18:43:01.590 に答える
0

すべてのビューを popup オブジェクトで初期化する必要があるため、コードは次のようになります

tv_popup = (TextView) popupView.findViewById(R.id.tv_popup_text);
于 2016-02-26T18:46:38.303 に答える