user3423347
user3423347

Reputation: 33

Null Pointer Exception while inflating listview

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

The stacktrace of the Exception is

    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)

Class 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;
    }

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;
}

The 49th line in trace is holder.headlineView = (TextView) convertView.findViewById(R.id.title);

Finally here is the 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" />

Upvotes: 0

Views: 580

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

Change this

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

to

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);

Since you have

View  row = convertView;

and

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

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

Edit:

As blackbelt suggested you can also avoid

View  row = convertView;

Retain the same code and change only return row; to return convertView;

Upvotes: 3

Related Questions