普通网友 2016-11-07 11:23 采纳率: 75%
浏览 1697
已采纳

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");这句变出不来了,这是为什么?(另:文中没有报错显示,在安卓机上效果是闪退。)

  • 写回答

7条回答 默认 最新

  • developerzjy 2016-11-08 02:30
    关注

    布局文件用的是什么布局,fragment应该是一个控件吧,改成LinearLayout, RelativeLayout这样的布局标签,注意选择的布局标签中要包含你现在布局文件中ImageView,TextView使用的属性

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏