在我的Android应用程序中,我正在尝试创建一个列表,我需要在每个项目列表中显示一个图像。我使用Drawable来显示URL中的图像。我在ArrayAdapter中执行此操作。该应用程序崩溃并显示错误。
下面是我的ArrayAdapter和logcat。你的代码有什么问题?请帮我。提前谢谢!
package com.makemyandroidapp.example.stacksites;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import android.view.View.OnClickListener;
/*
* Custom Adapter class that is responsible for holding the list of sites after they
* get parsed out of XML and building row views to display them on the screen.
*/
public class CategoryAdapter extends ArrayAdapter<StackSite> {
//ImageLoader imageLoader;
//DisplayImageOptions options;
Context c;
String url1;
String url2;
Drawable backgr;
public CategoryAdapter(Context ctx, int textViewResourceId, List<StackSite> sites) {
super(ctx, textViewResourceId, sites);
c=ctx;
//Setup the ImageLoader, we'll use this to display our images
// ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ctx).build();
// imageLoader = ImageLoader.getInstance();
// imageLoader.init(config);
//Setup options for ImageLoader so it will handle caching for us.
// options = new DisplayImageOptions.Builder()
//.cacheInMemory()
//.cacheOnDisc()
//.build();
}
/*
* (non-Javadoc)
* @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*
* This method is responsible for creating row views out of a StackSite object that can be put
* into our ListView
*/
@Override
public View getView(int pos, View convertView, ViewGroup parent){
RelativeLayout row = (RelativeLayout)convertView;
Log.i("StackSites", "getView pos = " + pos);
if(null == row){
//No recycled View, we have to inflate one.
LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (RelativeLayout)inflater.inflate(R.layout.row_site, null);
}
//Get our View References
final ImageView iconImg = (ImageView)row.findViewById(R.id.iconImg);
final TextView kompaniaTxt = (TextView)row.findViewById(R.id.nameTxt);
TextView pozicioniTxt = (TextView)row.findViewById(R.id.aboutTxt);
final TextView kategoriaTxt = (TextView)row.findViewById(R.id.kategoriaTxt);
TextView qytetiTxt = (TextView)row.findViewById(R.id.qytetiTxt);
final ProgressBar indicator = (ProgressBar)row.findViewById(R.id.progress);
kompaniaTxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//String emri_komp=kompaniaTxt.getText().toString().replaceAll("\\s+","%20");
String emri_komp=kompaniaTxt.getText().toString();
Intent intent=new Intent(c,CompanyDesc.class);
intent.putExtra("emri_komp", emri_komp);
intent.putExtra("url1", url1);
c.startActivity(intent);
}
});
kategoriaTxt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//String emri_komp=kompaniaTxt.getText().toString().replaceAll("\\s+","%20");
String idKateg=kategoriaTxt.getText().toString();
Intent intent=new Intent(c,CategoryList.class);
intent.putExtra("idKateg", idKateg);
intent.putExtra("url2", url2);
c.startActivity(intent);
}
});
//Initially we want the progress indicator visible, and the image invisible
//indicator.setVisibility(View.VISIBLE);
//iconImg.setVisibility(View.INVISIBLE);
//Setup a listener we can use to swtich from the loading indicator to the Image once it's ready
// ImageLoadingListener listener = new ImageLoadingListener(){
//
//
//
// @Override
// public void onLoadingStarted(String arg0, View arg1) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onLoadingCancelled(String arg0, View arg1) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
// indicator.setVisibility(View.INVISIBLE);
// iconImg.setVisibility(View.VISIBLE);
// }
//
// @Override
// public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
// // TODO Auto-generated method stub
//
// }
//
// };
//
//Load the image and use our options so caching is handled.
//imageLoader.displayImage(getItem(pos).getImgUrl(), iconImg,options, listener);
//Set the relavent text in our TextViews
kompaniaTxt.setText(getItem(pos).getKompania());
pozicioniTxt.setText(getItem(pos).getPozicioni());
kategoriaTxt.setText(getItem(pos).getKategoria());
qytetiTxt.setText(getItem(pos).getQyteti());
url1=getItem(pos).getImgUrl();
//url1=getItem(pos).getIdKategoria();
try {
backgr=drawable_from_url(getItem(pos).getImgUrl(),"kot");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iconImg.setBackground(backgr);
return row;
}
Drawable drawable_from_url(String url, String src_name) throws
java.net.MalformedURLException, java.io.IOException
{System.out.println("uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");
return Drawable.createFromStream(((java.io.InputStream)
new java.net.URL(url.trim()).getContent()), src_name);
}
}
logcat的:
05-12 01:13:07.011: E/AndroidRuntime(1665): FATAL EXCEPTION: main
05-12 01:13:07.011: E/AndroidRuntime(1665): Process: com.makemyandroidapp.example.stacksites, PID: 1665
05-12 01:13:07.011: E/AndroidRuntime(1665): android.os.NetworkOnMainThreadException
05-12 01:13:07.011: E/AndroidRuntime(1665): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
05-12 01:13:07.011: E/AndroidRuntime(1665): at com.makemyandroidapp.example.stacksites.CategoryAdapter.drawable_from_url(CategoryAdapter.java:187)
05-12 01:13:07.011: E/AndroidRuntime(1665): at com.makemyandroidapp.example.stacksites.CategoryAdapter.getView(CategoryAdapter.java:165)
第187行是:
new java.net.URL(url.trim()).getContent()), src_name);
第165行是:
backgr=drawable_from_url(getItem(pos).getImgUrl(),"kot");
答案 0 :(得分:0)
由于您已经在使用Android通用图像加载器。从网址加载图片应该非常简单。这也适用于适配器。
imageLoader.displayImage(imageUri, imageView);
在你的情况下:
ImageLoader imageloader = ImageLoader.getInstance();
imageLoader.displayImage(url1, iconImg);
答案 1 :(得分:0)
android.os.NetworkOnMainThreadException
您正在执行从主线程上的url获取图像的网络操作,这在Android版本的Honeycomb及更高版本上是不允许的。使用AsyncTask代替将在后台的单独线程中运行代码。
答案 2 :(得分:0)
当您尝试在主线程中执行netwok操作时,会出现此异常,建议使用AsyncTask执行此操作。
但是如果我们的代码很多,并且再次管理代码看起来很整洁。
然后添加
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
在 onCreate()方法中。
这是你的问题。
请尝试让我知道