0
我有2個活動。在第一個更新器活動中,我想創建一個ProgressDialog, ,我想在其他Activity(TopRatedFragment)中顯示。我怎樣才能做到這一點?來自其他活動的ProgressDialog
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。
我該如何解決這個問題?
您的TopRatedFragment不是「活動」。你的'Updater'是一個'Activity',但不管理你的'TopRatedFragment'。爲什麼,如何以及在哪裏發生錯誤尚不清楚。在發佈很多不相關的代碼之前,你應該先閱讀谷歌對[Fragments](http://developer.android.com/guide/components/fragments.html)和[Activities](http://developer.android的.com /參考/機器人/應用/ Activity.html)。 – Drew
首先,你只有一個活動,另一個是碎片,其次你可以詳細說明你想要在代碼中做什麼。 –
一切工作正常的JSON代碼是好的。但是我無法從其他Activity獲取ProgressDialog。我怎樣才能做到這一點 ? –