调用AsyncTask类时的ProgressDialog NullPointerException

时间:2016-02-25 01:38:07

标签: java android android-asynctask nullpointerexception progressdialog

当我尝试调用NullPointerException类的execute方法时,我遇到了AsyncTask

这是我的代码:

public class NewCustomersRetrieve extends AsyncTask<String, String, String> {
        ProgressDialog progressDialog;
        @Override
        protected void onPreExecute(){
            progressDialog = new ProgressDialog(NewCustomers.this);
            progressDialog.setTitle("Getting list");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.show();
        }
}   

在我的RecyclerView类中,这是我调用NewCustomersRetrieve

的地方
 @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.btnDownloadFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewCustomers newCustomers = new NewCustomers();
                NewCustomers.NewCustomersRetrieve NewCustomersRetrieve = newCustomers.new NewCustomersRetrieve();
                newCustomersRetrieve.execute();
            }
        });

我怀疑它与我其他课程中的onBindViewHolder()方法有关。我猜这里有人可以解释我哪里出错了

这是print stack

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:146)
            at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
            at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
            at android.app.AlertDialog.<init>(AlertDialog.java:98)
            at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
            at lukasz.musik.NewCustomers$NewCustomersRetrieve.onPreExecute(NewCustomers.java:254)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
            at android.os.AsyncTask.execute(AsyncTask.java:534)
            at lukasz.musik.CustomMusicAdapter$1.onClick(CustomMusicAdapter.java:67)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

嗯,你正走在正确的轨道上,因为你可以看到这是违规行:

        progressDialog = new ProgressDialog(NewCustomers.this);

ProgressDialog期待Context作为参数。

您初始化一个新的NewCustomers,然后在其上创建新的NewCustomersRetrieve,但它似乎缺少对您的应用上下文的引用?

根据你和Daniel Nugent的评论,你可能想看一下 Starting Another Activity

您应该使用

Intent intent = new Intent(CustomMusicAdapter.this, NewCustomers.class);
startActivity(intent);

然后在NewCustomers Activity内处理你的NewCustomersRetrieve逻辑。

答案 1 :(得分:1)

我认为你应该把你的asynctask放到你的适配器类中,或者将适配器中的上下文传递给它构造函数!

答案 2 :(得分:0)

执行后,如下所示关闭进度对话框

 @Override
    protected void onPostExecute(Integer results) {
        super.onPostExecute(results);

        if(pd != null && pd.isShowing()){
            pd.dismiss();
            pd=null;
        }

}