我正在尝试给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" />
答案 0 :(得分: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);