Android 图像 GridView 内部片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10798943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image GridView Inside Fragment
提问by StuStirling
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.
在iOS上开发之后,我才开始在android平台上开发。我环顾四周,似乎无法弄清楚。我试图在选择操作栏中的选项卡后显示网格视图。该片段由控制选项卡栏的主要活动带入视野。我认为问题可能与传递上下文有关,但我不确定。
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.
这是我的MainActivity.java。这是片段被初始化并附加到活动的地方。它无需片段中的代码即可工作。
if (mFragment == null){
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content,mFragment,mTag);
} else {
ft.attach(mFragment);
}
Here is my PhotosFragment.java This is where I want the grid view to be populated and displayed.
这是我的 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);
}
}
Here is my PhotoImageAdapter.javaclass. This is where the images are added to the adapter I think.
这是我的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, 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
};
}
And here is my photos_layout which contains the gridview with the id photogridview. photos_layout.xml
这是我的 photos_layout,其中包含带有 id photogridview 的 gridview。 照片布局.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>
EDIT
编辑
Here is the log report when it crashes
这是崩溃时的日志报告
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.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
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 developerssite.
当您单击选项卡并初始化片段时,应用程序崩溃。我正在遵循的教程是android 开发人员网站上的教程。
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.
任何帮助或解释都会很棒。就像我说的那样,我对此很陌生,因此如果有一些指示可以帮助我了解正在发生的事情,那就太好了。
Disco
迪斯科
回答by StuStirling
It turns out a few simple modifications to the original code and it works.
结果是对原始代码进行了一些简单的修改,并且可以正常工作。
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.
在调试和设置断点后,我发现 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;
}
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)
同样,这可能不是最好的或唯一的,但这种方式对我有用。(PhotoImageAdapter.java 已更名为 MyAdapter.java)
回答by StuStirling
hi chek the code which i just shared, i made the fragment with gridview so hopefully it will helpful for you check this link, there you will find full detail about gridview implementation with fragment for both device and tablet all the best
嗨,检查我刚刚分享的代码,我用 gridview 制作了片段,所以希望它对你检查这个链接有帮助,在那里你会找到关于设备和平板电脑片段的 gridview 实现的完整细节。
check this files mainly in my answer it will sure helpful for u MasterFragment,MasterGridActivity,MyAdapter,gridview.xml
主要在我的回答中检查这个文件它肯定对你有帮助 MasterFragment,MasterGridActivity,MyAdapter,gridview.xml