在片段中使用适配器

时间:2013-05-09 18:26:49

标签: android gridview android-fragments adapter

我尝试在GridView中使用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()));

我想我需要传递一个Context,但我不确定这是我应该怎么做的,或者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)

3 个答案:

答案 0 :(得分:2)

目前,如果convertView为convertView。更改null方法,则不会将创建的视图分配给getView

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

答案 1 :(得分: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()));
}

答案 2 :(得分:1)

您的getView方法返回null。如果有必要,你不应该在充气后返回convertView吗?