0

I want to add the random number to put on textview in Dialogfragment that gets from Timer Activity;

i try to change many times

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt_v=(TextView)findViewById(R.id.hello);
    txtv_2=(TextView)findViewById(R.id.textV_2);
    btn_str=(Button)findViewById(R.id.btn_give_str);

    dia=new DiagFragment();

    nBtb=(Button)findViewById(R.id.bt);
    nBtb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            dia.show(MainActivity.this.getSupportFragmentManager(), null);
        }
    });


    timer.schedule(task, 1000, 5000);


}

TimerTask task = new TimerTask() {
    public void run() {
        if (dia!=null){

            ((DiagFragment) dia).setData2(String.valueOf((Math.random() * 10)));

        }
    }
};


public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.list_dialog,null);
    mContext=getActivity();

    initView(view);

    return view;
}


public void initView(View view){
    tx1=(TextView)view.findViewById(R.id.tv_1);
     tx2=(TextView)view.findViewById(R.id.txv_2);

}



public void setData2(String string) {

    str2=string;
    Log.w(TAG, "setData2: "+string );
    tx2.setText(string);

}

I can get data on diafragment, but I get an error when I add data to the TextVIew (tx2)

the error message is :

   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.soundgil.dailogonclicklistend.DiagFragment.setData2(DiagFragment.java:68)
        at com.soundgil.dailogonclicklistend.MainActivity$2.run(MainActivity.java:54)
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65

2 Answers2

0

It seems that tx2 was not found correctly so it null. Try checking the layout xml (is your view id correct?) or put it up.

By the way, TimerTask.run() was run in a sub thread so you can not update the UI. UI must be operated in main thread. Try to use a handler like this:

Handler handler = new Handler(){
    public void handleMessage(Message message){
        if (message.what == 0 && dia!=null){
             ((DiagFragment) dia).setData2(String.valueOf((Math.random() * 10)));
        }
    }
};

TimerTask task = new TimerTask() {
    public void run() {
        handler.sendEmptyMessage(0);
    }
};
Chenhe
  • 924
  • 8
  • 19
0

When Dialogfragment is not shown, tx2 is not initialized, invoke setText() on it will cause error. Please try checking null for tx2 in setData2().

public void setData2(String string) {
    str2 = string;
    Log.w(TAG, "setData2: "+string );
    if (tx2 != null) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tx2.setText(str2);     
            }
        });
    }
}
TylerQITX
  • 318
  • 2
  • 9