我正在尝试使用我的Assets文件夹中json中的字段填充Android应用中的RecyclerView。 json文件在我的res的资产文件夹中。但是getClass()。getResourseAsStream()和getAssets()。open()都无法在资产文件夹中找到json文件。 它提供FileNotFoundException或NullPointerException
我尝试使用
InputStream is = getAssets().open("activities.json");
InputStream is = getClass().getResourceAsStream("/activities.json");
还尝试使用org.json.simple.parser.JSONParser;
这是我的活动文件
try {
URL url = getClass().getResource("activities.json");
File f = new File(url.toURI());
Object object = parser.parse(new FileReader(f));
JSONObject obj = new JSONObject(json);
JSONArray m_jArray = obj.getJSONArray("Activities");
for (int i = 0; i < m_jArray.length(); i++) {
JSONObject jo_inside = m_jArray.getJSONObject(i);
myDataset.add(new Activity(
jo_inside.getString("title"),
jo_inside.getString("desc")
));
}
} catch (JSONException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这是我的活动。json
{
"Activities" : [ {
"desc" : "Activity 0 desc",
"title" : "Activity 0"
}, {
"desc" : "Activity 1 desc",
"title" : "Activity 1"
}, {
"desc" : "Activity 2 desc",
"title" : "Activity 2"
}, {
"desc" : "Activity 3 desc",
"title" : "Activity 3"
}, {
"desc" : "Activity 4 desc",
"title" : "Activity 4"
}, {
"desc" : "Activity 5 desc",
"title" : "Activity 5"
} ]
}
出现以下错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ams_1, PID: 21647
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ams_1/com.example.ams_1.Activities}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.net.URI java.net.URL.toURI()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.net.URI java.net.URL.toURI()' on a null object reference
at com.example.ams_1.Activities.onCreate(Activities.java:110)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)