我难以将JSON解析为警报对话框,获得空指针异常

时间:2017-06-03 06:43:37

标签: java android nullpointerexception

我是android开发的初学者。我正在开发一款具有“当日特色”功能的Android应用。在我的选项菜单中选择项目时,我一直试图从URL解析JSON对象以显示在Alert Dialog中,但到目前为止,我还没有取得任何成功。 这是我的代码:

private TextView textView;


     public boolean onOptionsItemSelected(MenuItem item) {
                    LayoutInflater inflater = (LayoutInflater) 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE);


         View verseofTheDay = inflater.inflate(R.layout.my_verse, null);
                switch (item.getItemId())

     case R.id.bible:
                    AlertDialog.Builder verseofday = new AlertDialog.Builder(this);
                    verseofday.setCancelable(true);
                    verseofday.setView(verseofTheDay);
                    textView = (TextView) findViewById(R.id.verseDay);
                    verseofday.show();
                    new JSONTask().execute("http://app.wordedfm.com/api/scripture");
                    break;
            }
            return super.onOptionsItemSelected(item);
        }

    And here is my JSON class:
    public class JSONTask extends AsyncTask<String, String, String>{

            @Override
            protected String doInBackground(String... params) {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                try {
                    URL url = new URL(params[0]);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();
                    InputStream stream  = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(stream));
                    StringBuffer buffer= new StringBuffer();
                    String line = "";
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line);
                    }
                    return buffer.toString();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                    try {
                        if (reader != null){
                            reader.close();
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                textView.setText(result);
            }
        }
    }

但每次我都会收到此错误:

.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
06-02 05:08:46.983 16307-16307/com.musicianfocus.ben.wordedfm E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                java.lang.NullPointerException
                                                                                    at com.musicianfocus.ben.wordedfm.MainActivity$JSONTask.onPostExecute(MainActivity.java:209)
                                                                                    at com.musicianfocus.ben.wordedfm.MainActivity$JSONTask.onPostExecute(MainActivity.java:199)
                                                                                    at android.os.AsyncTask.finish(AsyncTask.java:631)
                                                                                    at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                                    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                    at android.os.Looper.loop(Looper.java:137)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

onPost方法返回的字符串为null。您需要将JSON的结果存储在不同的变量中。并使用方法toString()。

希望这有帮助。