List View在measureHeightOfChildren上崩溃应用程序,java nullpointer异常

时间:2012-12-06 09:47:37

标签: android listview layout height adapter

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

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,但仍然没有运气。我没有任何想法

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

 return null;

为:

return convertView;

答案 2 :(得分:0)

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