GridView
aで aを使用してからFragment
、 を使用しAdapter
てボタンを設定しようとしています。私のコードは現在:
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()));
そこに a を渡す必要があると思いますがContext
、それが私がそれを行うべきかどうか、または をFragments
使用するかどうかはわかりませんContext
。現在、Null Pointer Exception がスローされています。
スタックトレース:
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)