在Fragment中使用GridView与ArrayAdapter <string> </string>

时间:2014-06-13 03:37:53

标签: android gridview fragment android-arrayadapter

我正在创建一个片段,我想在其中显示一个将从StringArray生成的简单gridview。我不需要任何复杂的东西,基本的ArrayAdapter在不在片段中时可以很好地创建gridview。但是,当我尝试运行它时,我会不断获得nullpointerexception。

public class NarrowTopicsFragment extends Fragment {

private GridView grid;
private String narrowTopic;

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    GridView view = (GridView) inflater.inflate(R.layout.layout_narrow_topics, container);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {


    String[] letters = new String[] { "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

    grid = (GridView) getView().findViewById(R.id.gridView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, letters);
    grid.setAdapter(adapter); //FWIW, this is line 37


}

}

我必须认为这很简单,但我无法理解我的生活。

logcat的:

06-12 23:31:18.152: E/AndroidRuntime(9113): FATAL EXCEPTION: main
06-12 23:31:18.152: E/AndroidRuntime(9113): Process: com.gmail.goodwin.m.joshua.evidenceguide, PID: 9113
06-12 23:31:18.152: E/AndroidRuntime(9113): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gmail.goodwin.m.joshua.evidenceguide/com.gmail.goodwin.m.joshua.evidenceguide.EvidenceGuide}: java.lang.NullPointerException
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.ActivityThread.access$900(ActivityThread.java:161)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.os.Looper.loop(Looper.java:157)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.ActivityThread.main(ActivityThread.java:5356)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at java.lang.reflect.Method.invokeNative(Native Method)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at java.lang.reflect.Method.invoke(Method.java:515)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at dalvik.system.NativeStart.main(Native Method)
06-12 23:31:18.152: E/AndroidRuntime(9113): Caused by: java.lang.NullPointerException
06-12 23:31:18.152: E/AndroidRuntime(9113):     at com.gmail.goodwin.m.joshua.evidenceguide.NarrowTopicsFragment.onActivityCreated(NarrowTopicsFragment.java:37)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.Fragment.performActivityCreated(Fragment.java:1708)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1853)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.Activity.performCreate(Activity.java:5429)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
06-12 23:31:18.152: E/AndroidRuntime(9113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)

2 个答案:

答案 0 :(得分:3)

试试这个

public class NarrowTopicFragment extends Fragment {
GridView grid;
String narrowTopic;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.layout_narrow_topics, container,
            false);

    grid = (GridView) rootView.findViewById(R.id.gridView1);

    String[] letters = new String[] { "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, letters);

    grid.setAdapter(adapter);
    return rootView;
}

}

答案 1 :(得分:0)

gridView1是布局文件中GridView的ID吗?

无论如何,一个更简单的解决方案是将此代码移到onCreateView(),即

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

    String[] letters = new String[] { "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, letters);
    view.setAdapter(adapter); //FWIW, this is line 37

    return view;
}