片段中的自定义GridView适配器

时间:2016-04-15 15:39:47

标签: java android android-fragments gridview android-adapter

我想在我的片段中实现简单的Gridview自定义适配器。但我得到了以下错误:

04-15 11:35:21.064 1190-1190/amir.com.unnamed E/AndroidRuntime: FATAL EXCEPTION: main
                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{amir.com.unnamed/amir.com.unnamed.MainActivity}: java.lang.NullPointerException
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
                                                                at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                                                                at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                at android.os.Looper.loop(Looper.java:137)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5103)
                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                at java.lang.reflect.Method.invoke(Method.java:525)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                at dalvik.system.NativeStart.main(Native Method)
                                                             Caused by: java.lang.NullPointerException
                                                                at amir.com.unnamed.fragments.chooseCardFragment.onCreateView(chooseCardFragment.java:28)
                                                                at android.app.Fragment.performCreateView(Fragment.java:1695)
                                                                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
                                                                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
                                                                at android.app.BackStackRecord.run(BackStackRecord.java:682)
                                                                at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
                                                                at android.app.Activity.performStart(Activity.java:5142)
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
                                                                at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                at android.os.Looper.loop(Looper.java:137) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5103) 
                                                                at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                                at dalvik.system.NativeStart.main(Native Method) 

和我的片段代码:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.choose_card_fragment, container, false);

    GridView cardsGrid = (GridView) getActivity().findViewById(R.id.choosecardsGridview);
    cardsGrid.setAdapter(new cardAdapter(rootView.getContext()));
    return rootView;
}

和自定义适配器代码:

public class cardAdapter extends BaseAdapter {
Context context;

private Integer[] mThumbIds = {
        R.drawable.img, R.drawable.img3,
        R.drawable.img4, R.drawable.img5,
        R.drawable.img6, R.drawable.img7,
        R.drawable.img2
};

public cardAdapter(Context c) {
    this.context = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imga;
    if (convertView == null) {
        imga = new ImageView(context);
        imga.setLayoutParams(new GridView.LayoutParams(85, 85));
        imga.setScaleType(ImageView.ScaleType.CENTER);
        imga.setPadding(8, 8, 8, 8);
    } else {
        imga = (ImageView) convertView;
    }
    imga.setImageResource(mThumbIds[position]);

    return imga;
}

}

我不知道哪里弄错了。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

如果你正在做

var App = React.createClass({
    render: function() {
        var componentList = [
            <SampleComponent name="SomeName1"/>,
            <SampleComponent name="SomeName2"/>
        ]; // Change this to get the list from props or state
        return (
            <div>
                <h1>App main component! </h1>
                {componentList}
            </div>
        );
    }
});

不应该

    View rootView = inflater.inflate(R.layout.choose_card_fragment, container, false);

实际上是

    GridView cardsGrid = (GridView) getActivity().findViewById(R.id.choosecardsGridview);