问题描述
我有一个GridView的一个适配器,可以作为一个活动。我试图把它放在一个片段,现在和转换的东西,但它不工作。当我包括我的活动IconFragmentSystem我得到一个强制关闭,当我尝试打开活动。
我知道这个活动的作品,因为我可以用其他的片段,并一切正常,所以我知道这个文件在我的问题所在。
包com.designrifts.ultimatethemeui;
进口com.designrifts.ultimatethemeui.R;
进口android.content.Context;
进口android.content.Intent;
进口android.content.res.Resources;
进口android.net.Uri;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget *。
进口的java.util.ArrayList;
公共类IconFragmentSystem扩展片段实现AdapterView.OnItemClickListener {
私有静态最后弦乐RESULT_OK = NULL;
公众开放的CONTENT_URI;
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.main,集装箱,假);
super.onCreate(savedInstanceState);
INT iconSize = getResources()getDimensionPixelSize(android.R.dimen.app_icon_size)。
GridView控件的GridView =(GridView控件)getActivity()findViewById(R.id.icon_grid)。
gridview.setAdapter(新IconAdapter(这一点,iconSize));
gridview.setOnItemClickListener(本);
CONTENT_URI = Uri.parse(内容://+ iconsProvider.class.getCanonicalName());
返回查看;
}
@覆盖
公共无效onItemClick(适配器视图<>适配器视图,视图中查看,INT I,长L){
字符串图标= adapterView.getItemAtPosition(I)的ToString();
意向书结果=新的意图(空,Uri.withAppendedPath(CONTENT_URI,图标));
的setResult(RESULT_OK,结果);
完();
}
私人无效的setResult(字符串resultOk,意向结果){
// TODO自动生成方法存根
}
私人无效结束(){
// TODO自动生成方法存根
}
私有类IconAdapter扩展了BaseAdapter {
私人语境mContext;
私人诠释mIconSize;
公共IconAdapter(上下文mContext,INT iconsize){
超();
this.mContext = mContext;
this.mIconSize = iconsize;
loadIcon();
}
公共IconAdapter(IconFragmentSystem iconssystem,INT iconSize){
// TODO自动生成构造函数存根
}
@覆盖
公众诠释getCount将(){
返回mThumbs.size();
}
@覆盖
公共对象的getItem(INT位置){
返回mThumbs.get(位置);
}
@覆盖
众长getItemId(INT位置){
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ImageView的ImageView的;
如果(convertView == NULL){
ImageView的=新ImageView的(mContext);
imageView.setLayoutParams(新GridView.LayoutParams(mIconSize,mIconSize));
} 其他 {
ImageView的=(ImageView的)convertView;
}
imageView.setImageResource(mThumbs.get(位置));
返回ImageView的;
}
私人的ArrayList<整数GT; mThumbs;
////////////////////////////////////////////////
私人无效loadIcon(){
mThumbs =新的ArrayList<整数GT;();
最后的资源资源= getResources();
最后弦乐的packageName = getActivity()getApplication()getPackageName()。
addIcon(资源的packageName,R.array.systemicons);
}
私人无效addIcon(资源资源,串的packageName,INT表){
最终的String []演员= resources.getStringArray(名单);
对于(字符串加:演员){
INT RES = resources.getIdentifier(多余的,绘制的packageName);
如果(水库!= 0){
最终诠释thumbRes = resources.getIdentifier(多余的,绘制的packageName);
如果(thumbRes!= 0){
mThumbs.add(thumbRes);
}
}
}
}
}
}
我曾尝试不同的方式来实现这一点,但都失败了,我真的可以使用的帮助指点我在正确的方向。
这是我在布局XML
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< GridView控件
机器人:ID =@ + ID / GridView控件
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:columnWidth中=90dp
机器人:为numColumns =auto_fit
机器人:verticalSpacing =10dp
机器人:horizontalSpacing =10dp
机器人:stretchMode =columnWidth中
机器人:重力=中心
/>
< / RelativeLayout的>
我是能够解决这个问题。
我改变了
的GridView的GridView =(GridView控件)getActivity()findViewById(R.id.icon_grid)。
到
的GridView的GridView =(GridView控件)view.findViewById(R.id.icon_grid);
和我也改变了
gridview.setAdapter(新IconAdapter(这一点,iconSize));
到
gridview.setAdapter(新IconAdapter(getActivity(),iconSize));
I have an adapter with gridview that works as an Activity. I am trying to place it in a Fragment now and converted things but it does not work. When I include the IconFragmentSystem in my Activity I get a force close when I try to open the Activity.
I know the Activity works because I can use other Fragments and everything is okay so I know my issue lies within this file.
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);
}
}
}
}
}
}
I have tried different ways to implement this but all have failed and I could really use help pointing me in the right direction.
This is my xml in layout
<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_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
I was able to fix the issue
I changed
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
to
GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
and I also changed
gridview.setAdapter(new IconAdapter(this, iconSize));
to
gridview.setAdapter(new IconAdapter(getActivity(), iconSize));
这篇关于Android的ImageAdapter用的GridView的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!