在扩展Fragment的类中创建listview

时间:2014-06-30 00:02:07

标签: android android-listview android-asynctask

我有一个扩展Fragment的类,但我也想在该片段中创建一个ListView。我按照[Androidhive JSON parse] [1]中关于如何解析JSON数据的教程。成功地尝试了一个项目,其唯一目的是从不同的链接做同样的事情。

然后我继续尝试使用来自[Androidhive Tab Layout] [2]的视图寻呼机实现标签。

在第二个项目中,我的一个选项卡意味着有一个列表视图,其中的数据是从链接中解析的。我创建了一个扩展AsyncTask的私有类,但是当我运行应用程序时, doInBackground()方法导致应用程序崩溃...根据我的LogCat。

请帮忙,因为我不确定这个错误发生的确切位置,因为看起来我的try catch语句没有帮助,app在try catch块有机会显示哪些块导致错误之前崩溃。

我已经给了应用程序互联网许可。非常感谢帮助。

这是我的 doInBackground()方法:

protected Void doInBackground(Void... params) {
        ServiceHandler sh;
        String jsonStr = "";

        try {
            // creating a service handler
            sh = new ServiceHandler();

            // making a request to url and getting response
            // String jsonStr = sh.makeServiceCall(url,ServiceHandler.GET);

            /*
             * This code with concat json on it concatentates 7json arrays
             * from whatsonwindhoek by simply having the first
             * array,replacing the ] with a , and then appending other
             * arrays for 5 other days. Loops by replacing the ] with a ,
             * and finally when it exits the loop it adds the array from the
             * last day after it replaces the first char which is a [ with a
             * ,
             */
            // trying json concatenation here

            // array of first day of events i.e. current day
            jsonStr = sh.makeServiceCall(url2 + "0", ServiceHandler.GET);

            // variables to store and manipulate the arrays as they come
            String appendedJSON;
            StringBuffer buffer, buffer2;
            buffer = new StringBuffer(jsonStr);

            // loop for days in between
            for (int i = 1; i < 6; i++) {

                buffer.setCharAt(buffer.length() - 1, ','); // replace last
                                                            // character
                                                            // which
                                                            // is a ] with a
                                                            // ,

                appendedJSON = sh.makeServiceCall(url2 + i,
                        ServiceHandler.GET); // call
                                                // arrays
                                                // of
                                                // other
                                                // days
                                                // one
                                                // by
                                                // one
buffer2 = new StringBuffer(appendedJSON);// new buffer to
                                                            // store
                                                            // appendedJSON
                                                            // result for
                                                            // manipulation
                buffer2.deleteCharAt(0);// remove the first character which
                                        // is a
                                        // [

                buffer.append(buffer2); // add manipulated data to buffer
                                        // belonging to the first array

            }// end for
appendedJSON = sh.makeServiceCall(url2 + "6",ServiceHandler.GET);// call
                                        // for
                                        // last
                                        // day

            buffer2 = new StringBuffer(appendedJSON);// pull data for
                                                        // manipulation
            buffer2.setCharAt(0, ','); // replace [ with ,

            buffer.append(buffer2);// append to final buffer

            jsonStr = buffer.toString(); // convert back to string and end
                                            // the
                                            // madness of json concatenation

            // end trying json concat stunt
        } catch (Exception ex) {

            Log.e("doInBackGround", "JSON concat problem");
            Toast.makeText(getActivity(), "Error doInBackground(): " + ex.toString(),
                    Toast.LENGTH_SHORT).show();
        }
        if (jsonStr != null) {
            try {

                // convert string from result to json array
                events = new JSONArray(jsonStr);

                // loop for all json objects in the array
                for (int i = 0; i < events.length(); i++) {
                    JSONObject event = events.getJSONObject(i);

                    String title = event.getString(TAG_TITLE);
                String location = event.getString(TAG_LOCATION);
            String description = event.getString(TAG_DESCRIPTION);
                String organizer = event.getString(TAG_ORGANIZER);
                String file = event.getString(TAG_FILE);
                    String email = event.getString(TAG_EMAIL);

                    HashMap<String, String> singleEvent = new HashMap<String, String>();

                    singleEvent.put(TAG_TITLE, title);
                    singleEvent.put(TAG_EMAIL, email);
                    singleEvent.put(TAG_LOCATION, location);

                    eventList.add(singleEvent);

                }// endfor

            } catch (JSONException e) {
                Log.e("doInBackGround", "JSON problem");
                Toast.makeText(getActivity(), "Error doInBackground(): " + e.toString(),
                        Toast.LENGTH_SHORT).show();
            }// endtrycatch

        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }// endif
        return null;
    }

以及仅来自我的LogCat的错误记录:

<i>06-30 00:42:08.093: E/(28963): Warning: Could not set thread affinity 1. continueing..
06-30 00:44:14.538: E/(29611): Warning: Could not set thread affinity 1. continueing..
06-30 00:44:42.208: E/(29862): Warning: Could not set thread affinity 1. continueing..
06-30 00:46:08.898: E/AndroidRuntime(29862): FATAL EXCEPTION: AsyncTask #1
06-30 00:46:08.898: E/AndroidRuntime(29862): java.lang.RuntimeException: An error occured while executing doInBackground()
06-30 00:46:08.898: E/AndroidRuntime(29862):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at java.lang.Thread.run(Thread.java:856)
06-30 00:46:08.898: E/AndroidRuntime(29862): Caused by: java.lang.NullPointerException
06-30 00:46:08.898: E/AndroidRuntime(29862):    at com.inc.automata.whatsonwindhoek.main.WoWEvents$GetContacts.doInBackground(WoWEvents.java:194)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at com.inc.automata.whatsonwindhoek.main.WoWEvents$GetContacts.doInBackground(WoWEvents.java:1)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-30 00:46:08.898: E/AndroidRuntime(29862):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-30 00:46:08.898: E/AndroidRuntime(29862):    ... 5 more
06-30 00:46:34.178: E/WindowManager(29862): Activity com.inc.automata.whatsonwindhoek.main.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419086d8 that was originally added here
06-30 00:46:34.178: E/WindowManager(29862): android.view.WindowLeaked: Activity com.inc.automata.whatsonwindhoek.main.MainActivity has 
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-30 00:46:34.178: E/WindowManager(29862):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
06-30 00:46:34.178: E/WindowManager(29862):     at android.view.View.measure(View.java:12899)
06-30 00:46:34.178: E/WindowManager(29862):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-30 00:46:34.178: E/WindowManager(29862):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-30 00:46:34.178: E/WindowManager(29862):     at android.view.View.measure(View.java:12899)
06-30 00:46:34.178: E/WindowManager(29862):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
06-30 00:46:34.178: E/WindowManager(29862):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
06-30 00:46:34.178: E/WindowManager(29862):     at android.view.View.measure(View.java:12899)
06-30 00:46:34.178: E/WindowManager(29862):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-30 00:46:34.178: E/WindowManager(29862):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-30 00:46:34.178: E/WindowManager(29862):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2249)
06-30 00:46:34.178: E/WindowManager(29862):     at android.view.View.measure(View.java:12899)
06-30 00:46:34.178: E/WindowManager(29862):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1179)
06-30 00:46:34.178: E/WindowManager(29862):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2565)
06-30 00:46:34.178: E/WindowManager(29862):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 00:46:34.178: E/WindowManager(29862):     at android.os.Looper.loop(Looper.java:137)
06-30 00:46:34.178: E/WindowManager(29862):     at android.app.ActivityThread.main(ActivityThread.java:4502)
06-30 00:46:34.178: E/WindowManager(29862):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 00:46:34.178: E/WindowManager(29862):     at java.lang.reflect.Method.invoke(Method.java:511)
06-30 00:46:34.178: E/WindowManager(29862):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
06-30 00:46:34.178: E/WindowManager(29862):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
06-30 00:46:34.178: E/WindowManager(29862):     at dalvik.system.NativeStart.main(Native Method)
</i>


  [1]: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
  [2]: http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

0 个答案:

没有答案