我正在尝试在 aGridView
中使用 a Fragment
,然后使用 anAdapter
来填充它。我目前的代码是:
在 MainActivity 内部:
public static class MiscFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public MiscFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.misc_fragment,
container, false);
GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
gridview.setAdapter(new MiscAdapter(this.getActivity()));
return rootView;
}
}
我的适配器类:
public class MiscAdapter extends BaseAdapter {
private Context mContext;
public MiscAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mButtons.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button myButton;
if(convertView == null){
myButton = new Button(mContext);
myButton.setLayoutParams(new GridView.LayoutParams(100,55));
myButton.setPadding(8,8,8,8);
}
else{
myButton = (Button) convertView;
}
myButton.setText(mButtons[position]);
myButton.setId(position);
return null;
}
private String[] mButtons = {
"Button1", "Button2", "Button3", "Button4",
"Button5", "Button6", "Button7", "Button8",
"Button9", "Button10"
};
}
我相信我的错误在于:gridview.setAdapter(new MiscAdapter(this.getActivity()));
我想我需要在其中传递 aContext
但我不确定这是否是我应该这样做的方式,或者是否Fragments
使用Context
. 现在它正在抛出空指针异常。
堆栈跟踪:
05-09 18:29:19.541: E/AndroidRuntime(525): FATAL EXCEPTION: main
05-09 18:29:19.541: E/AndroidRuntime(525): java.lang.NullPointerException
05-09 18:29:19.541: E/AndroidRuntime(525): at android.widget.GridView.onMeasure(GridView.java:1028)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.View.measure(View.java:12723)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.View.measure(View.java:12723)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1389)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.View.measure(View.java:12723)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.View.measure(View.java:12723)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.View.measure(View.java:12723)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-09 18:29:19.541: E/AndroidRuntime(525): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.View.measure(View.java:12723)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.os.Looper.loop(Looper.java:137)
05-09 18:29:19.541: E/AndroidRuntime(525): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-09 18:29:19.541: E/AndroidRuntime(525): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 18:29:19.541: E/AndroidRuntime(525): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 18:29:19.541: E/AndroidRuntime(525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-09 18:29:19.541: E/AndroidRuntime(525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-09 18:29:19.541: E/AndroidRuntime(525): at dalvik.system.NativeStart.main(Native Method)