gpt4 book ai didi

android - 在 Fragment 中使用 GridView 和 ArrayAdapter

转载 作者:搜寻专家 更新时间:2023-11-01 07:58:52 24 4
gpt4 key购买 nike

我正在创建一个 fragment ,我想在其中显示一个将从 StringArray 生成的简单 GridView 。我不需要任何复杂的东西,基本的 ArrayAdapter 在不在 fragment 中时可以很好地创建 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


}

我必须认为这很简单,但我终究无法弄明白。

日志:

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)

最佳答案

试试这个

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;
}

关于android - 在 Fragment 中使用 GridView 和 ArrayAdapter<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24197323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com