从另一个 activity JAVA 呼叫 JSON

Calling JSON from another activity JAVA

我正在尝试调用以下方法,该方法应该从另一个 activity 解析一个 JSON 文件。为什么 Android Studio 向我显示错误?

这是第一个 activity 调用 FirstActivity.java

 public class FirstActivity extends AppCompatActivity {

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

        ReadJSON classToReadJSON = new ReadJSON();
        classToReadJSON.loadJSONFromAsset();
    }}

这是我的第二个class ReadJSON.java:

public class ReadJSON extends AppCompatActivity{
    public String loadJSONFromAsset() {
            String json = null;
            try {
                InputStream is = getAssets().open("jsonfile.json");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                json = new String(buffer, "UTF-8");
            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }
            return json;
        }
}

解析JSON文件的代码来自Faizan的回答。 Reading a json file in Android

编辑:很抱歉没有发布错误日志,我对编程还是很陌生。

所以这是错误日志:

D/AndroidRuntime: Shutting down VM


                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.zeus.jsonWhosebug, PID: 2805
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zeus.jsonWhosebug/com.example.zeus.jsonWhosebug.FirstActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                      at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
                      at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
                      at android.view.ContextThemeWrapper.getAssets(ContextThemeWrapper.java:116)
                      at com.example.zeus.jsonWhosebug.ReadJSON.loadJSONFromAsset(ReadJSON.java:16)
                      at com.example.zeus.jsonWhosebug.FirstActivity.onCreate(FirstActivity.java:14)
                      at android.app.Activity.performCreate(Activity.java:6662)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

对于此事,我将不胜感激。

如果您不使用 Activity,则必须将上下文传递给该方法,试试这个:

 public class FirstActivity extends AppCompatActivity {

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

        ReadJSON classToReadJSON = new ReadJSON();
        classToReadJSON.loadJSONFromAsset(this);
    }
}

ReadJSON.class

public class ReadJSON {
public String loadJSONFromAsset(Context c) {
        String json = null;
        try {
            InputStream is = c.getAssets().open("jsonfile.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }
}

您的代码中有不少 problems/misconceptions。

首先,您的 ReadJSON 不需要扩展 AppCompatActivity,因为它不是 Activity。这是一种方法:

public class FirstActivity extends AppCompatActivity {

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

        String myJsonString = loadJSONFromAsset();
    }

    public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = getAssets().open("jsonfile.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

}

其次,对于返回的 json,您必须做一些事情。我把它放在 myJsonString 变量中,您可能必须将它转换为 JSONObject 并解析它。

出现崩溃是因为您试图从空上下文中获取 .getAssets(...)。通过将 loadJSONFromAsset 放入您的 activity 您将拥有一个上下文。

谢谢大家到目前为止的帮助!

我发现它与以下行有关。

        InputStream is = getAssets().open("jsonfile.json");

为什么无法从我的第一个 activity 调用这条线?