0

我正在尝试为 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" />

4

1 回答 1

0

问题是您正在从无法访问的方法返回视图

问题:

    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); 
于 2014-06-08T16:50:35.707 回答