Android ImageAdapter 与 Fragment 中的 Gridview
Posted
技术标签:
【中文标题】Android ImageAdapter 与 Fragment 中的 Gridview【英文标题】:Android ImageAdapter with Gridview in Fragment 【发布时间】:2013-10-13 10:09:30 【问题描述】:我有一个带有 gridview 的适配器,可以作为 Activity 工作。我现在正试图将它放在一个片段中并转换了东西,但它不起作用。当我在我的 Activity 中包含 IconFragmentSystem 时,我在尝试打开 Activity 时会强制关闭。
我知道 Activity 可以正常工作,因为我可以使用其他 Fragments 并且一切正常,所以我知道我的问题出在这个文件中。
package com.designrifts.ultimatethemeui;
import com.designrifts.ultimatethemeui.R;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
public class IconFragmentSystem extends Fragment implements AdapterView.OnItemClickListener
private static final String RESULT_OK = null;
public Uri CONTENT_URI;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.main, container, false);
super.onCreate(savedInstanceState);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
gridview.setAdapter(new IconAdapter(this, iconSize));
gridview.setOnItemClickListener(this);
CONTENT_URI=Uri.parse("content://"+iconsProvider.class.getCanonicalName());
return view;
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
String icon=adapterView.getItemAtPosition(i).toString();
Intent result = new Intent(null, Uri.withAppendedPath(CONTENT_URI,icon));
setResult(RESULT_OK, result);
finish();
private void setResult(String resultOk, Intent result)
// TODO Auto-generated method stub
private void finish()
// TODO Auto-generated method stub
private class IconAdapter extends BaseAdapter
private Context mContext;
private int mIconSize;
public IconAdapter(Context mContext, int iconsize)
super();
this.mContext = mContext;
this.mIconSize = iconsize;
loadIcon();
public IconAdapter(IconFragmentSystem iconssystem, int iconSize)
// TODO Auto-generated constructor stub
@Override
public int getCount()
return mThumbs.size();
@Override
public Object getItem(int position)
return mThumbs.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ImageView imageView;
if (convertView == null)
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
else
imageView = (ImageView) convertView;
imageView.setImageResource(mThumbs.get(position));
return imageView;
private ArrayList<Integer> mThumbs;
////////////////////////////////////////////////
private void loadIcon()
mThumbs = new ArrayList<Integer>();
final Resources resources = getResources();
final String packageName = getActivity().getApplication().getPackageName();
addIcon(resources, packageName, R.array.systemicons);
private void addIcon(Resources resources, String packageName, int list)
final String[] extras = resources.getStringArray(list);
for (String extra : extras)
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0)
final int thumbRes = resources.getIdentifier(extra,"drawable", packageName);
if (thumbRes != 0)
mThumbs.add(thumbRes);
我尝试了不同的方法来实现这一点,但都失败了,我真的可以使用帮助为我指明正确的方向。
这是我在布局中的 xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_ >
<GridView
android:id="@+id/gridview"
android:layout_
android:layout_
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
【问题讨论】:
LogCat 对此有何评论? @Ray Stojonic 当我运行 logcat 它给了我'code'10-06 19:10:27.450: E/AndroidRuntime(10180): FATAL EXCEPTION: main 10-06 19:10:27.450: E/AndroidRuntime(10180): java.lang.NullPointerException 10-06 19:10:27.450: E/AndroidRuntime(10180): at com.designrifts.ultimatethemeui.IconFragmentSystem.onCreateView(IconFragmentSystem.java:28) 所以我知道它是从 onCreateView。我查看了示例并阅读了 Android 网站,但没有任何运气。 【参考方案1】:我能够解决问题
我变了
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
到
GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
我也变了
gridview.setAdapter(new IconAdapter(this, iconSize));
到
gridview.setAdapter(new IconAdapter(getActivity(), iconSize));
【讨论】:
【参考方案2】:试试这个。
gridView = (GridView) rootView.findViewById(R.id.gridView);
gridView.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view,int pos, long l)
// do something...
);
有关详细信息,请参阅此链接: Android gridview in fragment adapter constructor missing
【讨论】:
【参考方案3】:GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
应该是
GridView gridview = (GridView) container.findViewById(R.id.icon_grid);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.main, container, false);
int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
gridview.setAdapter(new IconAdapter(this, iconSize));
gridview.setOnItemClickListener(this);
CONTENT_URI = Uri.parse("content://"+iconsProvider.class.getCanonicalName());
return view;
您要删除对 super 的调用,这是您活动的剩余部分,片段不调用此方法。
【讨论】:
我添加时仍然遇到同样的错误。我把 logcat 以及工作的代码版本pastebin.com/ByjyURxs 是 logcat。这是来自 Activity github.com/designrifts/Ultimate_Theme_UI_Template/blob/master/… 的工作版本 在oncreateview中也不要调用super 我确实删除了超级。谢谢!以上是关于Android ImageAdapter 与 Fragment 中的 Gridview的主要内容,如果未能解决你的问题,请参考以下文章
adapter.setOnClickItemListener ( (ImageAdapter.OnItemClickListener) getActivity( ) );此代码生成错误,我无法解决错误
如何使用 Android 的 PagerAdapter 类自动显示图像?