这是一个非常简单的程序,它在按钮点击时调用asynctask,它会改变文本视图。
但是点击按钮会产生CalledFromWrongException。
public class FragmentC extends Fragment {
public FragmentC() {
}
public View contentViewC;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contentViewC = inflater.inflate(R.layout.fragment_fragment_b, container, false);
Button button = (Button) contentViewC.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (MainActivity.filepath != "Nothing") {
Frequency getFreq=new Frequency();
getFreq.execute();
}
}
});
return contentViewC;
}
private class Frequency extends AsyncTask<Void, Float, Void> {
@Override
protected Void doInBackground(Void... params) {
onProgressUpdate();
return null;
}
@Override
protected void onProgressUpdate(Float... values) {
textToChange.setText("Demo");
}
}
答案 0 :(得分:1)
请勿直接致电onProgressUpdate()
您只需从publishProgress()
拨打doInBackground()
,然后在用户界面线程中AsynchTask
拨打onProgressUpdate()
。
In Android only in main thread we update the UI
有关详情,请查看此onProgressUpdate() documnet