似乎displayView变量与void不兼容,我尝试将导航对话框添加到导航抽屉中,每次选中一个元素
public class AsyncClass extends AsyncTask<Void, String, Void> {
private Context context;
ProgressDialog dialog = new ProgressDialog(context);
public AsyncClass(Context cxt) {
context = cxt;
}
@Override
protected void onPreExecute() {
dialog.setTitle("Please wait");
dialog.show();
}
@Override
protected Void doInBackground(Void... unused) {
public void displayView(int position) { /*the message here is :void is an invalid type for the variable displayView*/
Fragment fragment = null;
switch (position) {
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new CalendarioFragment();
break;
case 5:
fragment = new ContactoFragment();
break;
case 6:
fragment = new OnclickFragment();
break;
default:
break;
}
if (fragment != null) {
try{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and tit|le, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
catch(Exception E)
{
Log.e("MainActivity", "Error in creating fragment");
}
}
else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
SystemClock.sleep(2000);
return (null);
}
@Override
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
答案 0 :(得分:0)
首先,Java
不支持您在doInBackground()
中执行的嵌套方法。这是给定错误的原因。详细检查here。
另一件事是,doInBackground()
将在后台线程上运行,因此不要从那里更新UI。 AsyncTask
仅用于长时间运行的操作。您可以考虑将doInBackground()
代码移至相关的Activity
或Fragment
。