在MainActivity中使用JSON解析发送致命错误

时间:2014-04-06 13:50:18

标签: android json parsing

当我想解析JSON时,我发现了这个错误:

04-06 13:39:13.517: W/dalvikvm(2582): threadid=1: thread exiting with uncaught exception (group=0xa62f9288)
04-06 13:39:13.521: E/AndroidRuntime(2582): FATAL EXCEPTION: main
04-06 13:39:13.521: E/AndroidRuntime(2582): java.lang.NullPointerException
04-06 13:39:13.521: E/AndroidRuntime(2582):     at JSONParser.GetTunes.onPostExecute(GetTunes.java:207)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at JSONParser.GetTunes.onPostExecute(GetTunes.java:1)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at android.os.AsyncTask.finish(AsyncTask.java:631)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at android.os.Looper.loop(Looper.java:137)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at java.lang.reflect.Method.invoke(Method.java:511)
04-06 13:39:13.521: E/AndroidRuntime(2582):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

更新: 上课在这里:

public class GetTunes extends AsyncTask<Void, Void, Void> {

    private Activity activity;
    private ArrayList<HashMap<String, String>> DATA;
    private String DOMAIN, URL;
    private ProgressDialog pDialog;

    LazyAdapter Lazyadapter;
    ListView list;

    private static final String TAG_TONE = "objects";
    // =================================================================
    // CATEGORY INFORMATION
    private static final String TAG_CAT = "category";
    private static final String TAG_CAT_CNT = "cnt_tones";
    private static final String TAG_CAT_ID = "id";
    private static final String TAG_CATID = "cat_id";
    private static final String TAG_CAT_IMG = "image";
    private static final String TAG_CAT_TITLE = "title";
    private static final String TAG_CATTITLE = "cat_title";
    private static final String TAG_CAT_RES_URI = "resource_uri";
    // ================================================================= TONE
    // INFORMATION
    private static final String TAG_DOWNLOAD = "cnt_download";
    private static final String TAG_LIKE = "cnt_like";
    private static final String TAG_CREATE_DATE = "create_time";
    private static final String TAG_URL = "file_url";
    private static final String TAG_TONE_ID = "id";
    private static final String TAG_TONEID = "tone_id";
    private static final String TAG_TITLE = "title";
    private static final String TAG_TONETITLE = "tone_title";
    private static final String TAG_RES_URI = "resource_uri";
    // ================================================================= USER
    // INFORMATION
    private static final String TAG_USER = "user";
    private static final String TAG_USER_AVATAR = "avatar";
    private static final String TAG_USER_ID = "id";
    private static final String TAG_USERID = "user_id";
    private static final String TAG_USER_NAME = "name";

    // CONSTRUCTOR
    public GetTunes(Activity a, ArrayList<HashMap<String, String>> d, String domain, String url) {
        activity = a;
        DATA = d;
        DOMAIN = domain;
        URL = url;
    }

    JSONArray Ringtunes = null;

    // =====================================================================
    // onPreExecute
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(activity);
        pDialog.setMessage("Wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        Log.d("phase1 ", "> " + "DONE");
    }

    // =====================================================================

    // =====================================================================
    // doInBackground
    @Override
    protected Void doInBackground(Void... arg0) {
        // CREATING SERVICE HANDLER CLASS INSTANCE
        ServiceHandler sh = new ServiceHandler();

        // MAKING A RESQUEST TO URL AND GETTING RESPONSE
        String jsonStr = sh.makeServiceCall(URL, ServiceHandler.GET);
        Log.d("phase2 ", "> " + "DONE");
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {

            try {

                JSONObject jsonObj = new JSONObject(jsonStr);

                // GETTING JSON ARRAY NODE
                Ringtunes = jsonObj.getJSONArray(MainActivity.TAG_TONE);

                // LOOPIN THROUGH ALL TONES
                for (int i = 0; i < Ringtunes.length(); i++) {

                    JSONObject ring = Ringtunes.getJSONObject(i);

                    // CATEGORY NODE IS A JSON OBJECT
                    JSONObject category             = ring.getJSONObject(MainActivity.TAG_CAT);
                    String cat_cnt                  = category.getString(MainActivity.TAG_CAT_CNT);
                    String cat_id                   = category.getString(MainActivity.TAG_CAT_ID);
                    String cat_img                  = category.getString(MainActivity.TAG_CAT_IMG);
                    String cat_title                = category.getString(MainActivity.TAG_CAT_TITLE);
                    String cat_uri                  = category.getString(MainActivity.TAG_CAT_RES_URI);

                    String tone_download            = ring.getString(MainActivity.TAG_DOWNLOAD);
                    String tone_like                = ring.getString(MainActivity.TAG_LIKE);
                    String tone_createDate          = ring.getString(MainActivity.TAG_CREATE_DATE);
                    String tone_url                 = DOMAIN + ring.getString(MainActivity.TAG_URL);
                    String tone_id                  = ring.getString(MainActivity.TAG_TONE_ID);
                    String tone_title               = ring.getString(MainActivity.TAG_TITLE);
                    String tone_uri                 = ring.getString(MainActivity.TAG_RES_URI);

                    // USER NODE IS A JSON OBJECT
                    JSONObject user                 = ring.getJSONObject(MainActivity.TAG_USER);
                    String user_avatar              = DOMAIN + user.getString(MainActivity.TAG_USER_AVATAR);
                    String user_id                  = user.getString(MainActivity.TAG_USER_ID);
                    String user_name                = user.getString(MainActivity.TAG_USER_NAME);

                    // TEM HASHMAP FOR EACH TONE
                    HashMap<String, String> tones = new HashMap<String, String>();

                    // ADDING EACH CHILD NODE TO HASH MAP KEY > VALUE
                    tones.put(TAG_CAT_CNT, cat_cnt);
                    tones.put(TAG_CATID, cat_id);
                    tones.put(TAG_CAT_IMG, cat_img);
                    tones.put(TAG_CATTITLE, cat_title);
                    // ======================================
                    tones.put(TAG_DOWNLOAD, tone_download);
                    tones.put(TAG_LIKE, tone_like);
                    tones.put(TAG_CREATE_DATE, tone_createDate);
                    tones.put(TAG_URL, tone_url);
                    tones.put(TAG_TONEID, tone_id);
                    tones.put(TAG_TONETITLE, tone_title);
                    // ======================================
                    tones.put(TAG_USER_AVATAR, user_avatar);
                    tones.put(TAG_USERID, user_id);
                    tones.put(TAG_USER_NAME, user_name);

                    Set keys = tones.keySet();
                    for (Iterator iter = keys.iterator(); iter.hasNext();) {
                        String key = (String) iter.next();
                        String value = (String) tones.get(key);
                                   Log.d(key, value);
                    }

                    // ADDING TONES TO LIST
                    DATA.add(tones);
                    Log.d("phase3 ", "> " + "DONE");
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
            Toast.makeText(activity, "Couldn't get any data from the url",
                    Toast.LENGTH_LONG).show();
        }

        return null;
    }

    // =====================================================================

    // =====================================================================
    // onPostExecute
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        // Dismiss the Progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();

        // Updating parsed JSON data into List view
        Lazyadapter = new LazyAdapter(activity, DATA);
        AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(Lazyadapter);
        animAdapter.setInitialDelayMillis(300);
        animAdapter.setAbsListView(list);
        list.setAdapter(animAdapter);
    }
    // =====================================================================

}

我想在我的主要活动中使用这个calss并将其称为:

新的GetTunes(MainActivity.this,TunesList,thumb,url).execute();

1 个答案:

答案 0 :(得分:1)

list为空。您没有在代码中的任何位置初始化它。所以这就是错误所在的行:list.setAdapter(animAdapter);。通过动态创建或设置资源ID来初始化它,您的问题就会得到解决。