使用json的Android错误引起:java.lang.NullPointerException

时间:2015-12-13 17:28:32

标签: java android json string

我必须创建一个程序,它从json文件中写入信息。我有RelativeLayout,如果单击它显示第二个Json文件,如果第二次单击显示第三个Json文件,则所有文件都在字符串数组中。当我尝试编译我的项目时,我有一个错误。

12-13 18:06:05.279  12257-12257/? E/AndroidRuntime﹕ FATAL EXCEPTION:     main
Process: miguel.example.com.jsonzadanie, PID: 12257
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{miguel.example.com.jsonzadanie/miguel.example.com.jsonzadanie.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
        at android.app.ActivityThread.access$900(ActivityThread.java:154)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5294)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
        at android.app.Activity.findViewById(Activity.java:2072)
        at miguel.example.com.jsonzadanie.MainActivity.<init>(MainActivity.java:41)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1606)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5294)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

这是我的代码:

package miguel.example.com.jsonzadanie;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class MainActivity extends AppCompatActivity {

    private static final String strJson[] = new String[2];

            @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        strJson[0] = "{\n\t\"items\": [\n\t\t{\n\t\t\t\"index\": 1,\n\t\t\t\"name\": \"Bobby\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 19,\n\t\t\t\t\t\"email\": \"louis@hardy.eg\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 13,\n\t\t\t\t\t\"email\": \"cynthia@mills.mc\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 0,\n\t\t\t\t\t\"email\": \"leo@graham.kp\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 2,\n\t\t\t\"name\": \"Maxine\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 14,\n\t\t\t\t\t\"email\": \"miriam@horne.mo\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 20,\n\t\t\t\t\t\"email\": \"danny@joyce.ro\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 25,\n\t\t\t\t\t\"email\": \"christina@davenport.fo\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 3,\n\t\t\t\"name\": \"Ruth\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 1,\n\t\t\t\t\t\"email\": \"heather@kirk.bm\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 29,\n\t\t\t\t\t\"email\": \"kim@middleton.ve\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 2,\n\t\t\t\t\t\"email\": \"harold@arthur.net\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 4,\n\t\t\t\"name\": \"Brandon\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 3,\n\t\t\t\t\t\"email\": \"danny@parrott.eg\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 5,\n\t\t\t\t\t\"email\": \"sarah@underwood.net\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 11,\n\t\t\t\t\t\"email\": \"bonnie@banks.ye\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 5,\n\t\t\t\"name\": \"Kristina\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 18,\n\t\t\t\t\t\"email\": \"frances@law.mf\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 15,\n\t\t\t\t\t\"email\": \"nancy@o.mo\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 6,\n\t\t\t\t\t\"email\": \"ben@holden.um\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 6,\n\t\t\t\"name\": \"Keith\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 10,\n\t\t\t\t\t\"email\": \"alfred@baldwin.za\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 9,\n\t\t\t\t\t\"email\": \"courtney@hinson.us\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 7,\n\t\t\t\t\t\"email\": \"tracey@keller.li\"\n\t\t\t\t}\n\t\t\t]\n\t\t}\n\t]\n}";
        strJson[1] = "{\n\t\"items\": [\n\t\t{\n\t\t\t\"index\": 7,\n\t\t\t\"name\": \"Jose\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 7,\n\t\t\t\t\t\"email\": \"vincent@crews.st\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 12,\n\t\t\t\t\t\"email\": \"louis@zimmerman.edu\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 11,\n\t\t\t\t\t\"email\": \"martin@creech.tw\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 8,\n\t\t\t\"name\": \"Douglas\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 8,\n\t\t\t\t\t\"email\": \"dorothy@west.as\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 13,\n\t\t\t\t\t\"email\": \"clyde@bolton.mw\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 0,\n\t\t\t\t\t\"email\": \"jerome@berger.bf\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 9,\n\t\t\t\"name\": \"Joshua\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 1,\n\t\t\t\t\t\"email\": \"sidney@watson.lb\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 21,\n\t\t\t\t\t\"email\": \"carol@cox.bs\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 10,\n\t\t\t\t\t\"email\": \"dianne@paul.am\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 1,\n\t\t\t\"name\": \"Caroline\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 19,\n\t\t\t\t\t\"email\": \"louis@hardy.eg\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 13,\n\t\t\t\t\t\"email\": \"clyde@bolton.mw\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 4,\n\t\t\t\t\t\"email\": \"paige@olson.ge\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 2,\n\t\t\t\"name\": \"Regina\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 14,\n\t\t\t\t\t\"email\": \"mary@schultz.lu\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 6,\n\t\t\t\t\t\"email\": \"vicki@archer.tl (changed from tp)\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 15,\n\t\t\t\t\t\"email\": \"jerome@griffin.ch\"\n\t\t\t\t}\n\t\t\t]\n\t\t}\n\t]\n}\n";
        strJson[2] = "{\n\t\"items\": [\n\t\t{\n\t\t\t\"index\": 3,\n\t\t\t\"name\": \"Edgar\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 1,\n\t\t\t\t\t\"email\": \"heather@kirk.bm\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 29,\n\t\t\t\t\t\"email\": \"kim@middleton.ve\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 5,\n\t\t\t\t\t\"email\": \"shawn@crawford.re\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 11,\n\t\t\t\"name\": \"Harvey\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 9,\n\t\t\t\t\t\"email\": \"paul@paul.mn\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 18,\n\t\t\t\t\t\"email\": \"kelly@horne.vi\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 28,\n\t\t\t\t\t\"email\": \"jim@moss.it\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 15,\n\t\t\t\"name\": \"Karen\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 2,\n\t\t\t\t\t\"email\": \"ross@mann.by\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 7,\n\t\t\t\"name\": \"Franklin\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 7,\n\t\t\t\t\t\"email\": \"louise@benson.ga\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 19,\n\t\t\t\t\t\"email\": \"melinda@zimmerman.ls\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 22,\n\t\t\t\t\t\"email\": \"lynda@desai.br\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"index\": 1,\n\t\t\t\"name\": \"Amanda\",\n\t\t\t\"events\": [\n\t\t\t\t{\n\t\t\t\t\t\"index\": 28,\n\t\t\t\t\t\"email\": \"jim@moss.it\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 13,\n\t\t\t\t\t\"email\": \"clyde@bolton.mw\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"index\": 4,\n\t\t\t\t\t\"email\": \"paige@olson.ge\"\n\t\t\t\t}\n\t\t\t]\n\t\t}\n\t]\n}\n";

    }
    public void Refresh_listener(View v) {
        RelativeLayout ll = (RelativeLayout) findViewById(R.id.relative);
        ll.removeAllViews();

        int iter = 0;
        iter++;
        if (iter >= strJson.length)
            iter = 0;

        parse(strJson[iter]);

    }

    TextView output = (TextView) findViewById(R.id.textView1);

    public void parse(String strJson) {
        String data = "";

        try {
            JSONObject jsonRootObject = new JSONObject(strJson);

            //Get the instance of JSONArray that contains JSONObjects
            JSONArray jsonArray = jsonRootObject.optJSONArray("items");

            //Iterate the jsonArray and print the info of JSONObjects
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                int id = Integer.parseInt(jsonObject.optString("index").toString());
                String name = jsonObject.optString("name").toString();


                JSONArray events = new JSONArray(jsonArray.getJSONObject(i).getString("events"));
                for (int j = 0; j < events.length(); j++) {
                    int index = events.getJSONObject(j).getInt("index");
                    String email = events.getJSONObject(j).getString("email");
                    data += "Index: " + id + "  Name: " + name + " index= " + index + " email: " + email + "\n";
                }


            }
            output.setText(data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

        /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }*/

和xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/relative"
    android:onClick="Refresh_listener">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Michał Latus"
        android:id="@+id/textView"
        android:textSize="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Zadanie Android"
        android:id="@+id/textView2"
        android:textSize="35dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

另一个问题:

12-13 19:00:56.109  23670-23670/miguel.example.com.jsonzadanie E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: miguel.example.com.jsonzadanie, PID: 23670
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4029)
            at android.view.View.performClick(View.java:4789)
            at android.view.View$PerformClick.run(View.java:19881)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5294)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4024)
            at android.view.View.performClick(View.java:4789)
            at android.view.View$PerformClick.run(View.java:19881)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5294)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at miguel.example.com.jsonzadanie.MainActivity.parse(MainActivity.java:68)
            at miguel.example.com.jsonzadanie.MainActivity.Refresh_listener(MainActivity.java:38)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4024)
            at android.view.View.performClick(View.java:4789)
            at android.view.View$PerformClick.run(View.java:19881)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5294)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

1 个答案:

答案 0 :(得分:0)

这一行:

TextView output = (TextView) findViewById(R.id.textView1);

外面有些方法。将它放入onCreate方法,如下所示:

TextView output;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    output = (TextView) findViewById(R.id.textView1);
    ...
}