Android 为什么图像不显示在列表视图中?

Android 为什么图像不显示在列表视图中?,android,Android,这是密码 图像未显示在列表视图中如何解决此问题?我将uri值设置为位图。那么问题出在哪里?更改了数组并解决了错误,但静态图像未显示在列表视图中。 MainActivity.java public class MainActivity extends Activity { ListView lv; Context context; ArrayList prgmName; Bitmap bitmap; //bitmap=getBitmapFromUrl("h

这是密码 图像未显示在列表视图中如何解决此问题?我将uri值设置为位图。那么问题出在哪里?更改了数组并解决了错误,但静态图像未显示在列表视图中。 MainActivity.java

public class MainActivity extends Activity {
    ListView lv;
    Context context;

    ArrayList prgmName;
    Bitmap bitmap;
    //bitmap=getBitmapFromUrl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg");

    public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bitmap=getBitmapFromUrl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg");
        context=this;
        Bitmap  [] prgmImages ={bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap};
        lv=(ListView) findViewById(R.id.listView);
        lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private Bitmap getBitmapFromUrl(String src) {
        // TODO Auto-generated method stub
        try {

            URL url =new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input=connection.getInputStream();
            Bitmap mybiBitmap=BitmapFactory.decodeStream(input);
            return mybiBitmap;


         } catch (Exception e) {
            // TODO: handle exception
           e.printStackTrace();
           return null;
         }


    }

}
CustomAdapter.java

public class CustomAdapter extends BaseAdapter{
    String [] result;
    Context context;
 Bitmap[] imageId;
      private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, Bitmap[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
         inflater = ( LayoutInflater )context.
                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;        
             rowView = inflater.inflate(R.layout.list_item, null);
             holder.tv=(TextView) rowView.findViewById(R.id.textView1);
             holder.img=(ImageView) rowView.findViewById(R.id.imageView1);       
//         holder.tv.setText(result[position]);
//         holder.img.setImageBitmap(imageId[position]);         
             if(result != null && result.length > position)
             { 
               holder.tv.setText(result[position]);
             }

             if(imageId != null && imageId.length > 0)
             {
                holder.img.setImageBitmap(imageId[0]); 
             }

             rowView.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });  
        return rowView;
    }

}
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text=" Your Theaters..." />

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
    </LinearLayout>
list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_gravity="center"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="TextView" />


</LinearLayout>

这可能是因为在位图加载并保存到数组之前,需要调用适配器。下载图像需要一些时间,并且适配器没有等待图像。请参见此示例,它正在使用映像加载程序:

日志中是否存在NetworkOnMainThreadException

从onCreate调用getBitmapFromUrl时,您正在主线程上下载位图。在您的情况下,它将无声地失败,因为您正在捕获异常,但您将得到一个空结果

由于从网络加载图像非常常见,所以有很多库可以处理这个问题,更像是为您提供缓存,例如。
或者

也发布您的xml布局列表项目文件。检查日志您是否收到任何错误您是否在清单中添加了权限?当位图已下载时,您需要在适配器中调用notifyDataSetChanged。但在本例中,url包含xml文件,但在我的示例中,url不包含xml文件,因此如何解析此文件?我的url是,你不需要解析。只需输入适配器的getView:imageLoader.DisplayImage,thumb_image;如何解析我的url?我得到了空指针异常,因为这个url没有节点名?我正在分析这个url,因为我想要电影的名称和电影的图像,我如何才能做到这一点?