ArrayAdapter in Fragment - Android
Asked Answered
A

3

13

I get some error I don't know why. I'm populating ListView with array adapter from string in fragment. I'm using this fragment as main screen to be shown in main activity This is my code

public class HomePage extends Fragment {

private ListView lvHomePage;
private String[] items;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home_page, container, false);

    items = getActivity().getResources().getStringArray(R.array.test);
    lvHomePage = (ListView) getActivity().findViewById(R.id.lvHomePage);

    lvHomePage.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),
    android.R.layout.simple_list_item_1 , items));

    return view;
}
}

I get this output error:

09-18 21:07:27.315: E/AndroidRuntime(3839): FATAL EXCEPTION: main
09-18 21:07:27.315: E/AndroidRuntime(3839): Process: com.kiko.bmgu.crnobelo, PID: 3839
09-18 21:07:27.315: E/AndroidRuntime(3839): java.lang.RuntimeException: Unable to start activity ComponentInfo     com.kiko.bmgu.crnobelo/com.kiko.bmgu.crnobelo.activities.MainActivity}: java.lang.NullPointerException
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)\
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.os.Looper.loop(Looper.java:136)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at java.lang.reflect.Method.invokeNative(Native Method)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at java.lang.reflect.Method.invoke(Method.java:515)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at dalvik.system.NativeStart.main(Naive Method)
09-18 21:07:27.315: E/AndroidRuntime(3839): Caused by: java.lang.NullPointerException
09-18 21:07:27.315: E/AndroidRuntime(3839):     at com.kiko.bmgu.fragments.HomePage.onCreateView(HomePage.java:25)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.Fragment.performCreateView(Fragment.java:1700)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.Activity.performStart(Activity.java:5240)
09-18 21:07:27.315: E/AndroidRuntime(3839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
09-18 21:07:27.315: E/AndroidRuntime(3839):     ... 11 more
Alonsoalonzo answered 18/9, 2014 at 21:12 Comment(0)
D
9

lvHomePage must be null. Use lvHomePage = (ListView) view.findViewById(R.id.lvHomePage); instead.

Doriedorin answered 18/9, 2014 at 21:16 Comment(0)
J
11

Try this way

 private ListView lvHomePage;
    private String[] items;
    private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home_page, container, false);

    return view;
}

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    items = view.getResources().getStringArray(R.array.test);
    lvHomePage = (ListView) view.findViewById(R.id.lvHomePage);

    lvHomePage.setAdapter(new ArrayAdapter<String>(view.getContext(),
    android.R.layout.simple_list_item_1 , items));
   ...
}
Jamesjamesian answered 18/9, 2014 at 21:19 Comment(1)
as AIL said, I should use view instead of getActivity()Alonsoalonzo
D
9

lvHomePage must be null. Use lvHomePage = (ListView) view.findViewById(R.id.lvHomePage); instead.

Doriedorin answered 18/9, 2014 at 21:16 Comment(0)
K
3

The problem is within lvHomePage = (ListView) getActivity().findViewById(R.id.lvHomePage); that's wrong you should get the root view is view not the activity so it should be like this

lvHomePage = (ListView) view.findViewById(R.id.lvHomePage);

Kaykaya answered 18/9, 2014 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.