java - Android:进度对话框错误 java.lang.NullPointerException

标签 java android

我正在尝试改编两个教程来学习 Volley。

我在显示进度对话框时出错。

这是我的代码:

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

错误是

    [...]
    FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webtemplum.crazyweather/com.webtemplum.crazyweather.MainActivity}: java.lang.NullPointerException
[...]
used by: java.lang.NullPointerException
            at com.webtemplum.crazyweather.MainActivity.showpDialog(MainActivity.java:222)
            at com.webtemplum.crazyweather.MainActivity.loadMarsWeatherData(MainActivity.java:56)
            at com.webtemplum.crazyweather.MainActivity.onCreate(MainActivity.java:46)

(删除了一些错误行,如果我理解正确的话,错误是在 showpDialog 中)。

当然,如果我评论 showpDialog应用程序运行。

如果我评论loadWeatherData中的所有 block ,同样的事情(应用程序运行)并仅留下

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();

关于java - Android:进度对话框错误 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30690832/

相关文章:

android - 在某些事件上暂停任何媒体播放器

android - Android最新的YouTube更新-离线下载时,YouTube视频将存储在哪个位置

java - 战舰计划将舰船置于一团中

java - 我怎样才能使这个程序更快更简单?

java - 如何在 Android 的 Receiver/Service 中获取 CheckBoxPreference 值?

android - ViewHolder ImageView setVisibility 重复

android - 我应该使用哪个图像处理库来创建视频幻灯片?

java - 如何检查对象数组中对象的重复值,合并重复值,然后删除重复值?

java - JSoup 的 select() 方法是上下文相关的吗?

java - 通过覆盖 Arrays.sort() 对对象数组进行排序