Fragment 中的 ArrayAdapter - Android

Posted

技术标签:

【中文标题】Fragment 中的 ArrayAdapter - Android【英文标题】:ArrayAdapter in Fragment - Android 【发布时间】:2014-11-13 08:55:08 【问题描述】:

我收到一些错误,我不知道为什么。我正在使用片段中的字符串中的数组适配器填充 ListView。我使用这个片段作为主屏幕显示在主要活动中 这是我的代码

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;


我得到这个输出错误:

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

【问题讨论】:

【参考方案1】:

试试这个方法

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

【讨论】:

正如 AIL 所说,我应该使用 view 而不是 getActivity()【参考方案2】:

lvHomePage 必须为空。请改用lvHomePage = (ListView) view.findViewById(R.id.lvHomePage);

【讨论】:

【参考方案3】:

问题在lvHomePage = (ListView) getActivity().findViewById(R.id.lvHomePage);内 错了,你应该得到根视图是view 而不是活动 所以应该是这样的

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

【讨论】:

以上是关于Fragment 中的 ArrayAdapter - Android的主要内容,如果未能解决你的问题,请参考以下文章

在Android中通过ArrayAdapter在ListView中单击单个ImageView背景后如何更改它?

当用户旋转手机并将所有数据保留在 ArrayAdapter 中时,保留列表视图项的良好解决方案

当 ArrayAdapter.isEnabled 返回 false 时,ListView 中的分隔符消失

android中的ArrayAdapter创建简单的listview

更新 Arrayadapter 中的数据

FragmentPagerAdapter/FragmentStatePagerAdapter/PagerAdapter