android – 图像GridView里面的片段

前端之家收集整理的这篇文章主要介绍了android – 图像GridView里面的片段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在iOS上开发之后,我刚开始在 android平台上开发.我环顾四周,似乎无法弄明白.
我正在尝试在选择操作栏中的选项卡后显示网格视图.片段由控制标签栏的主要活动进入视图.
我认为问题可能与传递上下文有关,但我不确定.

这是我的MainActivity.java.这是片段初始化并附加到活动的位置.它没有片段中的代码.

if (mFragment == null){
            mFragment = Fragment.instantiate(mActivity,mClass.getName());
            ft.add(android.R.id.content,mFragment,mTag);
        } else {
            ft.attach(mFragment);
        }

这是我的PhotosFragment.java这是我想要填充和显示网格视图的地方.

public class PhotosFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GridView gridview = (GridView) this.getActivity().findViewById(R.id.photogridview);
        gridview.setAdapter(new PhotoImageAdapter(this.getActivity()));
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }   
}

这是我的PhotoImageAdapter.java类.这是将图像添加到我认为的适配器的位置.

public class PhotoImageAdapter extends BaseAdapter {
    private Context mContext;

    public PhotoImageAdapter(Context c) {
        mContext = c;
    }

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

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

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

    public View getView(int position,View convertView,ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled,initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85,85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    private Integer[] mThumbIds = {
            R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7,R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_7
    };
}

这是我的photos_layout,其中包含带有id photogridview的gridview.
photos_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/photogridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center">
</GridView>

编辑

这是崩溃时的日志报告

05-29 14:15:43.895: W/dalvikvm(676): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-29 14:15:43.925: E/AndroidRuntime(676): FATAL EXCEPTION: main
05-29 14:15:43.925: E/AndroidRuntime(676): java.lang.NullPointerException
05-29 14:15:43.925: E/AndroidRuntime(676):  at com.corecoders.PhotosFragment.onCreate(PhotosFragment.java:21)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.app.BackStackRecord.run(BackStackRecord.java:622)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.os.Handler.handleCallback(Handler.java:605)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.os.Looper.loop(Looper.java:137)
05-29 14:15:43.925: E/AndroidRuntime(676):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-29 14:15:43.925: E/AndroidRuntime(676):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 14:15:43.925: E/AndroidRuntime(676):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 14:15:43.925: E/AndroidRuntime(676):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-29 14:15:43.925: E/AndroidRuntime(676):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-29 14:15:43.925: E/AndroidRuntime(676):  at dalvik.system.NativeStart.main(Native Method)
05-29 14:15:44.494: I/dalvikvm(676): threadid=3: reacting to signal 3
05-29 14:15:44.514: I/dalvikvm(676): Wrote stack traces to '/data/anr/traces.txt'
05-29 14:20:43.995: I/Process(676): Sending signal. PID: 676 SIG: 9

单击选项卡并初始化片段时,应用程序崩溃.
我关注的教程是android developers网站上的教程.

任何帮助或解释都会令人惊讶.就像我说的,我是新手,所以有一些指示可以帮助我了解最新情况.

迪斯科

解决方法

它对原始代码进行了一些简单的修改,并且可以正常工作.

在调试和设置断点之后,我能够发现PhotoImageAdapter中的上下文是一个空指针,因此导致应用程序崩溃.这是我在PhotoFragment中初始化适配器的方式,也是我初始化它的方法.
下面的代码适用于任何正在努力解决此问题的人.

@Override
    public View onCreateView (LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.photos_layout,container,false);
        GridView gridView = (GridView) view.findViewById(R.id.photogridview);
        gridView.setAdapter(new MyAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
        return view;
    }

再次,这可能不是最佳或唯一的做法,但这种方式对我有用. (PhotoImageAdapter.java名称更改为MyAdapter.java)

原文链接:https://www.f2er.com/android/316373.html

猜你在找的Android相关文章