2014-06-13 52 views
1

我正在創建一個片段,我想要顯示一個簡單的GridView,它將從StringArray中生成。我不需要任何複雜的東西,基本的ArrayAdapter在不在片段中時可以很好地用於創建gridview。但是,當我嘗試運行它時,我一直得到一個nullpointerexception。在ArrayAdapter中使用GridView裏面的片段<String>

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) 

回答

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

}

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