Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

android - ListFragment's ListView throws NullPointerException when I use my own ListAdapter

I'm trying to display data from my database in a ListFragment. To do this, I have followed the same way when I use ListActivity, created my own adapter (adapter might look strange, I converted it from a class that extends BaseAdapter in one of my old projects):

package com.mysys.asistan;

import android.app.ListFragment;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.mysys.asistan.database.Company;

public class CompanyListFragment extends ListFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new CompanyListAdapter(getActivity()));
    }

    private class CompanyListAdapter implements ListAdapter {

        private final Context context;
        private DatabaseHelper dbHelper;

        public CompanyListAdapter(Context context) {
            this.context = context;
            this.dbHelper = new DatabaseHelper(context);
        }

        public int getCount() {
            return dbHelper.getCompanyCount();
        }

        public Object getItem(int position) {
            return dbHelper.getAllCompanies().get(position);
        }

        public long getItemId(int position) {
            return (long) dbHelper.getAllCompanies().get(position).id;
        }

        public View getView(int position, View view, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.company_list_item, parent, false);
            Company company = (Company) getItem(position);

            TextView nameView = (TextView) row.findViewById(R.id.CompanyListName);
            TextView telephoneView = (TextView) row.findViewById(R.id.CompanyListTelephone);
            TextView addressView = (TextView) row.findViewById(R.id.CompanyListAddress);

            nameView.setText(company.name);
            telephoneView.setText(company.telephone);
            addressView.setText(company.address);

            return null;
        }

        public int getItemViewType(int position) {
            return IGNORE_ITEM_VIEW_TYPE;
        }

        public int getViewTypeCount() {
            return 1;
        }

        public boolean hasStableIds() {
            return true;
        }

        public boolean isEmpty() {
            return getCount() == 0 ? true : false;
        }

        public void registerDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        public void unregisterDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        public boolean areAllItemsEnabled() {
            return true;
        }

        public boolean isEnabled(int position) {
            return true;
        }

    }
}

However, that leads to a NullPointerException and I couldn't figure anything from the logs in LogCat, nothing related to my code:

01-24 20:24:06.040: E/AndroidRuntime(8731): FATAL EXCEPTION: main
01-24 20:24:06.040: E/AndroidRuntime(8731): java.lang.NullPointerException
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.ListView.measureScrapChild(ListView.java:1181)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1264)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.ListView.onMeasure(ListView.java:1173)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.View.measure(View.java:10848)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4353)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.View.measure(View.java:10848)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4353)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.View.measure(View.java:10848)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4353)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:956)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:521)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.View.measure(View.java:10848)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4353)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.View.measure(View.java:10848)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.View.measure(View.java:10848)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4353)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2042)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.View.measure(View.java:10848)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.ViewRoot.performTraversals(ViewRoot.java:941)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.view.ViewRoot.handleMessage(ViewRoot.java:2063)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.os.Looper.loop(Looper.java:132)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at android.app.ActivityThread.main(ActivityThread.java:4126)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at java.lang.reflect.Method.invokeNative(Native Method)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at java.lang.reflect.Method.invoke(Method.java:491)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
01-24 20:24:06.040: E/AndroidRuntime(8731):     at dalvik.system.NativeStart.main(Native Method)

And this is the layout which contains the fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.mysys.asistan.CompanyListFragment"
        android:layout_width="@dimen/company_listfragment_width"
        android:layout_height="wrap_content" />

</LinearLayout>

And this is company_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/CompanyListName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/CompanyListAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/CompanyListTelephone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

I found this question, but solution is not something related to my problem I think. Thanks in advance for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should return the view from getview() function. You are returning "Null".

        public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.company_list_item, parent, false);
        Company company = (Company) getItem(position);

        TextView nameView = (TextView) row.findViewById(R.id.CompanyListName);
        TextView telephoneView = (TextView) row.findViewById(R.id.CompanyListTelephone);
        TextView addressView = (TextView) row.findViewById(R.id.CompanyListAddress);

        nameView.setText(company.name);
        telephoneView.setText(company.telephone);
        addressView.setText(company.address);

        return row;//changed from null
    }

Note: Use recycled view and Holders to make the listview scroll smooth. For beter understanding read this tutorial.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...