2016-06-28 3 views
-4

IAM с помощью меню раздвигающегося, этот код для вызова другого классаКак использовать Фрагмент в Android?

}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

+0

Сообщение об ошибке LogCat пожалуйста –

ответ

0

Попробуйте заменить эту линию,

StockProduct fragment = new StockProduct(); 

с

Fragment fragment = new StockProduct(); 

Убедитесь, что все операторы импорта поддержка импорта фрагмента

Смежные вопросы