Filip Eriksson
Filip Eriksson

Reputation: 977

making an ArrayAdapter for my own class in a ListView

I am trying to create a list of my own elements but I can't make it work. The app starts but crashes as soon as it reaches this particular view. I hope someone can figure out what's wrong. Two java classes will be shown and also the error message.

Here's the first class. Observe that it is a fragment and I don't want to change to an activity:

public class SektionerFragment extends SherlockFragment {

private ListView lv;

        private SectionAdapter adapter;
        private ArrayList<Section> fetch = new ArrayList<Section>();

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View contentView = inflater.inflate(R.layout.fragment_sektioner, container, false);

            Section one = new Section(1,"Big1","Small1","Small1","Small1","Small1","Small1",2);

            fetch.add(one);
            lv = (ListView) getView().findViewById(R.id.SektionerList);
            adapter = new SectionAdapter(getActivity(),
                    R.id.SektionerList,
                    fetch);
            lv.setAdapter(adapter);
            return contentView;
        }   
}

Second class:

public class SectionAdapter extends ArrayAdapter<Section>{
    private ArrayList<Section> entries;
    private Activity activity;

    public SectionAdapter(Activity a, int textViewResourceId, ArrayList<Section> entries) {
        super(a, textViewResourceId, entries);
        this.entries = entries;
        this.activity = a;
    }

    public static class ViewHolder{
        public TextView item1;
        public TextView item2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            LayoutInflater vi =
                (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_element_sektioner, null);
            holder = new ViewHolder();
            holder.item1 = (TextView) v.findViewById(R.id.sektion_sektionsnamn);
            holder.item2 = (TextView) v.findViewById(R.id.sektion_programnamn);
            v.setTag(holder);
        }
        else
            holder=(ViewHolder)v.getTag();

        final Section custom = entries.get(position);
        if (custom != null) {
            holder.item1.setText(custom.getName());
            holder.item2.setText(custom.getName());
        }
        return v;
    }   
}

Error message:

06-29 17:16:34.059: D/AndroidRuntime(1241): Shutting down VM
06-29 17:16:34.059: W/dalvikvm(1241): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-29 17:16:34.069: E/AndroidRuntime(1241): FATAL EXCEPTION: main
06-29 17:16:34.069: E/AndroidRuntime(1241): java.lang.NullPointerException
06-29 17:16:34.069: E/AndroidRuntime(1241):     at se.Appsolut.student.SektionerFragment.onCreateView(SektionerFragment.java:120)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.os.Handler.handleCallback(Handler.java:615)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.os.Looper.loop(Looper.java:137)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at android.app.ActivityThread.main(ActivityThread.java:4745)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at java.lang.reflect.Method.invoke(Method.java:511)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-29 17:16:34.069: E/AndroidRuntime(1241):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 203

Answers (2)

Android Noob
Android Noob

Reputation: 3341

shouldn't it be lv = (ListView)contentView.findViewById(R.id.SektionerList)?

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

  lv = (ListView) getView().findViewById(R.id.SektionerList);

you can not use getView inside onCreateView, because getView returns what you have returned inside onCreateView.

if R.id.SektionerList belongs to fragment_sektioner.xml, you have to use the View object returned by inflater.inflate(R.layout.fragment_sektioner, container, false);

Upvotes: 0

Related Questions