Android:进度对话框错误java.lang.NullPointerException
我正在尝试改编两个教程,学习截击 显示进度对话框时出错 这是我的代码:Android:进度对话框错误java.lang.NullPointerException,java,android,Java,Android,我正在尝试改编两个教程,学习截击 显示进度对话框时出错 这是我的代码: public class MainActivity extends Activity { final static String MARS_WEATHER_RECENT = "http://marsweather.ingenology.com/v1/latest/"; private static String TAG = MainActivity.class.getSimpleName(); /
public class MainActivity extends Activity {
final static String MARS_WEATHER_RECENT = "http://marsweather.ingenology.com/v1/latest/";
private static String TAG = MainActivity.class.getSimpleName();
// Progress dialog
private ProgressDialog pDialog;
private TextView debug_container;
private TextView degrees;
// temporary string to show the parsed response
private String jsonResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
debug_container = (TextView) findViewById(R.id.debug_container);
degrees = (TextView) findViewById(R.id.degrees);
loadMarsWeatherData();
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
}
private void loadMarsWeatherData() {
showpDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
MARS_WEATHER_RECENT, (String)null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
// Parsing json object response
// response will be a json object
response = response.getJSONObject("report");
String min_degree = response.getString("min_temp");
degrees.setText(min_degree);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hidepDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
(去掉一些错误行,如果我理解正确,错误在showpDialog中)
当然,如果我评论showpDialog
app运行
如果我在loadWeatherData
中注释所有块并只留下,则会发生同样的事情(应用程序运行)
private void loadMarsWeatherData() {
showpDialog();
hidepDialog();
}
非常感谢。看看这段代码:
loadMarsWeatherData();
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
在实例化pDialog
之前,您正在调用loadMarsWeatherData
。但是,在loadMarsWeatherData
中,您正在调用pDialog
:
showpDialog();
将致电:
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
您需要更改顺序-首先实例化pDialog,然后调用该方法:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
loadMarsWeatherData();
看看这段代码:
loadMarsWeatherData();
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
在实例化pDialog
之前,您正在调用loadMarsWeatherData
。但是,在loadMarsWeatherData
中,您正在调用pDialog
:
showpDialog();
将致电:
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
您需要更改顺序-首先实例化pDialog,然后调用该方法:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
loadMarsWeatherData();
在声明返回空值的进度对话框之前,您正在调用
loadMarsWeatherData()
。在声明进度对话框后,只需放置loadMarsWeatherData()
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
loadMarsWeatherData();
你们都完了 在声明返回空值的进度对话框之前,您正在调用
loadMarsWeatherData()
。在声明进度对话框后,只需放置loadMarsWeatherData()
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
loadMarsWeatherData();
你们都完了 在
MainActivity.java
第222行中,有些东西是null
。在MainActivity.java
第222行中,有些东西是null
。