0
這是一個非常簡單的程序,它調用按鈕單擊上的asynctask,這會更改textview。Android:在onProgressUpdate函數中的AsyncTask中的CalledFromWrongThreadException
但在按鈕上單擊它會給出一個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");
}
}
也可以附上你的例外與只在必要的代碼段沿柱子。 – Daenarys