问题
I have an AsyncTask class within another class in a separate file from MainActivity
GetWarehouseSales.java
public class GetWarehouseSales extends Activity {
private String TAG = MainActivity.class.getSimpleName();
private ListView lv;
private ProgressDialog pDialog;
//URL to get JSON details
private static String url = "http://192.168.0.1/mycc/retrieve_ws.php";
ArrayList<HashMap<String,String>> sales_details;
public GetWarehouseSales(){
}
public void executeGWS(){
new RetrieveWarehouseSalesTask().execute();
}
public class RetrieveWarehouseSalesTask extends AsyncTask<Void,Void,Void>{
public RetrieveWarehouseSalesTask(){
}
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(GetWarehouseSales.this);
pDialog.setMessage("Getting you the best warehouse sales...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0){
HttpHandler sh = new HttpHandler();
//making a request to URL and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if(jsonStr != null){
try{
JSONObject jsonObj = new JSONObject(jsonStr);
//Getting JSON Array Node
JSONArray sales = jsonObj.getJSONArray("Result");
//looping through all results
for(int i = 0; i<sales.length();i++){
JSONObject s = sales.getJSONObject(i);
String title = s.getString("title");
String description = s.getString("description");
HashMap<String,String> salesDetails = new HashMap<>();
//adding each child node to HashMap key =>value
salesDetails.put("title",title);
salesDetails.put("description",description);
//adding to array list
sales_details.add(salesDetails);
}
Log.d("TAG",sales_details.toString());
}catch(final JSONException e){
Log.e(TAG, "JSON parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"JSON parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}else{
Log.e(TAG,"Couldn't get json from server");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Check logcat", Toast.LENGTH_LONG ).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
if(pDialog.isShowing()){
pDialog.dismiss();
}
//update parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(GetWarehouseSales.this, sales_details,R.layout.item_listview, new String[]{
"title","description"}, new int[]{R.id.text,R.id.description});
lv.setAdapter(adapter);
}
}
}
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
GetWarehouseSales gws = new GetWarehouseSales();
gws.executeGWS();
}
The error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
The error point to this line:
pDialog = new ProgressDialog(GetWarehouseSales.this);
I would like to execute the AsyncTask RetrieveWarehouseSalesTask which can be found in GetWarehouseSales.java in MainActivity.java but getting NPE errors.
回答1:
try passing the MainActivity Context..
@Override
protected void onCreate(Bundle savedInstanceState) {
GetWarehouseSales gws = new GetWarehouseSales(MainActivity.this);
gws.executeGWS();
}
Retreive the context:
private Activity activityContext;
public GetWarehouseSales(Activity context){
this.activityContext=context;
}
Now initialize your ProgressDialog using that context
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(activityContext);
pDialog.setMessage("Getting you the best warehouse sales...");
pDialog.setCancelable(false);
pDialog.show();
}
or ..try using
pDialog = new ProgressDialog(getApplicationContext());
回答2:
This is the reason of the error:
GetWarehouseSales gws = new GetWarehouseSales();
gws.executeGWS();
You create an instance of activity, and when the AsyncTask is executing, try to get access to the Context.
Looks like the simpliest fix is to place RetrieveWarehouseSalesTask into MainActivity, something like:
class MainActivity ... {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
new RetrieveWarehouseSalesTask().execute();
}
public class RetrieveWarehouseSalesTask extends AsyncTask<Void,Void,Void>{
...
}
回答3:
hope this will help you..
@Override
protected void onCreate(Bundle savedInstanceState) {
GetWarehouseSales gws = new GetWarehouseSales(this);
gws.executeGWS();
}
Retreive the context:
private Context activityContext;
public GetWarehouseSales(Context context){
this.activityContext=context;
}
Now initialize your ProgressDialog using that context
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(activityContext);
pDialog.setMessage("Getting you the best warehouse sales...");
pDialog.setCancelable(false);
pDialog.show();
}
来源:https://stackoverflow.com/questions/41414917/nullpointerexception-for-progressdialog-in-asynctask