为什么Imageloader无法正常工作?

时间:2016-02-08 06:12:26

标签: android android-viewpager universal-image-loader android-pageradapter

  

我想在视图pager中显示图片。我创建了一个班级pageradapter并使用imageloader library从互联网上获取图片,但屏幕上没有任何内容。

     

我的日志猫一行红色:

02-08 12:34:12.582 3560-3560/? E/libEGL: call to OpenGL ES API with no current context (logged once per thread)

我的pager adapter课程:

package autogenie.dg10;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

public class pageradapter extends PagerAdapter {
    Context mContext;
    LayoutInflater mLayoutInflater;
    List<String> l = MainActivity.list;
    ImageLoader mImageLoader;

    public pageradapter(Context context) {
        mContext = context;

        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return 4;
    }



    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.img, container, false);
        ImageLoader mImageLoader = ImageLoader.getInstance();
        mImageLoader.init(ImageLoaderConfiguration.createDefault(mContext));



        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView3);
        mImageLoader.displayImage("http://www.airtel.in/4g/images/airtel_4g_hotspot_responsive.jpg", imageView);

        container.addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
}

myapplication class:

package autogenie.dg10;


import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.provider.SyncStateContract;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class MyApplication extends Application {

    @Override
    public void onCreate() {


        super.onCreate();

        initImageLoader(getApplicationContext());
    }

    public static void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,
        // or you can create default configuration by
        //  ImageLoaderConfiguration.createDefault(this);
        // method.
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());
    }
}

主要活动类:

package autogenie.dg10;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import android.widget.Toast;

import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {
    InputStream i;
    String theString = "";
    static List<String> list = new ArrayList<>();

    Context mContext = this;
    JSONArray k;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            i = getAssets().open("swipperesponse.json");

        } catch (IOException e) {

        }

        Scanner scanner = new Scanner(i, "UTF-8");
        while (scanner.hasNext()) {
            theString = theString + scanner.next();

        }
        try {
            JSONObject js = new JSONObject(theString);
            k = js.getJSONArray("swipe");
            for (int i = 0; i < k.length(); i++) {
                try {
                    JSONObject oneObject = k.getJSONObject(i);
                    // Pulling items from the array
                    // Toast.makeText(this,oneObject.getString("productImageURL"), Toast.LENGTH_LONG).show();
                    String d = oneObject.getString("productImageURL");
                    list.add(i, d);
                    int l = 0;
                    int u = 0;
                } catch (JSONException e) {
                    // Oops
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


        pageradapter p = new pageradapter(this);

        ViewPager mViewPager = (ViewPager) findViewById(R.id.view);
        mViewPager.setAdapter(p);



    }


}
  

我无法弄清楚出了什么问题。运行app时未报告任何错误。

img.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="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView3"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="225dp" />



</LinearLayout>

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="autogenie.dg10">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

mainactivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<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: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="autogenie.dg10.MainActivity">


    <LinearLayout
        android:weightSum="2"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="270dp"
        android:id="@+id/linearLayout4"
        android:layout_below="@+id/view"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_alignParentBottom="true"
            android:weightSum="2"
            android:id="@+id/linearLayout3"
            android:layout_alignEnd="@+id/linearLayout2"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/imageView5"
                android:background="@drawable/a"
                android:layout_marginRight="10dp" />

            <ImageView
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imageView6"
                android:layout_gravity="bottom"
                android:background="@drawable/aa"
                android:layout_marginLeft="10dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:weightSum="2"
            android:layout_alignParentEnd="true"
            android:id="@+id/linearLayout"
            android:layout_alignParentStart="true"
            android:layout_marginTop="10dp">

            <ImageView
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imageView"
                android:background="@drawable/aaa"
                android:baselineAlignBottom="false"
                android:layout_marginRight="10dp" />

            <ImageView
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/imageView2"
                android:layout_gravity="bottom"
                android:background="@drawable/aaaa"
                android:visibility="visible"
                android:layout_marginLeft="10dp" />
        </LinearLayout>
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:id="@+id/view"
        android:layout_centerHorizontal="true" />

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:id="@+id/horizontalScrollView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/linearLayout4"
        android:layout_below="@+id/linearLayout4" />


</RelativeLayout>

MY LOG CAT:

  

02-08 12:34:12.374 3560-3560 /? W / dalvikvm:VFY:无法找到课程   在签名中引用(Landroid / view / SearchEvent;)02-08   12:34:12.374 3560-3560 /? I / dalvikvm:找不到方法   android.view.Window $ Callback.onSearchRequested,从方法引用   android.support.v7.view.WindowCallbackWrapper.onSearchRequested 02-08   12:34:12.374 3560-3560 /? W / dalvikvm:VFY:无法解析界面   方法16217:Landroid / view / Window $ Callback; .onSearchRequested   (Landroid / view / SearchEvent;)Z 02-08 12:34:12.374 3560-3560 /?   I / dalvikvm:找不到方法   android.view.Window $ Callback.onWindowStartingActionMode,引用   从方法   android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode   02-08 12:34:12.374 3560-3560 /? W / dalvikvm:VFY:无法解决   接口方法16221:   Landroid /视图/窗口$回调; .onWindowStartingActionMode   (Landroid /视图/ ActionMode $回调; I)Landroid /视图/ ActionMode; 02-08   12:34:12.378 3560-3560 /? I / dalvikvm:找不到方法   android.content.res.TypedArray.getChangingConfigurations,引用   从方法   android.support.v7.widget.TintTypedArray.getChangingConfigurations   02-08 12:34:12.378 3560-3560 /? W / dalvikvm:VFY:无法解决   虚方法414:   Landroid / content / res / TypedArray; .getChangingConfigurations()I 02-08   12:34:12.382 3560-3560 /? I / dalvikvm:找不到方法   android.content.res.TypedArray.getType,从方法引用   android.support.v7.widget.TintTypedArray.getType 02-08 12:34:12.382   3560-3560 /? W / dalvikvm:VFY:无法解析虚方法436:   Landroid / content / res / TypedArray; .getType(I)I 02-08 12:34:12.394   3560-3560 /? I / dalvikvm-heap:将堆(frag case)增长到3.753MB for   1125052字节分配02-08 12:34:12.414 3560-3560 /?   I / dalvikvm-heap:将堆(frag case)增长到8.041MB,用于4500172字节   分配02-08 12:34:12.458 3560-3560 /? I / dalvikvm-heap:增长堆   (frag case)为12:3434MB,用于4500172字节分配02-08 12:34:12.494   3560-3560 /? I / dalvikvm-heap:将堆(frag case)增长到16.627MB   4500172字节分配02-08 12:34:12.538 3560-3560 /?   I / dalvikvm-heap:将堆(frag case)增长到20.920MB,用于4500172字节   分配02-08 12:34:12.582 3560-3560 /? E / libEGL:调用OpenGL ES   没有当前上下文的API(每个线程记录一次)02-08   12:34:12.626 3560-3560 /? W / EGL_genymotion:eglSurfaceAttrib没有   实施

1 个答案:

答案 0 :(得分:0)

为什么你两次打电话给ImageLoader.init()

instantiateItem方法

中删除此行
mImageLoader.init(ImageLoaderConfiguration.createDefault(mContext));

你试图加载的图像不存在

http://www.airtel.in/4g/images/airtel_4g_hotspot_responsive.jpg

此路径显示403错误