【发布时间】: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