Checkbox从layoutinflater返回null

时间:2014-03-19 14:23:05

标签: android nullpointerexception

当我从自定义适配器中的inflater获取一个复选框时,它返回null,但是我得到的两个textview也不返回null。(我试着做system.out.println)这里是我的xml和适配器返回null:

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_with_checkbox, null);
    }
    HolderCheckbox holder = new HolderCheckbox();

    holder.txtTitle = (TextView)convertView.findViewById(R.id.txtTitle);
    holder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
    CheckBox cbox = (CheckBox)convertView.findViewById(R.id.checkBox);
    System.out.println(cbox)<--- this prints null
    holder.txtDescription = (TextView)convertView.findViewById(R.id.textDesc);

和我的xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    android:padding="10dp">
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:orientation="vertical">
     <TextView
         android:id="@+id/txtTitle"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:gravity="center_vertical"
         android:textColor="#C0C0C0"
         android:textSize="17sp" />

     <TextView
         android:id="@+id/textDesc"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:gravity="center_vertical"
         android:textColor="#808080"
         android:textSize="17sp" />
     </LinearLayout>
     <CheckBox
            android:id="@+id/checkBox"
            android:layout_gravity="right|center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="16dp"
            android:layout_centerHorizontal="true"
            android:focusable="false"
            android:focusableInTouchMode="false" >
        </CheckBox>
</RelativeLayout>

这是logcat:

    03-19 03:15:44.666: E/AndroidRuntime(27420): FATAL EXCEPTION: main
03-19 03:15:44.666: E/AndroidRuntime(27420): Process: com.codiaq.launcher.alpha, PID: 27420
03-19 03:15:44.666: E/AndroidRuntime(27420): java.lang.NullPointerException
03-19 03:15:44.666: E/AndroidRuntime(27420):    at com.codiaq.launcher.alpha.Preferences.SettingsHomeGridAdapter.getView(SettingsHomeGridAdapter.java:50)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.AbsListView.obtainView(AbsListView.java:2240)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.ListView.onMeasure(ListView.java:1175)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.View.measure(View.java:16504)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.View.measure(View.java:16504)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.View.measure(View.java:16504)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.View.measure(View.java:16504)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.View.measure(View.java:16504)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.View.measure(View.java:16504)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.Choreographer.doFrame(Choreographer.java:544)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.os.Handler.handleCallback(Handler.java:733)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.os.Handler.dispatchMessage(Handler.java:95)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.os.Looper.loop(Looper.java:136)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at android.app.ActivityThread.main(ActivityThread.java:5081)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at java.lang.reflect.Method.invoke(Method.java:515)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 03:15:44.666: E/AndroidRuntime(27420):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我想通了,这是因为我在创建适配器时声明了它,因此它不是null所以它没有用复选框来扩充视图