简体   繁体   中英

ProgressDialog from other Activity

i have 2 Activitys. In the first Updater Activity i want to create a ProgressDialog, which I want to show in the other Activity(TopRatedFragment). How can i do this?

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

This method is called after an 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();
        }

If i click on the Button, a lot of errors are shown. The first is the FATAL EXCEPTION: main and a java.lang.NullPointerException.

How can I fix that ?

If you look into the logcat, you will find the full exception backtrace. It tells you the line, where the NullPointerException was thrown. Please check, if that button really exists. Maybe, it is caused by a wrong ID or something else.

If you have a very loooong backtrace, read through the trace and look for " Caused by.... " lines. It often happened to me, that the exception was encapsulated and wasn't obvious shown in the logcat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM