【问题标题】:Android strange error reported on the onPreExecute methodonPreExecute方法报Android奇怪错误
【发布时间】:2013-09-20 09:45:55
【问题描述】:

今天我的 G. Analytics 帐户报告我在这行代码中出现错误

private class GetSubscriptionListTask extends AsyncTask<Void, Void, Void> {

    boolean onlyUnread=true;

    public GetSubscriptionListTask(boolean onlyUnread) {
        super();
        this.onlyUnread=onlyUnread;
    }

    ProgressDialog progress;

    @Override
    protected void onPreExecute() {
          //Show progress Dialog here
          super.onPreExecute();

          // create ProgressDialog here ...
          progress = new ProgressDialog(getActivity()); // <-- That's the line
          progress.setMessage("Downloading Subscriptions");
          // set other progressbar attributes

          progress.setCancelable (false);
          progress.setIndeterminate (true);
          progress.show();

    }
}

这是行

progress = new ProgressDialog(getActivity()); // <-- That's the line

这是错误报告

NullPointerException (@SubscriptionsListFragment$GetSubscriptionListTask:onPreExecute:415) {main}

这是什么意思? nullexception 是关于 ProgressDialog 还是 getActivity()?

*更新** 此错误仅在 100 多个会话中发生一次。

【问题讨论】:

  • 你最好在**oncreate**方法中初始化你的进度条,你也可以把getActivity()改成YouCLassName.this
  • 此错误仅在 100 多个会话中发生一次。

标签: java android exception android-asynctask


【解决方案1】:

如果您的课程扩展了FragmentActivity,您可以使用getActivity()。否则,如果您的 Class 扩展了 Activity,请尝试 yourActivty.this

或者你在 Fragment 类调用 onAttach 中获得 Activty 引用

Activity mActivity=null
@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

【讨论】:

  • 随 FragmentActivity 改变
  • 为什么?我有一个扩展 FragmentActivity 的 SubscriptionsListActivity,其中包含扩展 Fragment 的我的 SubscriptionsListFragment。为什么我还必须更改 SubscriptionsListFragment 以扩展 FragmentActivity??
【解决方案2】:

有时,Fragment 会从您的 Activity 中分离出来,并且 getActivity() 返回 null。

你可以在这里看到这个:http://developer.android.com/guide/components/fragments.html

注意:如果您需要在 Fragment 中添加 Context 对象,您可以 调用 getActivity()。但是,请注意仅调用 getActivity() 当片段附加到活动时。当片段不是 尚未连接,或在其生命周期结束时分离, getActivity() 将返回 null。

这就是你在这里获得 NPE 的原因。

【讨论】:

  • 谢谢!很好的解释。现在我明白了。但是我该如何解决这个问题呢?我可以将什么传递给进度对话框而不是 getActivity()?
  • 如果您在创建片段时执行任务,请将其移至 onAttached 方法。否则,你的fragment就不会再显示了,所以你不需要显示你的对话框,只需要检查getActivity()之前是否为null。
  • 所以你认为 if(getActivity()!=null) progress = new ProgressDialog(getActivity());可以解决吗?
  • 是的,但前提是您在创建片段时没有执行任务
【解决方案3】:
// try this way
private class GetSubscriptionListTask extends AsyncTask<Void, Void, Void> {

        boolean onlyUnread=true;
        Context context;

        public GetSubscriptionListTask(boolean onlyUnread,Context context) {
            super();
            this.onlyUnread=onlyUnread;
            this.context=context;
        }

        ProgressDialog progress;

        @Override
        protected void onPreExecute() {
            //Show progress Dialog here
            super.onPreExecute();

            // create ProgressDialog here ...
            progress = new ProgressDialog(context); <-- That's the line
            progress.setMessage("Downloading Subscriptions");
            // set other progressbar attributes

            progress.setCancelable (false);
            progress.setIndeterminate (true);
            progress.show();

        }

【讨论】:

    猜你喜欢
    • 2016-01-11
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2014-04-12
    • 2016-10-29
    • 2017-06-01
    • 1970-01-01
    相关资源
    最近更新 更多