当我执行ProgressDialog.show()方法时,AsyncTask会杀死我的应用程序

时间:2015-06-08 11:07:31

标签: android android-fragments android-asynctask

标题清楚。当我启动AsyncTask我的应用程序崩溃。究竟问题在于ProgressDialog的show方法。

我从Fragment(onCreate方法)启动我的AsncTask。

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    context = getActivity().getBaseContext();

                LoadLastNewAsync llna = new LoadLastNewAsync(context, lang);

                try {
                    tituloAviso = llna.execute().get();

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
}

这是mi AsyncTask代码:

public class LoadLastNewAsync extends AsyncTask <Void, Void, String>{
    Context context;
    private DataBaseHelper myDbHelper;
    private String tituloAviso;
    private Aviso aviso;
    private String lang;
        private ProgressDialog pDialog; 


    //constructor
     public LoadLastNewAsync(Context context, String lang) {
            //mActivity = activity;
         this.context = context;
         this.lang = lang;
        }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);//erabiltzaileak atzera botoia sakatuz ez kantzelatzeko
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//barrarewn estiloa. Espiral bat izango da
        pDialog.show();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);


    }

    @Override
    protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub
        Log.i("Testing", "LoadLastNewAsync. Entra en doInBackGround");
        myDbHelper = new DataBaseHelper(context);
        boolean dbExist = myDbHelper.checkDataBase();
        if(dbExist){
            aviso = new Aviso();
            aviso = recuperarUltimoAviso();
            tituloAviso = aviso.getTitle();
        }

        return tituloAviso; 

    }

    private Aviso recuperarUltimoAviso() {
        // TODO Auto-generated method stub
        try {
            myDbHelper.openDataBase();
            aviso = myDbHelper.getUltimoAviso(this.lang);
        } catch (Exception e) {
            // TODO: handle exception
        }
        myDbHelper.close();
        return aviso;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pDialog.dismiss();
    }
}

我收到此日志:

FATAL EXCEPTION: main
Process: com.kirolm.instalacionesdep, PID: 29441
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kirolm.instalacionesdep/com.kirolm.instalacionesdep.Main}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

at android.view.ViewRootImpl.setView(ViewRootImpl.java:536)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at com.kirolm.instalacionesdep.asynctask.LoadLastNewAsync.onPreExecute(LoadLastNewAsync.java:41)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.kirolm.instalacionesdep.HomeFragment.onCreate(HomeFragment.java:71)
at android.app.Fragment.performCreate(Fragment.java:1678)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)

我不知道问题出在哪里。它可能在上下文中?问题出现在片段循环中吗?

4 个答案:

答案 0 :(得分:1)

ProgressDialog需要显示它的活动上下文。尝试将AsyncTask声明替换为

 LoadLastNewAsync llna = new LoadLastNewAsync(getActivity(), lang);

答案 1 :(得分:0)

您无法通过非活动的上下文显示应用程序窗口/对话框。尝试传递有效的活动参考。

答案 2 :(得分:0)

使用当前活动上下文而不是基本上下文 MyActivity.this 作为上下文,然后再将其传递给构造函数

也可以在onPost of async task中使用进度对话框

try{
    if (pWindow != null) {
       pWindow.dismiss();
    } 
} catch (IllegalStateException ise) {
        ise.printStackTrace();
}

答案 3 :(得分:0)

您的onPreExecute方法存在问题。试试这个,它对我有用。 替换

pDialog = new ProgressDialog(context);

pDialog = new ProgressDialog(getDialogContext());

将getDialogContext()方法定义为:

private Context getDialogContext()
{
    Context context;
    if (getParent() != null)
        context = getParent();
    else
        context = this;
    return context;
}