2

我正在尝试在 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)
4

3 回答 3

2

convertView目前,如果 convertView 是null.changegetView方法,您没有将创建的视图分配给 :

@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);

        convertView=myButton;  //>>> assign myButton to convertView
    }
    else{
        myButton = (Button) convertView;
    }
     ....
    return convertView; /// return View from here
}
于 2013-05-09T18:34:56.830 回答
2

在您的 OnCreateView 中,父活动尚不可用,因此在那里调用 getActivity() 是不安全的。

将您的代码移动到 onActivityCreated:

@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);
    return rootView;
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    gridview.setAdapter(new MiscAdapter(getActivity()));
}
于 2013-05-09T18:35:46.627 回答
1

您的getView方法返回 null。convertView必要时充气后不应该返回吗?

于 2013-05-09T19:26:56.107 回答