getView Of Adapter中的NullPointerException扩展了BaseAdapter

时间:2013-08-07 11:37:06

标签: android android-widget baseadapter layout-inflater

我正在tabwidget中填充listview进行搜索。我的列表包含一个图像和textview.Without TabWidget它可以罚款,但在TabWidget中它会出错。我在getview中有错误我的locat是

08-07 16:24:44.300: E/AndroidRuntime(20166): FATAL EXCEPTION: main
08-07 16:24:44.300: E/AndroidRuntime(20166): java.lang.NullPointerException
08-07 16:24:44.300: E/AndroidRuntime(20166):    at com.example.project.SearchDictionaryActivity$MyCustomAdapter.getView(SearchDictionaryActivity.java:392)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.AbsListView.obtainView(AbsListView.java:2485)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.ListView.onMeasure(ListView.java:1161)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2242)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2242)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.View.measure(View.java:12842)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2632)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.os.Looper.loop(Looper.java:137)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at android.app.ActivityThread.main(ActivityThread.java:4517)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at java.lang.reflect.Method.invokeNative(Native Method)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at java.lang.reflect.Method.invoke(Method.java:511)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-07 16:24:44.300: E/AndroidRuntime(20166):    at dalvik.system.NativeStart.main(Native Method)

我的search_dictionary_item_item.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left|center"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingTop="10dp" >

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="50dp"
        android:layout_height="60dp" >
    </ImageView>

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textColor="#0099CC"
        android:textSize="20dp"
        android:textStyle="bold" >
    </TextView>

    </LinearLayout>

我的行 392 ,这是

at com.example.project.SearchDictionaryActivity$MyCustomAdapter.getView(SearchDictionaryActivity.java:392)

    TextView textview = (TextView) row.findViewById(R.id.TextView01);

我试过了2个建议

1

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

          View row=convertView;
          Context c = null;

          if(convertView==null){
          LayoutInflater inflater = getLayoutInflater(null);
          row = inflater.inflate(R.layout.search_dictionary_item_item, parent, false);
         }

    TextView textview = (TextView) row.findViewById(R.id.TextView01);
    ImageView imageview = (ImageView) row.findViewById(R.id.ImageView01);

    textview.setText(data_text[position]);
    imageview.setImageBitmap(data_image[position]);

    return (row);

    }

用于放置null getLayoutInflater(null);我得到了

The method getLayoutInflater() in the type Activity is not applicable for the arguments (null)

2.Sugesstion

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

   // LayoutInflater inflater = getLayoutInflater();
    View row;
    LayoutInflater inflater = (LayoutInflater) convertView.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

    row = inflater.inflate(R.layout.search_dictionary_item_item, parent, false);

    TextView textview = (TextView) row.findViewById(R.id.TextView01);
    ImageView imageview = (ImageView) row.findViewById(R.id.ImageView01);

    textview.setText(data_text[position]);
    imageview.setImageBitmap(data_image[position]);

    return (row);

    }

两者都不适用于上面给出的Logcat

4 个答案:

答案 0 :(得分:2)

您的convertView从未在建议#2中被触及

convertView = inflater.inflate(R.layout.search_dictionary_item_item, parent, false);

应该修复它,您还需要返回convertView而不是row

答案 1 :(得分:1)

尝试使用此链接进行自定义适配器here

答案 2 :(得分:1)

像这样改变你的getview方法..

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

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) convertView.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(
                R.layout.search_dictionary_item_item, null);

    }

    TextView textview = (TextView) convertView
            .findViewById(R.id.TextView01);
    ImageView imageview = (ImageView) convertView
            .findViewById(R.id.ImageView01);

    textview.setText(data_text[position]);
    imageview.setImageBitmap(data_image[position]);

    return convertView;

}

答案 3 :(得分:1)

我认为您使用了错误的布局而不是正确的行布局,我得到了同样的错误。当我验证行布局是另一个

e.g。 而不是

convertView = inflater.inflate(R.layout.wrong_layout, parent, false);

使用

convertView = inflater.inflate(R.layout.Correct_layout, parent, false);