而充气列表视图空指针异常 [英] Null Pointer Exception while inflating listview

查看:187
本文介绍了而充气列表视图空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个 NullPointerException异常 - 我想夸大一个的ListView 片段

I am getting a NullPointerException - I am trying to inflate a ListView inside a Fragment.

例外的堆栈跟踪是

    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)

类code

    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方法

GetView method of customadapter

    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" />

推荐答案

更改此

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

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

同样,对于其他视图

Similarly for other views

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的视图。所以,你需要使用View对象初始化意见。

findViewById looks for a view with the id in the current inflated layout. So you need to use the View object row to initialize views.

                  OR

编辑:

黑带作为建议你也可避免

As blackbelt suggested you can also avoid

View  row = convertView;

保持相同的code和只更改返回行; 返回convertView ;

这篇关于而充气列表视图空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆