android 错误只有创建视图层次结构的原始线程可以触及它的视图
android error only the original thread that created a view hierarchy can touch its views
我有 class 名称 Main1,它从主 class 获取图像链接数组和文本数组,并将它们显示在 listview
上。我收到错误 "only the original thread that created a view hierarchy can touch its views" 所以我搜索解决方案我发现我使用 runOnUithread
但它只能在 oncreate
上使用所以我不知道那是解决方案。这是从 main1 class
得到的
main1 class 是
public class Main1 extends ArrayAdapter<String> {
private final Activity context;
private final String[] Cat_Name;
private final String[] logo;
public Main1(Activity context,String[] Cat_Name, String[] logo) {
super(context, R.layout.main1, Cat_Name);
this.context = context;
this.Cat_Name = Cat_Name;
this.logo = logo;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.main1, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1);
final ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
txtTitle.setText(Cat_Name[position]);
Thread thread = new Thread() {
@Override
public void run() {
try {
String img_url=logo[position]; //url of the image
// System.out.println(img_url);
URL url = new URL(img_url);
final Bitmap bmp;
bmp=BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread.start();
return rowView;
}
}
在 runOnUiThread 方法中设置图像,如下所示
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bmp);
}
});
希望对您有所帮助。
我有 class 名称 Main1,它从主 class 获取图像链接数组和文本数组,并将它们显示在 listview
上。我收到错误 "only the original thread that created a view hierarchy can touch its views" 所以我搜索解决方案我发现我使用 runOnUithread
但它只能在 oncreate
上使用所以我不知道那是解决方案。这是从 main1 class
main1 class 是
public class Main1 extends ArrayAdapter<String> {
private final Activity context;
private final String[] Cat_Name;
private final String[] logo;
public Main1(Activity context,String[] Cat_Name, String[] logo) {
super(context, R.layout.main1, Cat_Name);
this.context = context;
this.Cat_Name = Cat_Name;
this.logo = logo;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.main1, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1);
final ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
txtTitle.setText(Cat_Name[position]);
Thread thread = new Thread() {
@Override
public void run() {
try {
String img_url=logo[position]; //url of the image
// System.out.println(img_url);
URL url = new URL(img_url);
final Bitmap bmp;
bmp=BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread.start();
return rowView;
}
}
在 runOnUiThread 方法中设置图像,如下所示
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bmp);
}
});
希望对您有所帮助。