如何在Android中使用Fragment?

时间:2016-06-28 02:32:02

标签: android android-fragments

iam使用菜单滑动,此代码用于调用其他类

  }else if (id == R.id.list) {
        //Set the fragment initially
        StockProduct fragment = new StockProduct();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }

fragment错误我不知道为什么...... 这是我的班级代码

public class StockProduct extends Fragment implements ListView.OnItemClickListener {

String myJSON;

private static final String TAG_RESULTS = "result";
private static final String TAG_NAME = "name_product";
private static final String TAG_ID = "product_id";

JSONArray peoples = null;

ArrayList<HashMap<String, String>> personList;

ListView list;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.list_product, container, false);
    list = (ListView) view.findViewById(R.id.allproduct);
    personList = new ArrayList<HashMap<String, String>>();
    getData();

    return view;
}


public void getData() {
    class GetDataJSON extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost(Config.ALLPRODUCT_URL);

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }


    }
}

protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);

            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);

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

            persons.put(TAG_ID, id);
            persons.put(TAG_NAME, name);

            personList.add(persons);
        }

        ListAdapter adapter = new SimpleAdapter(
                StockProduct.this.getActivity(), personList, R.layout.list_product_component,
                new String[]{TAG_ID,TAG_NAME},
                new int[]{R.id.idproduct, R.id.nameproduct}
        );

        list.setAdapter(adapter);

    } catch (JSONException e) {
        e.printStackTrace();
    }

}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this.getActivity(), StockProductView.class);
    HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
    String product_id = map.get(TAG_ID).toString();
    intent.putExtra(Config.TAG_PRODUCT_ID,product_id);
    startActivity(intent);
}}

and this is log error

1 个答案:

答案 0 :(得分:0)

尝试替换此行,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="items"></p>
<button id="next">Next</button>

 StockProduct fragment = new StockProduct();

确保所有导入语句都导入支持片段