我是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使用的屬性