繁体   English   中英

ImageView充气机无法访问的代码

[英]ImageView inflater unreachable code

我正在尝试给ImageView充气,但是android表示这是无法访问的代码ImageView imageView = (ImageView) row.findViewById(R.id.imageV); 我必须将其删除。 哪里有错误?

package com.examples.listimage;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListImage extends Activity {
String[] myImages;
String[] descript;
int [] images = {R.drawable.ic_launcher, R.drawable.red, R.drawable.strange};
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_image);
    Resources res = getResources();
    myImages = res.getStringArray(R.array.images);
    descript = res.getStringArray(R.array.desc);
    list = (ListView) findViewById(R.id.listView1);

}
class myAdapter extends ArrayAdapter<String>{
Context context;
myAdapter (Context c, String[]images){

    super(c, R.layout.example, R.id.textView1,images);
    this.context = c;

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater myInflater = (LayoutInflater)
    context.getSystemService(contex.LAYOUT_INFLATER_SERVICE);                                  
View row = myInflater.inflate(R.layout.example, parent, false);
return super.getView(position, convertView, parent);

ImageView imageView = (ImageView) row.findViewById(R.id.imageV);
TextView textV1 = (TextView) row.findViewById(R.id.textView1);
TextView textV2 = (TextView) row.findViewById(R.id.textView2);
}

}}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/imageV"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="14dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageV"
    android:layout_below="@+id/imageV"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView1"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

问题是您正在从方法返回视图,为什么它不可达

问题:

    return super.getView(position, convertView, parent); //you return from here code below will unredchable

ImageView imageView = (ImageView) row.findViewById(R.id.imageV);
TextView textV1 = (TextView) row.findViewById(R.id.textView1);
TextView textV2 = (TextView) row.findViewById(R.id.textView2);

解:

从方法返回之前添加它

ImageView imageView = (ImageView) row.findViewById(R.id.imageV);
    TextView textV1 = (TextView) row.findViewById(R.id.textView1);
    TextView textV2 = (TextView) row.findViewById(R.id.textView2);
    return super.getView(position, convertView, parent); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM