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)