来自其他Activity的ProgressDialog

时间:2014-03-28 14:57:35

标签: java android android-fragments progressdialog

我有2个活动。在第一个Updater Activity中,我想创建一个ProgressDialog, 我希望在其他Activity(TopRatedFragment)中显示。我怎么能这样做?

public class Updater extends Activity {

    String pid = "1";
    JSONObject x;
    int success;
    ProgressDialog pDialog;
    int y;
    private String result;
    String Url = "domain.com";
     JSONArray products = null;
    private static final String TAG_SUCCESS = "success";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checker);

    }

    public int getResult(JSONObject json) {
        try {
            Log.d("Request: ", json.toString());
            // Getting JSON Array
            success = json.getInt(TAG_SUCCESS);
            Log.i("Status 2 z", "Status z: "+ success); 
         } catch (JSONException e) {
             e.printStackTrace();
         }

         return success;
     }

    public final int updaten(String site) {

        Update task = new Update();
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", site));
            x = task.execute(params).get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        y = getResult(x);

        return y;
    }

class Update extends AsyncTask<List<NameValuePair>, String, JSONObject> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(Updater.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected JSONObject doInBackground(List<NameValuePair>... params) {

        // Getting JSON from URL

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(Url, params[0]);
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        Log.d("Updater ", json.toString());

    }
}

public class TopRatedFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
        return rootView;
    }

在OnClickListener之后调用此方法。

public void Updatequest(final String site) {
        ConnectivityManager connMgr = (ConnectivityManager) getActivity()
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            ado = new Updater();
            Log.i("Status 2 z", "Status z: " + z);
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getActivity());

            // set title
            alertDialogBuilder.setTitle("Do you really want to report?");

            // set dialog message
            alertDialogBuilder
                    .setMessage("Press Yes to submit your report")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if this button is clicked, close
                                    // current activity
                                    int z = ado.updaten(site); //call the method in the other Activity
                                    Log.i("Status 1 z", "Status z: " + z);
                                    if (z == 1) {
                                        Toast.makeText(
                                                getActivity(),
                                                "Thanks, your report was successfull",
                                                Toast.LENGTH_LONG).show();
                                        z = 0;
                                    } else {
                                        Toast.makeText(
                                                getActivity(),
                                                "Please check your Internet connection!",
                                                Toast.LENGTH_LONG).show();
                                    }
                                }
                            })
                    .setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }

        else {
            Toast.makeText(getActivity(),
                    "An error has occured please check your Internet Connection again", Toast.LENGTH_LONG)
                    .show();
        }

如果我点击按钮,会显示很多错误。第一个是致命的例外:主要 和java.lang.NullPointerException。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

如果你查看logcat,你会发现完整的异常回溯。它会告诉您NullPointerException被抛出的行。如果该按钮确实存在,请检查。也许,它是由错误的ID或其他原因引起的。

如果您有非常冷静的回溯,请仔细阅读跟踪并查找“Caused by....”行。经常发生在我看来,异常是封装的,并且在logcat中没有显示出来。