ListFragment显示数据的问题

时间:2013-08-26 12:37:41

标签: android android-fragments android-listfragment

我有一个使用ListFragment显示所有者列表的类。当用户点击所有者时,它应该用一个列出所有者汽车的新片段替换底部片段。问题是,即使我为它创建了一个视图,它仍然会抛出异常找不到id 0x7f090013 的视图。我在这里做错了什么?

//这是我要调用的onListItemClick替换另一个片段(完整代码将在最后发布。)

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    //super.onListItemClick(l, v, position, id);
    int search = 0;
    Owner a = (Owner) getListView().getItemAtPosition(position);
    String name = a.getFirstName();
    String lName = a.getLastName();
    String Id = a.getCarId();

    FragmentManager mFragment = getChildFragmentManager();
    FragmentTransaction ft = mFragment.beginTransaction();
    CarDetail cDetail = new CarDetail();
    cDetail.SearchParameters(Integer.parseInt(Id));
    ft.replace(R.id.fCarDetail, cDetail);
    ft.addToBackStack(null);
    ft.commit();    
}

//这是汽车细节碎片

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CarDetail extends ListFragment {
   private ListView lCars;
   private ArrayList<Car> listCar = new ArrayList<Car>();
   private CarAdapter carAdapter;
    private int carId = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v1 = inflater.inflate(R.layout.customize_layout,container,false);
        lCars = (ListView) v1.findViewById(android.R.id.list);
        return v1;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lCars.setAdapter(carAdapter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        carAdapter = new CarAdapter(getActivity(), R.layout.car_detail, listCar);
        if(carId > 0)
        {
           new CarAsyncTask(carId).execute();
                     //As soon as it done with the on create, it throw the exception. 
        }
    }

    public void SearchParameters(int id) {
        carId = id;
    }




    private class CarAsyncTask extends AsyncTask<Integer, Void, List<Car>> {
        public int id =0;


        public CarAsyncTask(int carId) {
            id = carId;
        }

        @Override
        protected List<Car> doInBackground(Integer... params) {
            //List<Car> item = null;
            List<Car> item = new ArrayList<Car>();
            item.add(new Car("Chevy","Caprice","2002", 1));
            item.add(new Car("Chevy","Malibu","2014", 2));
            item.add(new Car("Dodge","Stratus","2002", 3));
            item.add(new Car("Saturn","L300","2004", 4));

            return item;
        }



        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            listCar.clear();
            carAdapter.clear();

        }

        @Override
        protected void onPostExecute(List<Car> result) {
            super.onPostExecute(result);
            for(Car c: result)
            {
                if(c.getID() == carId)
                {
                    carAdapter.add(c);
                    carAdapter.notifyDataSetChanged();
                }
            }
        }

    }

}

// car_detail.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/tvMake"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test" />

    <TextView
        android:id="@+id/tvModel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="make" />

    <TextView
        android:id="@+id/tvYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="year" />

     <TextView
        android:id="@+id/tvID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ID" />

</LinearLayout>
//the customize layout

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

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:background="#FFFF00"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#FF00FF"
               android:text="No data"/>
 </LinearLayout>
Exception:

08-22 21:00:21.051: E/AndroidRuntime(907): FATAL EXCEPTION: main
08-22 21:00:21.051: E/AndroidRuntime(907): java.lang.IllegalArgumentException: No view found for id 0x7f090013 (com.mb.carlovers:id/fCarDetail) for fragment CarDetail{40d97200 #0 id=0x7f090013}
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.os.Handler.handleCallback(Handler.java:725)
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.os.Looper.loop(Looper.java:137)
08-22 21:00:21.051: E/AndroidRuntime(907):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-22 21:00:21.051: E/AndroidRuntime(907):  at java.lang.reflect.Method.invokeNative(Native Method)
08-22 21:00:21.051: E/AndroidRuntime(907):  at java.lang.reflect.Method.invoke(Method.java:511)
08-22 21:00:21.051: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-22 21:00:21.051: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-22 21:00:21.051: E/AndroidRuntime(907):  at dalvik.system.NativeStart.main(Native Method)
08-22 21:00:21.172: W/ActivityManager(290):   Force finishing activity com.mb.carlovers/.People
08-22 21:00:21.231: W/WindowManager(290): Failure taking screenshot for (237x148) to layer 21015
08-22 21:00:21.631: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
08-22 21:00:21.751: W/ActivityManager(290): Activity pause timeout for ActivityRecord{40f05be8 u0 com.mb.carlovers/.People}
08-22 21:00:21.821: D/dalvikvm(290): GC_CONCURRENT freed 752K, 18% free 5916K/7192K, paused 74ms+104ms, total 515ms
08-22 21:00:31.265: W/ActivityManager(290): Launch timeout has expired, giving up

//以下是OwnerDetail代码

package com.mb.carlovers;
import java.util.ArrayList;
import java.util.List;

import com.mb.carlovers.adapter.OwnerAdapter;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class OwnerDetail extends ListFragment {
   private ListView mListView;
   private ArrayList<Owner> listItem = new ArrayList<Owner>();
   private OwnerAdapter ownerAdapter;
   private String[] searchParameter = null;

   public void SearchParameters(String[] parameters)
   {
       searchParameter = parameters;
   }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View v1 = inflater.inflate(R.layout.customize_layout,container, false);
        mListView = (ListView) v1.findViewById(android.R.id.list);
        return v1;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
        mListView.setAdapter(ownerAdapter);
      //  ownerAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ownerAdapter = new OwnerAdapter(getActivity(), R.layout.owner_detail, listItem);
        if(!(searchParameter == null))
        {
            //ownerAdapter.clear();
        //  ownerAdapter.addAll(listItem);
            new OnwerAsyncTask().execute();
            //ownerAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        //super.onListItemClick(l, v, position, id);
        int search = 0;
        Owner a = (Owner) getListView().getItemAtPosition(position);
        String name = a.getFirstName();
        String lName = a.getLastName();
        String Id = a.getCarId();

        FragmentManager mFragment = getChildFragmentManager();
        FragmentTransaction ft = mFragment.beginTransaction();
        CarDetail cDetail = new CarDetail();
        cDetail.SearchParameters(Integer.parseInt(Id));
        ft.replace(R.id.fCarDetail, cDetail);
        ft.addToBackStack(null);
        ft.commit();    
    }


    private class OnwerAsyncTask extends AsyncTask<Void, Void, List<Owner>> {
        @Override
        protected List<Owner> doInBackground(Void... params) {
            ArrayList<Owner> listItemLocal = new ArrayList<Owner>();
            try
            {
                Owner items = new Owner();
                items.setFirstName("John");
                items.setLastName("Smith");
                items.setCarId("1");
                listItemLocal.add(items);

                Owner item1 = new Owner();
                item1.setFirstName("Samantha");
                item1.setLastName("Right");
                item1.setCarId("2");
                listItemLocal.add(item1);

                Owner item2 = new Owner();
                item2.setFirstName("Regie");
                item2.setLastName("Miller");
                item2.setCarId("3");
                listItemLocal.add(item2);

                Owner item3 = new Owner();
                item3.setFirstName("Mark");
                item3.setLastName("Adam");
                item3.setCarId("4");
                listItemLocal.add(item3);


            } catch(Exception ex)
            {
                ex.toString();
            }
            return listItemLocal;
        }



        @Override
        protected void onPreExecute() {
            listItem.clear();
            ownerAdapter.clear();
            super.onPreExecute();
        }



        @Override
        protected void onPostExecute(List<Owner> result) {
            /*OwnerDetail  fragment = (OwnerDetail) mActivity.getSupportFragmentManager().findFragmentById(R.id.fOwnerDetail);
            fragment.SetAdapter(result);*/
            listItem = (ArrayList<Owner>) result;
            super.onPostExecute(result);
            if(listItem.size() > 0)
            {
                ownerAdapter.clear();
                ownerAdapter.addAll(listItem);
                ownerAdapter.notifyDataSetChanged();
                Log.i("Testing", "Added " + result.size() + "items to adapter.");
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

一遍又一遍地调试应用程序后,我终于找到了问题。在汽车类中,carId被声明为整数;但是,我调用set ID传递一个整数而不将其转换为CarAdapter中的字符串。我将ID更改为字符串,然后在进行Id比较之前将其转换回整数。有用。

//the piece of code below was causing the issue.
holder.tvId.setText(item.getID());