带有自定义数组适配器的 NullPointerException

NullPointerException with Custom array adapter

我正在为这个错误而苦苦挣扎:我创建了一个自定义数组适配器,当我尝试访问我用 findviewbyid 初始化的文本视图时,getView 方法出现空指针异常:

  private class MarkerArrayAdapter extends ArrayAdapter<MarkerWrapper> {
    private ArrayList<MarkerWrapper> markerList;

    public MarkerArrayAdapter(Context context, int textViewResourceId,
            ArrayList<MarkerWrapper> markersList) {
        super(context, textViewResourceId, markersList);
        this.markerList = markersList;
    }

    private class ViewHolder {
        TextView ID_marker;
        TextView chr;
        TextView position;
        TextView risk_allele;
        TextView ref_allele;
        TextView genome_patient;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        // Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.raw_data_item, null);

            holder = new ViewHolder();
            holder.ID_marker = (TextView) findViewById(R.id.raw_data_report_textview_1);
            holder.chr = (TextView) findViewById(R.id.raw_data_report_textview_2);
            holder.position = (TextView) findViewById(R.id.raw_data_report_textview_3);
            holder.risk_allele = (TextView) findViewById(R.id.raw_data_report_textview_4);
            holder.ref_allele = (TextView) findViewById(R.id.raw_data_report_textview_5);
            holder.genome_patient = (TextView) findViewById(R.id.raw_data_report_textview_6);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MarkerWrapper marker = markerList.get(position);

        //NullPointer on this line
        holder.ID_marker.setText(marker.getID_marker());
        holder.chr.setText(marker.getChr());
        holder.position.setText(marker.getPosition());
        holder.risk_allele.setText(marker.getRisk_allele());
        holder.ref_allele.setText(marker.getRef_allele());
        holder.genome_patient.setText(marker.getGenome_patient());

        return convertView;

    }

} 

我的 raw_data_item.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="6dip" >

<TextView
    android:id="@+id/raw_data_report_textview_1"
    android:layout_width="22dp"
    android:layout_height="fill_parent"
    android:layout_weight="0.90"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/raw_data_report_textview_2"
    android:layout_width="14dp"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/raw_data_report_textview_3"
    android:layout_width="38dp"
    android:layout_height="fill_parent"
    android:layout_weight="0.35"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/raw_data_report_textview_4"
    android:layout_width="36dp"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/raw_data_report_textview_5"
    android:layout_width="44dp"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/raw_data_report_textview_6"
    android:layout_width="85dp"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceSmall" />

我看到很多人遇到同样的问题,但是 none 他们的回答对我有用,所以我非常感谢你的帮助。

04-19 11:05:03.657: E/AndroidRuntime(2229): FATAL EXCEPTION: main
04-19 11:05:03.657: E/AndroidRuntime(2229): Process:  PID: 2229
04-19 11:05:03.657: E/AndroidRuntime(2229): java.lang.NullPointerException:                     Attempt to invoke virtual method 'void     android.widget.TextView.setText(java.lang.CharSequence)' on a null object     reference
04-19 11:05:03.657: E/AndroidRuntime(2229):     at RawDataReportActivity$MarkerArrayAdapter.getView(RawDataReportActivity.java:162)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.AbsListView.obtainView(AbsListView.java:2842)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.ListView.makeAndAddView(ListView.java:1884)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.ListView.fillDown(ListView.java:713)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.ListView.fillFromTop(ListView.java:779)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.ListView.layoutChildren(ListView.java:1693)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.AbsListView.onLayout(AbsListView.java:2632)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.View.layout(View.java:16550)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewGroup.layout(ViewGroup.java:5303)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.View.layout(View.java:16550)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewGroup.layout(ViewGroup.java:5303)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.View.layout(View.java:16550)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewGroup.layout(ViewGroup.java:5303)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:801)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.View.layout(View.java:16550)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewGroup.layout(ViewGroup.java:5303)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.View.layout(View.java:16550)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewGroup.layout(ViewGroup.java:5303)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.View.layout(View.java:16550)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewGroup.layout(ViewGroup.java:5303)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.View.layout(View.java:16550)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewGroup.layout(ViewGroup.java:5303)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2308)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2021)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.Choreographer.doCallbacks(Choreographer.java:590)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.Choreographer.doFrame(Choreographer.java:560)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.os.Handler.handleCallback(Handler.java:739)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.os.Looper.loop(Looper.java:145)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at android.app.ActivityThread.main(ActivityThread.java:5832)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at java.lang.reflect.Method.invoke(Native Method)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at java.lang.reflect.Method.invoke(Method.java:372)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
04-19 11:05:03.657: E/AndroidRuntime(2229):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

您必须添加 convertview.findViewById(.... )

 holder.ID_marker = (TextView) convertView.findViewById(R.id.raw_data_report_textview_1);
            holder.chr = (TextView) convertView.findViewById(R.id.raw_data_report_textview_2);
            holder.position = (TextView) convertView.findViewById(R.id.raw_data_report_textview_3);
            holder.risk_allele = (TextView) convertView.findViewById(R.id.raw_data_report_textview_4);
            holder.ref_allele = (TextView) convertView.findViewById(R.id.raw_data_report_textview_5);
            holder.genome_patient = (TextView) convertView.findViewById(R.id.raw_data_report_textview_6);

祝你好运

我认为您没有在 convertView.I 上调用 findViewById 认为您的 if 循环应该如下所示

if (convertView == null) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.raw_data_item, null);

        holder = new ViewHolder();
        holder.ID_marker = (TextView) convertView.findViewById(R.id.raw_data_report_textview_1);
        holder.chr = (TextView) convertView.findViewById(R.id.raw_data_report_textview_2);
        holder.position = (TextView) convertView.findViewById(R.id.raw_data_report_textview_3);
        holder.risk_allele = (TextView) convertView.findViewById(R.id.raw_data_report_textview_4);
        holder.ref_allele = (TextView) convertView.findViewById(R.id.raw_data_report_textview_5);
        holder.genome_patient = (TextView) convertView.findViewById(R.id.raw_data_report_textview_6);
        convertView.setTag(holder);

    }

我想你已经在 activity 中写了这个 class,所以你试图在 activity 布局中找到那些视图。