android问题 LayoutInflater.from
我是android新手各位大神帮忙:
android程序 在View view = LayoutInflater.from(getContext()).inflate(resourceId, null); 这一句运行不下去额=了 是怎么回事
package com.example.listviewtest;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
private String msg = "This is a logcat";
public FruitAdapter(Context context,int textViewResourceId,List<Fruit> objects){
super(context,textViewResourceId,objects);
resourceId = textViewResourceId;
Log.d("FruitAdapter", msg+"FruitAdapter()");
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
Log.d("FruitAdapter", msg+"getView()"+"BEGIN");
Fruit fruit = getItem(position);
// View view =LayoutInflater.from(getContext()).inflate(resourceId, null);
Log.d("FruitAdapter", msg+"getView()"+"position1");
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
Log.d("FruitAdapter", msg+"getView()"+"position2");
ImageView fruitImage = (ImageView)view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView)view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
Log.d("FruitAdapter", msg+"getView()"+"END");
return view;
}
}
最近刚刚学android 上面是一个FruitAdapter适配器类,为什么就在
View view = LayoutInflater.from(getContext()).inflate(resourceId, null); 这一句的位置上时就运行不下去了呀?上面我使用logcat打印信息来确认的
Log.d("FruitAdapter", msg+"getView()"+"position1");这句可以打印出来
但是Log.d("FruitAdapter", msg+"getView()"+"position2");这句变出不来了,这是为什么?(另:文中没有报错显示,在安卓机上效果是闪退。)
布局文件用的是什么布局,fragment应该是一个控件吧,改成LinearLayout, RelativeLayout这样的布局标签,注意选择的布局标签中要包含你现在布局文件中ImageView,TextView使用的属性
resourceId你这个传进来是不是对应的xml布局啊,比如R.layout.**.xml
是的呀 这个是 布局的文件 不觉得有问题啊
试一下将xml布局文件的Fragment该为FrameLayout
写成,inflat(id,parent,false)试试
你还没传值呢。把布局写上去就好了