在Android应用中使用Glide时出现崩溃

3
我的应用程序每次尝试查看图像时都会关闭。你能解释一下原因吗?
我认为问题可能是字符串“Glide.with(getApplicationContext())”,可能不正确。但是对于Android Studio,我没有错误:
所有其他元素都没问题,问题只出现在Glide上。
public class MainActivity extends AppCompatActivity {

    private final String url= "MY ARRAY";
    ...

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        new gson().execute(url);
    }

    public class gson extends AsyncTask<String,String, List<MovieModel> >{

    ....
public class Adapter extends ArrayAdapter{
    private List<MovieModel> movieModelList;
    private int resource;
    private LayoutInflater inflater;
    public Adapter(Context context, int resource, List<MovieModel> objects) {
        super(context, resource, objects);
        movieModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.fruitsfoto = (ImageView)convertView.findViewById(R.id.fruits);
            holder.fruitsname = (TextView)convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageView imageView = (ImageView) findViewById(R.id.pics);
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
try {
Glide.with(MainActivity.this)//getApplicationContext()
        .load(ModelList.get(position).getImage())
        .listener(new RequestListener<String, GlideDrawable>() {
    @Override
    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
        return false;
    }
    @Override
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
        progressBar.setVisibility(View.GONE);
        return false;
    }
})
        .into(imageView);
} catch (Exception e) {
    e.printStackTrace();
}
holder.fruitsname.setText(ModelList.get(position).getName());
return convertView;
            }
        }
    }

更新:展示的图片不按照数组列表的顺序显示。需要解决方案。
    public Adapter(Context context, int resource, List<MovieModel> objects) {
        super(context, resource, objects);
        ModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }
.....
    try {
    Glide.with(MainActivity.this)//getApplicationContext()
            .load(ModelList.get(position).getFruits())
            .into(imageView);
    } catch (Exception e) {
        e.printStackTrace();
    }

编辑:
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if(convertView == null){
        holder = new ViewHolder();
        convertView = inflater.inflate(resource, null);
        holder.fruitsfoto = (ImageView)convertView.findViewById(R.id.fruits);
        holder.fruitsname = (TextView)convertView.findViewById(R.id.name);
1)holder.image = (TextView)convertView.findViewById(R.id.pics);
2)ImageView imageView = (ImageView)convertView.findViewById(R.id.pics);            
convertView.setTag(holder);
//the first cause a crash
//the second return "Variable 'imageView is never used'"

这是控制台错误信息:

09-28 17:19:42.079 30111-30111/app E/AndroidRuntime: FATAL EXCEPTION: main
                                            Process: app, PID: 30111
                                            java.lang.NullPointerException
                                            at app$1.onResourceReady(MainActivity.java:309)
                                            at app$1.onResourceReady(MainActivity.java:301)
                                            at com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:522)
                                            at com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:507)
                                            at com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:158)
                                            at com.bumptech.glide.load.engine.EngineJob.access$100(EngineJob.java:22)
                                            at com.bumptech.glide.load.engine.EngineJob$MainThreadCallback.handleMessage(EngineJob.java:202)
                                            at android.os.Handler.dispatchMessage(Handler.java:98)
                                            at android.os.Looper.loop(Looper.java:146)
                                            at android.app.ActivityThread.main(ActivityThread.java:5511)
                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                            at dalvik.system.NativeStart.main(Native Method)

2
发布 logcat 错误 - Nikhil
1
用 Glide.with(MainActivity.this) 替换 Glide.with(getApplicationContext()) - Surya Prakash Kushawah
如果您遇到图像错误放置的问题,请随时在此处分享您的适配器代码。 - Reaz Murshed
你的适配器代码不足。bindView方法在哪里?希望Glide能够在bindView方法内加载图像。请删除...并粘贴完整的适配器代码。 - Reaz Murshed
2个回答

2
请检查您的应用程序清单中是否添加了INTERNET权限?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="viewpager.nested.example.com.testapplicationforso">

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

    <application
        ...
    </application>

</manifest>

是的,将滑动图像加载代码段放在try/catch语句中总是很好的做法。

try {
    Glide.with(getApplicationContext()).load(yourUrl).into(img);
} catch (Exception e) {
    e.printStackTrace();
}

更新:

关于错误放置的图片 - 我可以看到在你的getView函数中,你没有正确地找到ImageView。你需要像列表中的其他项目一样,在convertView内查找ImageView,如下所示。

holder.image = (TextView)convertView.findViewById(R.id.pics);

// Then load the image in holder.image
Glide.with(MainActivity.this)//getApplicationContext()
    .load(ModelList.get(position).getFruits())
    .into(holder.image);

我更改了代码以尝试您的解决方案:try { Glide.with(MainActivity.this)//getApplicationContext() .load(movieModelList.get(position).getImage()) .into(imageView); } catch (Exception e) { e.printStackTrace(); }它有效,我可以看到图片,但是我有不同的问题:显示的图像不遵循数组列表:例如,苹果的照片出现在香蕉列表中,如果向下滑动,它会从香蕉列表中消失,并代之以香蕉的照片。为什么我没有按正确顺序看到它们? - Question
我在你的问题下面也添加了一条评论。请在问题中分享您的适配器代码。尝试使用适配器代码编辑您的问题。 - Reaz Murshed
我修改了我的帖子。 - Question
现在试一下。 - Question
1
请您检查更新后的答案。您需要将图像加载到holder.image中。 - Reaz Murshed
显示剩余2条评论

0
在我的情况下,是在静态方法中使用Glide,从而导致内存泄漏。尝试直接在活动或片段中使用它,而不是在静态方法中使用。
希望这能帮助到某些人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接