web-dev-qa-db-fra.com

ArrayAdapter in Fragment - Android

Je reçois une erreur, je ne sais pas pourquoi. Je remplis ListView avec un adaptateur de tableau à partir d'une chaîne en fragment. J'utilise ce fragment comme écran principal à afficher dans l'activité principale Ceci est mon 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;
}
}

J'obtiens cette erreur de sortie:

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
10
KiKo

lvHomePage doit être nul. Utilisez lvHomePage = (ListView) view.findViewById(R.id.lvHomePage); à la place.

8
mbmc

Essayez de cette façon

 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));
   ...
}
6
ArmDroid

Le problème est dans lvHomePage = (ListView) getActivity().findViewById(R.id.lvHomePage); c'est faux, vous devriez obtenir la vue racine est view pas l'activité donc ça devrait être comme ça

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

3
Kiloreux