无法从android资产中获取json文件

时间:2014-06-06 13:08:18

标签: android json android-context android-assets

我有一个类JSONParser,我正在尝试解析其中的JSON文件。我已存储了 assets文件夹中的json文件。我试图这样做:

public class JSONParser {
    Context context;
    public JSONParser(Context context) {
        this.context = context;
    }
    private String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = context.getAssets().open("websites.json");
            // Other required codes
        }
        //rest of the code
    }
    //rest of code. I have a public function in this to return the parsed data to MainActivity
}

MainActivity我已经像这样开始了这个课程:

public class MainActivity extends Activity {
    private JSONParser jsonParser;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        jsonParser = new JSONParser(getApplicationContext());
        // Rest of code. I am calling the function in the JSONParser to get the object and all.
    }
}

该应用程序崩溃了。我尝试设置断点,发现json文件无法获取。这是什么问题?我究竟做错了什么?如何纠正这个?

修改

我在logcat中遇到的异常:

06-06 18:42:41.724: E/AndroidRuntime(27040): FATAL EXCEPTION: main
06-06 18:42:41.724: E/AndroidRuntime(27040): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.NullPointerException
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.os.Handler.dispatchMessage(Handler.java:107)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.os.Looper.loop(Looper.java:194)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.app.ActivityThread.main(ActivityThread.java:5371)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at java.lang.reflect.Method.invokeNative(Native Method)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at java.lang.reflect.Method.invoke(Method.java:525)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at dalvik.system.NativeStart.main(Native Method)
06-06 18:42:41.724: E/AndroidRuntime(27040): Caused by: java.lang.NullPointerException
06-06 18:42:41.724: E/AndroidRuntime(27040):    at info.androidhive.slidingmenu.json.WebSitesList.addToWebSitesArray(WebSitesList.java:51)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at info.androidhive.slidingmenu.json.JSONParser.getWebSitesList(JSONParser.java:43)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at info.androidhive.slidingmenu.MainActivity.onCreate(MainActivity.java:60)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.app.Activity.performCreate(Activity.java:5135)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
06-06 18:42:41.724: E/AndroidRuntime(27040):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
06-06 18:42:41.724: E/AndroidRuntime(27040):    ... 11 more

MainActivity.java中的第60行是我在JSONParser中调用函数以返回已解析对象的位置。我尝试设置断点,我发现实际问题是json文件永远不会被读取来解析。在WebSitesList.java第51行,我有这个:

this.webSitesArray.add(aWebSiteDetail);

2 个答案:

答案 0 :(得分:1)

检查this.webSitesArray。看起来这个变量是null。

答案 1 :(得分:-1)

从资产文件夹加载json数据的正确方法是

public String loadJSONFromAsset(){

String json = null;

try {

    InputStream is = getAssets().open("yourfilename.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;

}