0

当用户单击片段中的网格视图时,我想显示 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)

我哪里错了?

4

2 回答 2

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();
        }
    }
    }

在您初始化progressdialoge 条件检查之前,它们会给您空指针异常。

于 2012-11-16T06:28:14.977 回答
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 操作。

于 2012-11-16T08:42:27.650 回答