简体   繁体   中英

Unable to get image from url

I need to get images from url and set it in a listview

Here is my main xml file:

<RelativeLayout 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:background="#ffffff" >

<ScrollView
    android:id="@+id/scrollFeed"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="40sp"
            android:background="#d2d2d2"
            android:clickable="true"
            android:gravity="center"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:text="^\nRefresh" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="15000sp" >
        </ListView>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="100" 
            >

            <TextView
                android:id="@+id/nextPosts"
                android:layout_width="match_parent"
                android:layout_height="40sp"
                android:background="#d2d2d2"
                android:clickable="true"
                android:gravity="center"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:text="next >>"
                android:layout_weight="50" />

            <TextView
                android:id="@+id/previousPosts"
                android:layout_width="match_parent"
                android:layout_height="40sp"
                android:background="#d2d2d2"
                android:clickable="true"
                android:gravity="center"
                android:layout_weight="50"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:text=" previous" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

list_item.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:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#abc0e3"
    android:gravity="right"
    android:text="TextView"
    android:textColor="#b1b1b1" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:autoLink="web"
    android:linksClickable="true"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:text="TextView" />

The error appears at image.setImageResource(R.drawable.cover) as NULLPOINTEREXCEPTION

Here's the code:

if (c.has("picture") == true) {
    try {
        Log.d("IT HAS PICTURE! TADA!!", "Image Found");
        imgUrl = c.getString("picture");
//      Thread thread = new Thread(){
//      public void run(){
        System.out.println("Thread Running");
        NewsFeed.this.runOnUiThread(new Runnable(){
            @Override 
            public void run() { 
            //Your code to run in GUI thread here 
            URL newUrl = null;
            try {
                newUrl = new URL(imgUrl);
            } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            myBitmap = getBitmapFromUrl(newUrl);
            image.setImageBitmap(myBitmap);
            }
        });
//    }
//  };
    thread.start();
    } catch (Exception e) {
    Log.d("ERROR LOADING IMAGE","Why you do this, InputStream? Why?");
    }
}else{
    NewsFeed.this.runOnUiThread(new Runnable(){
    @Override 
    public void run() { 
        //Your code to run in GUI thread here 
        image.setImageResource(R.drawable.cover);
    }
    });
    }

You can't achieve it with a default adapter. You need to create a custom adapter for listview.

In getView method of adapter you need to inflate the list item layout and then have to bind the image to image view.

You can refer this for example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM