膨胀listview时出现空指针异常

时间:2014-03-16 17:44:15

标签: android android-layout android-fragments android-listview

我收到了NullPointerException - 我正试图在ListView内对Fragment进行充气。

Exception的堆栈跟踪是

    03-16 22:46:41.721: W/dalvikvm(1571): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
    03-16 22:46:41.840: E/AndroidRuntime(1571): FATAL EXCEPTION: main
    03-16 22:46:41.840: E/AndroidRuntime(1571): java.lang.NullPointerException
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at net.justanotherblog.swipeview.CustomListAdapter.getView(CustomListAdapter.java:49)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.ListView.fillDown(ListView.java:652)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.ListView.fillFromTop(ListView.java:709)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.ListView.layoutChildren(ListView.java:1580)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.View.layout(View.java:7035)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.View.layout(View.java:7035)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.View.layout(View.java:7035)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1528)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.View.layout(View.java:7035)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.View.layout(View.java:7035)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.View.layout(View.java:7035)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.View.layout(View.java:7035)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.os.Looper.loop(Looper.java:123)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at java.lang.reflect.Method.invoke(Method.java:521)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
    03-16 22:46:41.840: E/AndroidRuntime(1571):     at dalvik.system.NativeStart.main(Native Method)

班级代码

    public static class Songs_Fragment extends Fragment {

    public Songs_Fragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.songs_fragment, container, false);

        ListView lv1 = (ListView) rootView.findViewById(R.id.songs_list);
        ArrayList<NewsItem> image_details = getListData();

        CustomListAdapter C_Adapter = new CustomListAdapter(getActivity(),image_details);
        lv1.setAdapter(C_Adapter);
        return rootView;
    }

customadapter的GetView方法

    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View  row = convertView;

    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(this.context);
        row = inflater.inflate(R.layout.listview_movies, parent, false);
        holder = new ViewHolder();
        holder.headlineView = (TextView) convertView.findViewById(R.id.title);
        holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
        holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
        holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    NewsItem newsItem = (NewsItem) listData.get(position);

    holder.headlineView.setText(newsItem.getHeadline());
    holder.reporterNameView.setText("By, " + newsItem.getReporterName());
    holder.reportedDateView.setText(newsItem.getDate());

    if (holder.imageView != null) {
        new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
    }

    return row;
}

static class ViewHolder {
    TextView headlineView;
    TextView reporterNameView;
    TextView reportedDateView;
    ImageView imageView;
}

追踪中的第49行是holder.headlineView = (TextView) convertView.findViewById(R.id.title);

最后这里是listview_movies.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/thumbImage"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@drawable/list_placeholder" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/thumbImage"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:text=""
    android:textColor="@drawable/list_item_text_selector"
    android:textStyle="bold"
    android:typeface="sans" />

<TextView
    android:id="@+id/reporter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:layout_marginTop="5dip"
    android:layout_toRightOf="@id/thumbImage"
    android:paddingLeft="5dp"
    android:text=""
    android:textColor="@drawable/list_item_text_selector"
    android:textSize="12sp" />

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/reporter"
    android:layout_alignBottom="@+id/reporter"
    android:layout_alignParentRight="true"
    android:paddingRight="5dp"
    android:text=""
    android:textColor="@drawable/list_item_text_selector"
    android:textSize="12sp" />

1 个答案:

答案 0 :(得分:3)

更改此

holder.headlineView = (TextView) convertView.findViewById(R.id.title);

holder.headlineView = (TextView) row.findViewById(R.id.title);

同样适用于其他观点

holder.reporterNameView = (TextView) row.findViewById(R.id.reporter);
holder.reportedDateView = (TextView) row.findViewById(R.id.date);
holder.imageView = (ImageView)row.findViewById(R.id.thumbImage);

因为你有

View  row = convertView;

row = inflater.inflate(R.layout.listview_movies, parent, false); // layout inflated

findViewById在当前膨胀的布局中查找id为id的视图。因此,您需要使用View对象row来初始化视图。

                  OR

编辑:

黑带建议你也可以避免

View  row = convertView;

保留相同的代码,仅将return row;更改为return convertView;