我正在处理一个包含特定 url 文件夹 Android 图像的画廊,我发现了一个有效的代码,但是当我在我的项目中添加这个代码时,活动崩溃了
这是Java代码:
package com.dvp.android.gallery;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
/**
* @author Micka�l Le Trocquer
*/
public class Galeries extends Activity {
//Adresse o� se trouve l'ensemble des images gif (num�rot�es de 1 � 21).
private final static String SERVER_IM = "http://10.0.2.2/www/Bardo/images/Galerie/";
// GUI
private Gallery gallery;
private ImageView imgView;
//Data
private ArrayList<URL> mListImages;
private Drawable mNoImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galeries);
//R�cup�ration d'une image par d�faut � afficher en cas d'erreur ou de liste vide
mNoImage = this.getResources().getDrawable(R.drawable.no_photo);
//Construction des URL pour les images
mListImages = buildListImages();
//R�cup�ration du composant affichant l'image en grand
imgView = (ImageView)findViewById(R.id.imageview);
//On lui met une image par d�faut (la premiere de la liste ou � d�faut l'image d'erreur)
if (mListImages.size() <= 0) {
imgView.setImageDrawable(mNoImage);
} else {
try {
setImage(imgView, mListImages.get(0));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//R�cup�ration du composant affichant la liste des vignettes
gallery = (Gallery) findViewById(R.id.gallery);
//On lui donne notre adapter qui s'occup�ra de l'alimenter en vignette
gallery.setAdapter(new AddImgAdp(this));
//Espacement entre les vignette
gallery.setSpacing(10);
//Lors d'un clic sur une des vignettes, on affiche l'image correspondante en grand
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, final int position, long id) {
new Thread(new Runnable() {
public void run() {
try {
setImage(imgView, mListImages.get(position));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
/**
* Permet de construire la liste des urls pour les images
* @return
*/
private ArrayList<URL> buildListImages() {
int nbTotalImage = 21;
ArrayList<URL> listFic = new ArrayList<URL>();
for(int i = 1; i <= nbTotalImage; i++) {
try {
listFic.add(new URL(SERVER_IM + "/" + i + ".gif"));
} catch (MalformedURLException e) {
Log.e("DVP Gallery", "Erreur format URL : " + SERVER_IM + "/" + i + ".gif");
e.printStackTrace();
}
}
return listFic;
}
/**
* Notre adapter qui g�re la liste des vignettes
* @author Micka�l Le Trocquer
*/
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return mListImages.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imgView = null;
//R�cyclage du composant
if (convertView == null) {
imgView = new ImageView(cont);
} else {
imgView = (ImageView)convertView;
}
//On affecte notre image � la vignette
if (mListImages.size() <= 0) {
imgView.setImageDrawable(mNoImage);
} else {
try {
setImage(imgView, mListImages.get(position));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//On d�fini la taille de l'image
imgView.setLayoutParams(new Gallery.LayoutParams(150, 150));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
//On fixe un arri�re plan plus sympa
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
/**
* M�thode permettant de t�l�charger une image depuis une URL et de l'affecter � un composant de type ImageView
* @param aView
* @param aURL
*/
public void setImage(ImageView aView, final URL aURL) throws IOException {
final Bitmap bm = null;
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
URLConnection conn = null;
try {
conn = aURL.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = null;
try {
is = conn.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Bufferisation pour le t�l�chargement
BufferedInputStream bis = new BufferedInputStream(is, 8192);
// Cr�ation de l'image depuis le flux des donn�es entrant
Bitmap bm = BitmapFactory.decodeStream(bis);
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
};
task.execute(null, null, null);
// Fixe l'image sur le composant ImageView
aView.setImageBitmap(bm);
}
}
logcat 输出
09-27 17:33:22.719: E/AndroidRuntime(2016): FATAL EXCEPTION: Thread-171
09-27 17:33:22.719: E/AndroidRuntime(2016): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-27 17:33:22.719: E/AndroidRuntime(2016): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
09-27 17:33:22.719: E/AndroidRuntime(2016): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:869)
09-27 17:33:22.719: E/AndroidRuntime(2016): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4253)
09-27 17:33:22.719: E/AndroidRuntime(2016): at android.view.View.invalidate(View.java:10539)
09-27 17:33:22.719: E/AndroidRuntime(2016): at android.view.View.invalidate(View.java:10494)
09-27 17:33:22.719: E/AndroidRuntime(2016): at android.widget.ImageView.setImageDrawable(ImageView.java:424)
09-27 17:33:22.719: E/AndroidRuntime(2016): at android.widget.ImageView.setImageBitmap(ImageView.java:437)
09-27 17:33:22.719: E/AndroidRuntime(2016): at com.dvp.android.gallery.Galeries.setImage(Galeries.java:231)
09-27 17:33:22.719: E/AndroidRuntime(2016): at com.dvp.android.gallery.Galeries$1$1.run(Galeries.java:83)
09-27 17:33:22.719: E/AndroidRuntime(2016): at java.lang.Thread.run(Thread.java:841)
09-27 17:33:22.719: E/SoundPool(289): error loading /system/media/audio/ui/KeypressReturn.ogg
我用谷歌搜索并没有发现有关此异常的信息。我真的很困惑这个问题,不知道如何解决它。有谁知道如何解决它?谢谢。