我自己写了一个方法来传递参数的,目的是要修改控件的值
activity 代码部分
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)));
}
}
};
Dialogfragment 部分
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);
}
我能打印到定时器传过来的数据
但只要我加上tx2.setText(string)就报错