图像的GridView里面片段 [英] Image GridView Inside Fragment

查看:75
本文介绍了图像的GridView里面片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始开发开发iOS上后,在Android平台上。我环顾四周,我似乎无法弄清楚。 我想有一个网格视图显示被选中在操作栏选项卡后。该片段由控制标签栏主要活动带入视图。 我认为这个问题可能是与合格范围内,但我不知道。

I have just started to develop on the android platform after developing on iOS. I have looked around and I can't seem to figure it out. I am trying to have a grid view appear after a tab in the action bar is selected. The fragment is brought into view by a main activity that controls the tab bar. I think the problem may be something to do with passing context but I'm not sure.

下面是我的 MainActivity.java 。这是其中的片段被初始化并连接到活性。它的工作原理没有code的片段。

Here is my MainActivity.java. This is where the fragment is initialized and attached to the activity. It works without the code in the fragment.

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

下面是我的PhotosFragment.java这就是我想要的网格视图进行填充,并显示出来。

Here is my PhotosFragment.java This is where I want the grid view to be populated and displayed.

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 类。这是其中的图像被添加到我认为适配器

Here is my PhotoImageAdapter.java class. This is where the images are added to the adapter I think.

  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, 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_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_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

和这里是我的photos_layout其中包含了ID photogridview GridView控件。 photos_layout.xml

And here is my photos_layout which contains the gridview with the id photogridview. 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

当您单击该选项卡上的片段被初始化的应用程序崩溃。 我下面的教程是一个在href="http://developer.android.com/resources/tutorials/views/hello-gridview.html" rel="nofollow"> Android开发网站在

The application crashes when you click on the tab and the fragment is initialised. The tutorial I am following is the one on the android developers site.

任何帮助或解释将是惊人的。就像我说的,我是新来这个所以这将是巨大的,有一些指引,以帮助我了解怎么回事。

Any help or explanations would be amazing. Like I said, I am new to this so it would be great to have some pointers to help me understand whats going on.

迪斯科

推荐答案

原来一些简单的修改,原来的code和它的作品。

It turns out a few simple modifications to the original code and it works.

调试,设置断点,我能够发现在PhotoImageAdapter的背景下是一个空指针,因此,导致应用程序崩溃之后。这是我在初始化适配器在我的光碎片的方式,也是我正在初始化它的方法。 下面是code,它可以正确处理别人谁是挣扎与此有关。

After debugging and setting breakpoints I was able to find that the context in the PhotoImageAdapter was a null pointer and therefore, causing the app to crash. It was the way I was initialising the adapter in my PhotoFragment and also the method I was initialising it in. Below is the code that works correctly for anyone else who is struggling with this.

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

Again this might not be the best or the only of doing it but this way worked for me. (PhotoImageAdapter.java had a name change to MyAdapter.java)

这篇关于图像的GridView里面片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆