我想在用户单击Fragment中的gridview时显示ProgressDialog。 ProgressDialog必须显示,直到下一个活动加载。我有很多次谷歌也在SO,但不能像我的问题解决方案。直到我的工作是,
My Fragment课程:
@TargetApi(11)
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
gridView.setAdapter(new ImageAdapterTwo(Constant.getContext(),countries1));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
System.out.print("gridView.getId();=="+gridView.getId()) ;
if(v==null)
{
System.out.print("gridView.getId();=="+gridView.getId()) ;
}
else
{
TextView t=(TextView) v.findViewById(R.id.grid_item_text);
if(t==null)
{
System.out.print("gridView.getId();==") ;
}
else
{
if(t.getText()=="")
{
new Task().execute();
}
else if(t.getText()=="")
{
Intent i2 = new Intent(Constant.getContext(), ServerBackup.class);
startActivity(i2);
}
else if(t.getText()=="")
{
Intent i2 = new Intent(Constant.getContext(), ServerBackup.class);
startActivity(i2);
}
else
{
Intent i2 = new Intent(Constant.getContext(), ViewPagerFragmentActivity.class);
startActivity(i2);
}
}
}
}
});
}
Task.java:
package com.net.elderlyhealth.weather;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import com.net.elderlyhealth.weather.AsyncClass;
public class Task extends AsyncTask<Void, Void, String>
{
Context context;
AsyncClass asyncclass=new AsyncClass();
//context=Constant.getContext();
public Task(Context context)
{
//this.context=context;
//Log.d("context",context.toString());
}
@Override
protected void onPreExecute()
{
asyncclass.showLoadingProgressDialog();
}
@Override
protected String doInBackground(Void... params)
{
Intent intent=new Intent(context,ActivityWeatherSetting.class);
context.startActivity(intent);
return null;
}
@Override
protected void onPostExecute(String params)
{
asyncclass.dismissProgressDialog();
}
}
AsyncClass.java:
import android.app.Activity;
import android.app.ProgressDialog;
public class AsyncClass extends Activity
{
protected static final String TAG = AsyncClass.class.getSimpleName();
public ProgressDialog progressDialog;
private boolean destroyed = false;
@Override
protected void onDestroy()
{
super.onDestroy();
destroyed = true;
/*if ( progressDialog!=null && progressDialog.isShowing() ){
progressDialog.cancel();
}*/
}
public void showLoadingProgressDialog()
{
this.showProgressDialog("Loading. Please wait...");
}
public void showProgressDialog(CharSequence message)
{
if (progressDialog == null)
{
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(true);
}
progressDialog.setMessage(message);
progressDialog.show();
}
public void dismissProgressDialog()
{
if (progressDialog != null && !destroyed)
{
progressDialog.dismiss();
}
}
}
logcat的:
11-16 14:03:31.744: E/AndroidRuntime(2246): FATAL EXCEPTION: main
11-16 14:03:31.744: E/AndroidRuntime(2246): java.lang.NullPointerException
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.app.AlertDialog.<init>(AlertDialog.java:98)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
11-16 14:03:31.744: E/AndroidRuntime(2246): at com.net.elderlyhealth.weather.AsyncClass.showProgressDialog(AsyncClass.java:33)
11-16 14:03:31.744: E/AndroidRuntime(2246): at com.net.elderlyhealth.weather.AsyncClass.showLoadingProgressDialog(AsyncClass.java:26)
11-16 14:03:31.744: E/AndroidRuntime(2246): at com.net.elderlyhealth.weather.Task.onPreExecute(Task.java:24)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.os.AsyncTask.execute(AsyncTask.java:511)
11-16 14:03:31.744: E/AndroidRuntime(2246): at com.net.elderlyhealth.view.SampleGridTwoFragment$1.onItemClick(SampleGridTwoFragment.java:126)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
11-16 14:03:31.744: E/AndroidRuntime(2246): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
我哪里出错了?
答案 0 :(得分:0)
public void showProgressDialog(CharSequence message)
{
progressDialog = new ProgressDialog(this);
if (progressDialog == null)
{
progressDialog.setIndeterminate(true);
}
progressDialog.setMessage(message);
progressDialog.show();
}
public void dismissProgressDialog()
{
if (progressDialog != null && !destroyed)
{
progressDialog.dismiss();
}
}
}
在初始化进度对话框条件之前,检查显示它们给出了空指针异常。
答案 1 :(得分:0)
异步任务的修改代码
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import com.net.elderlyhealth.weather.AsyncClass;
public class Task extends AsyncTask<Void, Void, String>
{
Activity currentActivity;
ProgressDialog dialog;
public Task(Activity activity)
{
currentActivity = activity;
}
@Override
protected void onPreExecute()
{
dialog=ProgressDialog.show(currentActivity, "Title", "Msg");
}
@Override
protected String doInBackground(Void... params)
{
//do your work
}
@Override
protected void onPostExecute(String params)
{
dialog.dismiss();
}
}
调用异步任务
new Task(getActivity()).execute()
这应该可以解决您的问题。 请记住,您不能仅仅实例化一个扩展活动并期望它执行UI操作的类。