ListView自定义适配器XML Inflater NullPointerException

时间:2011-07-10 18:10:03

标签: android listview nullpointerexception

我正在尝试使用XML Inflater来使用自定义适配器,但是我得到了NullPointerException。

这是课程:

public class ListView_Start_Quran_Adapter2 extends BaseAdapter {
private String[]            SuraNames;
private int[]               PageNumber;
Context                     mContext;
private LayoutInflater mInflater;

public ListView_Start_Quran_Adapter2(Context context, String[] SuraNames, int[] PageNumber) {
    mContext        = context;
    this.SuraNames  = SuraNames;
    this.PageNumber = PageNumber;
}

@Override
public int getCount() {
    return SuraNames.length;
}

@Override
public Object getItem(int arg0) {
    return SuraNames[arg0];
}

@Override
public long getItemId(int arg0) {
    return PageNumber[arg0];
}

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

    if(convertView == null) {
        convertView = mInflater.inflate(R.layout.start_row, parent,false); //line 47
        holder=new ViewHolder();

        holder.tv_SuraName   =(TextView)convertView.findViewById(R.id.Start_Name);
        holder.tv_PageNumber = (TextView)convertView.findViewById(R.id.Start_Numbering);
        convertView.setTag(holder);
    } else {
        holder  = (ViewHolder) convertView.getTag();
    }

    holder.tv_SuraName.setText(SuraNames[position]);
    holder.tv_PageNumber.setText(Integer.toString(PageNumber[position]));

    return convertView;
}

class ViewHolder {
    TextView       tv_SuraName;
    TextView       tv_PageNumber;
}

}

logcat的:

07-10 20:56:31.643: ERROR/AndroidRuntime(1464): FATAL EXCEPTION: main
07-10 20:56:31.643: ERROR/AndroidRuntime(1464): java.lang.NullPointerException
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at omar.quran1.ListView_Start_Quran_Adapter2.getView(ListView_Start_Quran_Adapter2.java:47)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.AbsListView.obtainView(AbsListView.java:1560)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1265)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.ListView.onMeasure(ListView.java:1176)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.View.measure(View.java:8366)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.os.Looper.loop(Looper.java:130)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at android.app.ActivityThread.main(ActivityThread.java:3687)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at java.lang.reflect.Method.invoke(Method.java:507)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-10 20:56:31.643: ERROR/AndroidRuntime(1464):     at dalvik.system.NativeStart.main(Native Method)

start_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:id="@+id/relativeLayout1" android:layout_height="wrap_content">
    <TextView
    android:layout_alignParentLeft="true"
    android:id="@+id/Start_Numbering"         
    android:textSize="19.5sp"              
    android:layout_width="wrap_content"         
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>
    <TextView 
    android:textStyle="bold"
    android:layout_alignParentRight="true"
    android:id="@+id/Start_Name"   
    android:textColor="#808080"      
    android:textSize="21sp"              
    android:layout_width="fill_parent"         
    android:layout_height="wrap_content"/>

    </RelativeLayout>

我推销第47行(崩溃的原因)。谁能告诉我如何解决这个问题?感谢。

2 个答案:

答案 0 :(得分:1)

您没有初始化LayoutInflater对象,这就是您获得NullPouinterException的原因。在构造函数中执行此操作:

mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

答案 1 :(得分:0)

而不是false,尝试null并告诉我它是否有效。如果没有,我还有另一条线索。

我的意思是:

convertView = mInflater.inflate(R.layout.start_row, null);

克里斯蒂安说得对。试试这个:

mInflater = LayoutInflater.from(context);