-1

==================这是我的片段:

public class ChooseAssigmentFragment extends Fragment {
private ListArrayAdapter listArrayAdapter;

private Assigment assigment;

private Assigments assigments;

private ListView assigList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.assigments, null);
    init(v);
    assigments = new Assigments();
    assigment = new Assigment("Nikon D700", "aisle 5, range 1m");
    assigments.add(assigment); // daca comentez linia, atuncia functioneaza assigment.name si assigment.details nu is nule
    loadAssigments(assigments);

    return v;
}

private void init(View v) {
    // TODO Auto-generated method stub
    EditText assigSearchET = (EditText) v.findViewById(R.id.assigSearchET);
    ImageButton assigSearcthIB = (ImageButton) v.findViewById(R.id.assigSearchIB);
    assigList = (ListView) v.findViewById(R.id.assigListView);

    assigList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> assigList, View v, int position, long id) {
            Assigment assigment = ((ListArrayAdapter) assigList.getAdapter()).getItem(position);
            LogService.log(getTag(), "position: " + assigList.getAdapter().getItem(position));

        }
    });

}

private void loadAssigments(Assigments assigments) {

    listArrayAdapter = new ListArrayAdapter(ChooseAssigmentFragment.this, assigments);

    listArrayAdapter.notifyDataSetChanged();
    assigList.setAdapter(listArrayAdapter);
}

}

========================这是我的 ListView 适配器:

public class ListArrayAdapter  extends BaseAdapter implements Filterable {
private final LayoutInflater mInflater;
private ChooseAssigmentFragment chooseAssigmentFragment;
private Assigments assigments;
private Object mFilter;

public ListArrayAdapter(ChooseAssigmentFragment chooseAssigmentFragment, Assigments assigments) {
    this.chooseAssigmentFragment = chooseAssigmentFragment;
    this.assigments = assigments;
    mInflater = LayoutInflater.from(chooseAssigmentFragment.getActivity());
}


public int getCount() {
    return assigments.size();
}

public Assigment getItem(int position) {
    return assigments.get(position);
}

public long getItemId(int position) {
    return position;
}

public Assigment getAssigments(int i) {
    return assigments.get(i);
}

public Filter getFilter() {
    if (mFilter == null) {
        mFilter = new ItemFilter();
    }
    return (Filter) mFilter;
}


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

    ListContent holder = null;

    Assigment assigment = getAssigments(position);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row_layout_main_fragment, null);

        holder = new ListContent();
        holder.assigName = (TextView) convertView.findViewById(R.id.listItemName);
        holder.assigDetails = (TextView) convertView.findViewById(R.id.listItemDetails);

        convertView.setTag(holder);
    } else {
        holder = (ListContent) convertView.getTag();

    }
    holder.assigName.setText(assigment.name);
    holder.assigDetails.setText(assigment.details);

    return null;
}
private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence prefix) {
        return null;

    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

    }
}

class ListContent {
    TextView assigName;
    TextView assigDetails;

}
}

=================我的 Inflater xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:paddingLeft="5dp" >

<TextView android:id="@+id/listItemName" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:text="@+id/listItemName" android:textSize="18dp"
    android:lineSpacingExtra="15dp" android:textStyle="bold"
    android:textColor="@color/Black">
</TextView>

<TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
    android:layout_height="1dp">
</TextView>

<TextView android:id="@+id/listItemDetails" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:text="@+id/listItemDetails" android:textSize="14dp"
    android:lineSpacingExtra="15dp"
    android:textColor="@color/DarkGray">
</TextView>

</LinearLayout>

====================我的xml片段文件:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/RelativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@color/SlateGray"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:text="@string/sensor_conf" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/RelativeLayout2"
    android:weightSum="8" >

    <EditText
        android:id="@+id/assigSearchET"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="7" >

        <requestFocus />
    </EditText>

    <ImageButton
        android:id="@+id/assigSearchIB"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.99"
        android:background="@color/white"
        android:src="@drawable/ic_action_search" />
</LinearLayout>

<ListView
    android:id="@+id/assigListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayout1"
    android:layout_centerHorizontal="true" >
</ListView>

</RelativeLayout>

=================这就是我的Logcat给我的:

12-06 11:35:54.156: E/AndroidRuntime(26171): FATAL EXCEPTION: main
12-06 11:35:54.156: E/AndroidRuntime(26171): java.lang.NullPointerException
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.AbsListView.obtainView(AbsListView.java:2048)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.ListView.onMeasure(ListView.java:1127)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.View.measure(View.java:8459)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.View.measure(View.java:8459)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.View.measure(View.java:8459)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.View.measure(View.java:8459)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.View.measure(View.java:8459)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.View.measure(View.java:8459)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.View.measure(View.java:8459)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.View.measure(View.java:8459)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.ViewRoot.performTraversals(ViewRoot.java:864)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1902)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.os.Looper.loop(Looper.java:130)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at android.app.ActivityThread.main(ActivityThread.java:3744)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at java.lang.reflect.Method.invokeNative(Native Method)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at java.lang.reflect.Method.invoke(Method.java:507)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-06 11:35:54.156: E/AndroidRuntime(26171):    at dalvik.system.NativeStart.main(Native Method)

我尝试更改片段 xml,只包含列表,尝试更改膨胀的 xml,但仍然没有运气。我没有任何想法

4

3 回答 3

6

您的适配器的 getView() 返回 null。您应该返回 convertView:

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

    ListContent holder = null;

    Assigment assigment = getAssigments(position);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row_layout_main_fragment, null);

        holder = new ListContent();
        holder.assigName = (TextView) convertView.findViewById(R.id.listItemName);
        holder.assigDetails = (TextView) convertView.findViewById(R.id.listItemDetails);

        convertView.setTag(holder);
    } else {
        holder = (ListContent) convertView.getTag();

    }
    holder.assigName.setText(assigment.name);
    holder.assigDetails.setText(assigment.details);

    return convertView;
}
于 2012-12-06T09:56:16.937 回答
0

在我的 getView 方法的适配器中,我忘了更改:

 return null;

到:

return convertView;
于 2012-12-06T09:56:14.287 回答
0

你返回空值;请尝试更改并返回 View 对象。

于 2015-05-17T09:57:29.530 回答