【问题标题】:ProgressDialog returns null view when getActivity() is used使用 getActivity() 时 ProgressDialog 返回空视图
【发布时间】:2014-07-21 09:21:51
【问题描述】:

我正在开发应用程序,当从套接字接收数据时,我使用 ProgressDialog 显示加载屏幕。这是我的片段代码

public class MainFragment extends Fragment {
 public View onCreateView (LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        Button button = (Button) view.findViewById(R.id.button);

button.setOnLongClickListener(new View.OnLongClickListener() {
public void onClick(View view) {
// do some stuff
}
} );

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
progressDialog = ProgressDialog.show(getActivity(), "", "Loading..",true);
new Thread() {

                    public void run() {
                    try{
// establish socket connection and get data
                        }

                    } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                    }
                progressDialog.dismiss();
                    }
                }.start();

} );
return view
}
}

现在的问题是关于 getActivity()。执行应用程序一次时,会建立正确的视图,但在连续运行时,视图为空,因为 getActivity 没有正确实例化或设置。

我尝试使用 onAttach() 保存活动,然后将其传递给 ProgressDialog,如下所示

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

但返回的视图仍然为空。谁能帮助我如何实现这一目标?谢谢!

【问题讨论】:

  • 试试:ProgressDialog.show(view.getContext(), "", "Loading...", true);
  • 谢谢,我也试过了。它第一次显示视图,然后连续运行,它根本不会放大视图

标签: android android-activity progressdialog


【解决方案1】:

您正在尝试在线程中执行一些 UI 操作。这是禁止的,除非您为线程设置 Looper(我认为这不专业)。

创建ASyncTask,您可以在其中轻松定义后台进程。我为你做了一个小例子,请欣赏:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
            new SomethingInBackground().execute();
        } 
    });




private class SomethingInBackground extends AsyncTask<Void, Void, Boolean>
{
    private ProgressDialog pd;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pd = ProgressDialog.show(YourActivity.this, "", "Loading..",true);
        // it's your UI thread, you can use your activity things here
    }


    @Override
    protected Boolean doInBackground(Void... params) {

        // background process, no UI operations allowed

        // establish socket connection and get data

        // return true if success, false if error (or use some strings instead of boolean)

        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        // your UI thread, draw on screen

        pd.dismiss();

        if (result)
            Log.i("tag", "success!");
        else
            Log.e("tag", "error");
    }
}

【讨论】:

  • 我确实实例化并尝试了异步任务,我的套接字在后台运行。由于我的按钮有 2 个选项,即正常单击和长按,因此活动有些混乱。当我将 YourActivity.this 或任何与上下文或 getActivity 相关的内容时,它们不会被正确地拾取。当应用程序第一次运行时,它运行良好,但随后的视图不会膨胀。
  • 请查看this
【解决方案2】:

在初始化这个MainFragment时尝试传递context(或活动):

private YourAvtivity context;

public MainFragment(YourAvtivity c){
    this.context = c;
}

......

progressDialog = ProgressDialog.show(context, "", "Loading..",true);

【讨论】:

  • 您能说得更具体些吗?谢谢你:)
  • 我的意思是您在 MainFragment 中创建一个构造函数,在初始化 MainFragment 时,传递您初始化 MainFragment 的活动。
  • 你不认为存储活动不是一个好的选择吗?
【解决方案3】:
  1. 首先我没有显示你的拨号声明像private ProgressDialog progressDialog;
  2. 使用这个语法

    progressDialog = ProgressDialog.show(getActivity(),"title","massage", true);
    progressDialog.show();
    
  3. 您不需要在 onAttach() 方法中传递上下文。 getActivity 完成任务

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2013-12-29
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多