当用户单击片段中的网格视图时,我想显示 ProgressDialog。ProgressDialog 必须显示直到下一个活动加载。我有很多次谷歌,也有很多次,但不能像我的问题一样解决。直到我的工作是,
我的片段类:
@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);
}
}
}
}
});
}
任务.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();
}
}
}
日志猫:
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)
我哪里错了?