自定义ListView有什么问题?

时间:2016-06-22 20:26:11

标签: android listview android-fragments android-view

启动我的应用时,我在logcat中遇到此错误:

  

处理:pc.dd.fragment_animation,PID:25093                                                                             java.lang.RuntimeException:无法启动活动ComponentInfo {pc.dd.fragment_animation / pc.dd.fragment_animation.MainClass}:android.view.InflateException:二进制XML文件行#8:二进制XML文件行#8:错误膨胀类片段

这是我在主要活动中的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
    android:id="@+id/listFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="pc.dd.fragment_animation.newFragment"
    />
</LinearLayout>

这是我的自定义适配器中的孩子:

public class myListAdapter  extends ArrayAdapter<Integer> {


public myListAdapter(Context context, int resource, Integer[] objects) {
    super(context, resource, objects);
}

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

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.fragment_listview, null);
        }

        Integer objects = getItem(position);

        if (objects != null) {
            ImageView tt1 = (ImageView) v.findViewById(R.id._fragmentImage);
            TextView tt2 = (TextView) v.findViewById(R.id._fragmentNickanme);
            TextView tt3 = (TextView) v.findViewById(R.id._fragmentDescription);

            if (tt1 != null) {
                tt1.setImageResource(objects);
            }

        }

        return v;
    }}

最后,这是我的片段,我尝试做自定义ListView:

public class newFragment extends Fragment {
Integer[] items = new Integer[]{R.drawable.image1,R.drawable.picture};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.fragment_listview, container, false);
    ListView myListView = (ListView)view.findViewById(R.id.listFragment);
    myListAdapter adapter =
            new myListAdapter(getActivity(), android.R.layout.simple_list_item_1, items);
    myListView.setAdapter( adapter);
    return view;
}}

注意:R.id.listFragment位于main_activity.xml。 在代码fragment_listview下面:

<?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">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/_fragmentImage"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/picture"
        app:civ_border_width="2dp"
        app:civ_border_color="#efefef"
        android:layout_margin="6dp" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/nickname"
            android:id="@+id/_fragmentNickanme"
            android:textSize="8pt"
            android:layout_margin="4dp"
            android:layout_gravity="top"
            android:textColor="#282828" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/string_small_text"
            android:id="@+id/_fragmentDescription"
            android:textSize="6pt"
            android:gravity="bottom"
            android:layout_margin="4dp" />
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

是的,我只是在我的主要课程中这样做:

<li>

并删除 ListView myListView = (ListView)findViewById(R.id.listMainAct); myListAdapter adapter = new myListAdapter(this, android.R.layout.simple_list_item_1, items); myListView.setAdapter( adapter); 类,thx cricket_007 以获取提示:)

相关问题