【问题标题】:LayoutInflater in androidandroid中的LayoutInflater
【发布时间】:2016-03-16 08:47:24
【问题描述】:

当我在 android 中使用 layoutInflater 在列表中创建自定义布局时,然后在 LayoutInflater 的声明中给出错误无法访问的声明。如何解决它

public View getView(int position, View convertView, ViewGroup parent) {
        return super.getView(position, convertView, parent);

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View vw = inflater.inflate(R.layout.list_custom , parent,false);

        Tour tour = tours.get(position);
        TextView tv =(TextView) vw.findViewById(R.id.texttile);
        tv.setText(tour.getTitle());

       tv =(TextView) vw.findViewById(R.id.price);
        NumberFormat nf= NumberFormat.getCurrencyInstance();
        tv.setText(nf.format(tour.getPrice()));

        ImageView iv  =(ImageView) vw.findViewById(R.id.imageview);
        int imageResource  = context.getResources().getIdentifier(tour.getImage() , "drawable" , context.getPackageName());
        if (imageResource!=0){
            iv.setImageResource(imageResource);
        }
        return  vw;
    }

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 说无法到达的声明

【问题讨论】:

  • 你看到return 声明了吗?
  • 这正是警告所说的内容。
  • 它将执行返回行并退出该方法。只需将代码放在 return 语句之前。
  • 这是编程的基础知识:Every method should have only a single exit point

标签: android


【解决方案1】:

你的问题

  return super.getView(position, convertView, parent);

删除此return

实际上是错误的return语句创建了un-reachable statement

我们不会将 return 语句放在任何其他语句之上,除非 return 在任何条件语句下。

感谢

I get the error "Unreachable statement" return in android

【讨论】:

    【解决方案2】:

    删除线
    return super.getView(position, convertView, parent);

    【讨论】:

    【解决方案3】:
       return super.getView(position, convertView, parent);
    

    *删除返回关键字 删除后

    super.getView(position, convertView, parent);
    

    【讨论】:

    • 感谢 StackOverflow 的帮助。答案基本上是正确且有建设性的,但为了清晰和可读性需要重新措辞。
    猜你喜欢
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多