Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我在解析JSON到警报对话框时遇到困难,获取空指针异常_Java_Android_Nullpointerexception - Fatal编程技术网

Java 我在解析JSON到警报对话框时遇到困难,获取空指针异常

Java 我在解析JSON到警报对话框时遇到困难,获取空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,我是android开发的初学者。我正在开发一款android应用程序,它具有“每日诗句”功能。我已经尝试了一段时间从URL解析JSON对象,以便在我的选项菜单中选择该项时出现在警报对话框中,但到目前为止,我还没有取得任何成功。 这是我的密码: private TextView textView; public boolean onOptionsItemSelected(MenuItem item) { LayoutInflater infla

我是android开发的初学者。我正在开发一款android应用程序,它具有“每日诗句”功能。我已经尝试了一段时间从URL解析JSON对象,以便在我的选项菜单中选择该项时出现在警报对话框中,但到目前为止,我还没有取得任何成功。 这是我的密码:

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

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


希望这有帮助。

因为您的textview引用为空。使用textView=(textView)vs.findViewById(R.id.verseDay);你的文本视图为空。谢谢你!工作。
.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)