2014-11-05 3 views
0

не в состоянии отобразить список в фрагменте .. Ошибка:Дисплей ListView фрагментарно

11-05 11:57:32.987: E/AndroidRuntime(9058): FATAL EXCEPTION: main 
11-05 11:57:32.987: E/AndroidRuntime(9058): Process: com.example.mysqltest, PID: 9058 
11-05 11:57:32.987: E/AndroidRuntime(9058): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysqltest/com.example.jobtracker.ReadComments}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.ActivityThread.access$800(ActivityThread.java:144) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.os.Looper.loop(Looper.java:212) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.ActivityThread.main(ActivityThread.java:5135) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at java.lang.reflect.Method.invoke(Method.java:515) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at dalvik.system.NativeStart.main(Native Method) 
11-05 11:57:32.987: E/AndroidRuntime(9058): Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.ListFragment.ensureList(ListFragment.java:402) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.ListFragment.onViewCreated(ListFragment.java:203) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.BackStackRecord.run(BackStackRecord.java:684) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.Activity.performStart(Activity.java:5240) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210) 
11-05 11:57:32.987: E/AndroidRuntime(9058):  ... 11 more 

это fragment_Jobqueue.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="100dp" 
     android:text="Job Queue" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textStyle="bold" /> 

    <ListView 
     android:id="@+id/listViewjobqueue" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:dividerHeight="0.1dp" 
     android:divider="#0000CC"> 

    </ListView> 

</LinearLayout> 

displayList.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:paddingTop="6dip" 
     android:paddingLeft="15dip" 
     android:textSize="16sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/quantity" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="4dip" 
     android:paddingLeft="15dip"/> 

</LinearLayout> 

JobQueueFragment.java

public class JobqueueFragment extends ListFragment { 

    public JobqueueFragment(){} 

    private static final String JOBQUEUE_URL = "http://192.168.2.102/webservice/jobqueue.php"; 

    private ProgressDialog pDialog; 

    // JSON parser class 
    JSONArray jsonParser = null; 

    private ListView mainListView; 

    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_MESSAGE = "msg"; 
    private static final String TAG_CONTACTS = "message"; 
    private static final String TAG_NAME = "ClientName"; 
    private static final String TAG_QUANTITY = "Quantity"; 


    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 
    Context ctx; 

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

     ctx = inflater.getContext();//.getApplicationContext(); 
     mainListView = (ListView)getActivity().findViewById(R.id.listViewjobqueue); 
     new JobQueue().execute(); 

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

    class JobQueue extends AsyncTask<Void, Void, Void> { 

     boolean failure = false; 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      // Creating service handler class instance 
      ServiceHandler sh = new ServiceHandler(); 

      // Making a request to url and getting response 
      String jsonStr1 = sh.makeServiceCall(JOBQUEUE_URL, ServiceHandler.GET); 

      String[] separated = jsonStr1.split("#4"); 

      String jsonStr = separated[1].replace('"', '\"'); 

      Log.d("Response: ", "> " + jsonStr); 

      if (jsonStr != null) { 
       try { 
        JSONObject jsonObj = new JSONObject(jsonStr); 

        // Getting JSON Array node 
        jsonParser = jsonObj.getJSONArray(TAG_CONTACTS); 

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

         String name = c.getString(TAG_NAME); 
         StringBuffer qty = new StringBuffer(c.getString(TAG_QUANTITY)); 
         String quantity = new String(qty.append(" images")); 

         // tmp hashmap for single contact 
         HashMap<String, String> contact = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 

         contact.put(TAG_NAME, name); 
         contact.put(TAG_QUANTITY, quantity); 

         // adding contact to contact list 
         contactList.add(contact); 

         ListAdapter adapter = new SimpleAdapter(ctx, contactList, 
           R.layout.displaylist, new String[] { TAG_NAME, TAG_QUANTITY 
         }, new int[] { R.id.name, 
         R.id.quantity}); 

         // updating listview 
         setListAdapter(adapter); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      } 

      return null; 
     } 
    } 

ответ

0
use this fragment_Jobqueue.xml: 
<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:dividerHeight="0.1dp" 
    android:divider="#0000CC"> 

</ListView> 

братан прикрепить этот код в на резюме

JobQueueFragment.java 


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


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

@Override 
public void onResume() { 
    ctx = getActivity(); 
    mainListView = getListView(); 
    new JobQueue().execute(); 
    super.onResume(); 
} 
+0

Я только что начал с android ... я сделал изменения, все еще получая ошибку ... могу ли я прикрепить код к какой-то причине ... – user3479916

+0

изменить свой метод onCreateView(), как указано выше, редактировать –

+0

Нет, не работает .. – user3479916

0

Изменить этот код

public class JobqueueFragment extends Fragment { 

    public JobqueueFragment(){ 
    } 

    private static final String JOBQUEUE_URL = "http://192.168.2.102/webservice/jobqueue.php"; 

    private ProgressDialog pDialog; 

    // JSON parser class 
    JSONArray jsonParser = null; 

    private ListView mainListView; 

    private static final String TAG_SUCCESS = "succes public class JobqueueFragment extends ListFragment { 

    public JobqueueFragment(){} 

    private static final String JOBQUEUE_URL = "http://192.168.2.102/webservice/jobqueue.php"; 

    private ProgressDialog pDialog; 

    // JSON parser class 
    JSONArray jsonParser = null; 

    private ListView mainListView; 

    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_MESSAGE = "msg"; 
    private static final String TAG_CONTACTS = "message"; 
    private static final String TAG_NAME = "ClientName"; 
    private static final String TAG_QUANTITY = "Quantity"; 


    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 
    Context ctx; 

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

     ctx = inflater.getContext();//.getApplicationContext(); 
     View view = inflater.inflate(R.layout.fragment_jobqueue, container, false) 
     mainListView = (ListView) view.findViewById(R.id.listViewjobqueue); 
     new JobQueue().execute(); 

     return view; 
    } 

    class JobQueue extends AsyncTask<Void, Void, Void> { 

     boolean failure = false; 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      // Creating service handler class instance 
      ServiceHandler sh = new ServiceHandler(); 

      // Making a request to url and getting response 
      String jsonStr1 = sh.makeServiceCall(JOBQUEUE_URL, ServiceHandler.GET); 

      String[] separated = jsonStr1.split("#4"); 

      String jsonStr = separated[1].replace('"', '\"'); 

      Log.d("Response: ", "> " + jsonStr); 

      if (jsonStr != null) { 
       try { 
        JSONObject jsonObj = new JSONObject(jsonStr); 

        // Getting JSON Array node 
        jsonParser = jsonObj.getJSONArray(TAG_CONTACTS); 

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

         String name = c.getString(TAG_NAME); 
         StringBuffer qty = new StringBuffer(c.getString(TAG_QUANTITY)); 
         String quantity = new String(qty.append(" images")); 

         // tmp hashmap for single contact 
         HashMap<String, String> contact = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 

         contact.put(TAG_NAME, name); 
         contact.put(TAG_QUANTITY, quantity); 

         // adding contact to contact list 
         contactList.add(contact); 

         ListAdapter adapter = new SimpleAdapter(ctx, contactList, 
           R.layout.displaylist, new String[] { TAG_NAME, TAG_QUANTITY 
         }, new int[] { R.id.name, 
         R.id.quantity}); 

         // updating listview 
         setListAdapter(adapter); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      } 

      return null; 
     } 
    } 
+0

Если я изменить его фрагмент, то setListAdapter даст ошибку говоря – user3479916

+0

метод не определен для этого фрагмента – user3479916

+0

измените его с помощью mainListView.setAdapter (адаптер); –

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