为Listview创建自定义片段无法正常工作

时间:2014-06-11 11:59:14

标签: android android-layout listview android-fragments android-listview

大家好我正在尝试使用片段创建自定义查看Listview。我收到错误如下

06-11 17:01:51.251: E/AndroidRuntime(5371): FATAL EXCEPTION: main
06-11 17:01:51.251: E/AndroidRuntime(5371): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at com.pavan.mylistview.CustomAdapter.getView(CustomAdapter.java:55)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.AbsListView.obtainView(AbsListView.java:1592)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.ListView.makeAndAddView(ListView.java:1782)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.ListView.fillDown(ListView.java:705)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.ListView.fillFromTop(ListView.java:762)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.ListView.layoutChildren(ListView.java:1633)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.AbsListView.onLayout(AbsListView.java:1422)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.View.layout(View.java:7175)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.View.layout(View.java:7175)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.View.layout(View.java:7175)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.View.layout(View.java:7175)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.View.layout(View.java:7175)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.os.Looper.loop(Looper.java:130)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at java.lang.reflect.Method.invoke(Method.java:507)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at dalvik.system.NativeStart.main(Native Method)
06-11 17:01:51.251: E/AndroidRuntime(5371): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f050042, tag null, or parent id 0x0 with another fragment for com.pavan.mylistview.ExtendedRelativeLayout
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
06-11 17:01:51.251: E/AndroidRuntime(5371):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
06-11 17:01:51.251: E/AndroidRuntime(5371):     ... 33 more

以下是我的android listview的代码

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.mylistview.ExtendedFragmentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/t2">
</fragment>

activity_main.xml中

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

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
     />
</LinearLayout>

CustomAdapter.java

public View getView(int position, View convertView, ViewGroup parent) {

  ViewHolder holder = null;

  LayoutInflater mInflater = (LayoutInflater) context
    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
   convertView = mInflater.inflate(R.layout.listitem, null);
  return convertView;
 }

ExtendedFragmentLayout.java

public class ExtendedFragmentLayout extends Fragment
{

    Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup view,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, view,savedInstanceState);

         TextView textName = new TextView(context);
        textName.setText("Text1");
        textName.setTextSize(18);
        ViewGroup.MarginLayoutParams textNameParams = new ViewGroup.MarginLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        textNameParams.leftMargin = 90;
        textNameParams.topMargin = 15;


        TextView dateDisplay = new TextView(context);
        dateDisplay.setText("Today");
        dateDisplay.setTextSize(14);
        ViewGroup.MarginLayoutParams dateDisplayParams = new ViewGroup.MarginLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        dateDisplayParams.leftMargin = 250;
        dateDisplayParams.topMargin = 8;

        RelativeLayout layout1 = new RelativeLayout(context);
        layout1.addView(textNameParams , textNameParams );
        layout1.addView(dateDisplay, dateDisplayParams);

return layout1;
}

我在使用此自定义片段时遇到错误,我将我的MainActivity类作为

0 个答案:

没有答案