源代码

public class VendingFragment extends ListFragment {

private String Tag = "VendingFragment";
private static final String TAG_SUCCESS = "success";

private ListView listView;

// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
JSONArray vendingmachine = null;

ArrayList<HashMap<String, String>> vendinglist;

// url to get all products list
MainActivity main = new MainActivity();
private String url_all_vendingmachine = main.getIP()
        + "vendingmachine/get_all_vendingmachine.php";

// products JSONArray

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.vending_main, container, false);
    vendinglist = new ArrayList<HashMap<String, String>>();
    //listView = (ListView) view.findViewById(R.id.allVendingListView);

    listView = getListView();
    new get_all_vendingmachine().execute();

    return view;
}

class get_all_vendingmachine extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        Log.i(Tag, "pDialog");
    }

    protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        Log.i(Tag, url_all_vendingmachine);
        // getting JSON Object
        // Note that create product url accepts POST method

        JSONObject json = jsonParser.makeHttpRequest(
                url_all_vendingmachine, "GET", params);

        // check log cat for response
        Log.i(Tag, json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // Ada record Data (SUCCESS = 1)
                // Getting Array of vendingmachine
                vendingmachine = json.getJSONArray("vendings");

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

                    // Storing each json item in variable
                    String id = c.getString("VMid");
                    String name = c.getString("Name");
                    String address = c.getString("Address");

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    map.put("VMid", id);
                    map.put("Name", name);
                    map.put("Address", address);

                    // adding HashList to ArrayList
                    vendinglist.add(map);
                }

            } else {
                // failed to create product
                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 once done
        pDialog.dismiss();
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // updating listview
                String[] from = { "name", "address", "id" };                    int[] to = { R.id.vending_name, R.id.vending_address,
                        R.id.vending_id };

                SimpleAdapter adapter = new SimpleAdapter(getActivity(),
                        vendinglist, R.layout.vending_list, from, to);
                listView.setAdapter(adapter);
            }
        });
    }

}

}


我运行,并在应用程序启动后在logcat中发现错误,logcat中没有显示log.i

错误消息:

    03-15 18:21:48.680: W/dalvikvm(13868): threadid=1: thread exiting with uncaught exception (group=0x40cf12d0)
03-15 18:21:48.710: E/AndroidRuntime(13868): FATAL EXCEPTION: main
03-15 18:21:48.710: E/AndroidRuntime(13868): java.lang.IllegalStateException: Content view not yet created
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.app.ListFragment.ensureList(ListFragment.java:386)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.app.ListFragment.getListView(ListFragment.java:280)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at com.ecnu.vendingmachine.VendingFragment.onCreateView(VendingFragment.java:54)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.app.BackStackRecord.run(BackStackRecord.java:622)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.os.Handler.handleCallback(Handler.java:605)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.os.Looper.loop(Looper.java:137)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at android.app.ActivityThread.main(ActivityThread.java:4456)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at java.lang.reflect.Method.invoke(Method.java:511)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
03-15 18:21:48.710: E/AndroidRuntime(13868):    at dalvik.system.NativeStart.main(Native Method)


这是我将listview放入片段的地方。现在我需要将其扩展为使用viewpager,在该片段中有一个viewpaper和其中的三个片段。人们可以水平滑动它,然后选择帧显示到屏幕上。

每个片段都包含自己的listview并从mysql获取数据,任何人都可以教我如何编码...我对此非常困惑。

最佳答案

onCreateView必须仅返回代表片段的视图。 View上的其他操作应在另一个回调中执行,您的代码必须如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.vending_main, container, false);
}

 @Override
 public void onViewCreated (View view, Bundle savedInstanceState) {

    vendinglist = new ArrayList<HashMap<String, String>>();
    listView = (ListView) view.findViewById(R.id.allVendingListView);

    listView = getListView();
    new get_all_vendingmachine().execute();
}

关于android - 无法使用JSON从MySQL检索数据并将其放入带有 fragment 的Lis​​tView中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22422186/

10-10 00:46