无法使用Json在片段上填充ListView

时间:2014-06-06 07:15:04

标签: android-fragments android-listview

你好!我正在开发一个应用程序,我需要在其中填充来自Mysql数据库的ListView。这个填充的列表必须显示在片段上。

public class AppsTabFragment extends ListFragment
{
private ProgressDialog pdialog;
ListView listView;
JSONParser jparser = new JSONParser();
ArrayList<HashMap<String,String>> appslist;
public String url_all_apps="http://10.0.2.2/sisoft/get_all_apps.php";
 // JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_APPS = "appname";
private static final String TAG_PID = "id";
private static final String TAG_NAME = "appname";
MainActivity main=new MainActivity();
// products JSONArray
JSONArray apps = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {        
    View view = inflater.inflate(R.layout.apps_tab_fragment, container, false); 
    listView = (ListView) view.findViewById(android.R.id.list);
    listView = getListView();
    return view;
}
@Override
public void onViewCreated(View view,Bundle SavedInstanceState)
{
    appslist = new ArrayList<HashMap<String,String>>();

    new loadAllApps().execute();
}
class loadAllApps extends AsyncTask<String, String, String>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdialog = new ProgressDialog(getActivity());
        pdialog.setMessage("Loading apps. Please wait...");
        pdialog.setIndeterminate(false);
        pdialog.setCancelable(true);
        pdialog.show();
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jparser.makeHttpRequest(url_all_apps, "GET", params);

        // Check your log cat for JSON response
        Log.d("All apps: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                apps = json.getJSONArray(TAG_APPS);

                // looping through All Products
                for (int i = 0; i < apps.length(); i++) {
                    JSONObject c = apps.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    // adding HashList to ArrayList
                    appslist.add(map);
                }
            } else {
                // no apps found
                // Launch Add New product Activity
                getActivity().finish();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
     /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pdialog.dismiss();
        // updating UI from Background Thread
       getActivity().runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        getActivity(), appslist,
                        R.layout.list_apps, new String[] { TAG_PID,
                                TAG_NAME},
                        new int[] { R.id.id, R.id.appname });
                // updating listview
               listView.setAdapter(adapter);
            }
        });

    }

}
  }

这是我的LogCat显示错误。

06-06 06:12:43.834: D/dalvikvm(862): GC_CONCURRENT freed 72K, 8% free 2781K/2996K, paused 14ms+22ms, total 172ms
06-06 06:12:44.344: D/AndroidRuntime(862): Shutting down VM
06-06 06:12:44.344: W/dalvikvm(862): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-06 06:12:44.454: E/AndroidRuntime(862): FATAL EXCEPTION: main
06-06 06:12:44.454: E/AndroidRuntime(862): java.lang.IllegalStateException: Content view not yet created
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
06-06 06:12:44.454: E/AndroidRuntime(862):  at com.sisoft.sisoftstore.fragment.AppsTabFragment.onCreateView(AppsTabFragment.java:45)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.View.measure(View.java:15518)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.View.measure(View.java:15518)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.View.measure(View.java:15518)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-06 06:12:44.454: E/AndroidRuntime(862):  at   android.view.View.measure(View.java:15518)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-06 06:12:44.454: E/AndroidRuntime(862):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.View.measure(View.java:15518)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.Choreographer.doFrame(Choreographer.java:532)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.os.Handler.handleCallback(Handler.java:725)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.os.Looper.loop(Looper.java:137)
06-06 06:12:44.454: E/AndroidRuntime(862):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-06 06:12:44.454: E/AndroidRuntime(862):  at java.lang.reflect.Method.invokeNative(Native Method)
06-06 06:12:44.454: E/AndroidRuntime(862):  at java.lang.reflect.Method.invoke(Method.java:511)
06-06 06:12:44.454: E/AndroidRuntime(862):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-06 06:12:44.454: E/AndroidRuntime(862):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-06 06:12:44.454: E/AndroidRuntime(862):  at dalvik.system.NativeStart.main(Native Method)
06-06 06:17:44.713: I/Process(862): Sending signal. PID: 862 SIG: 9

它在第listView = getListView();行显示错误。但是当我删除这一行时,它会显示如下错误。

06-06 07:04:06.705: E/AndroidRuntime(1753): java.lang.RuntimeException: Your content  must have a ListView whose id attribute is 'android.R.id.list'

任何人都可以帮我解决这个问题吗?如果有人帮助我解决这个问题,我将非常感激。

1 个答案:

答案 0 :(得分:0)

在您的xml中,将列表ID设置为

@android:id/list

然后使用:

listView = getListView();

listView = (ListView) view.findViewById(android.R.id.list);