Question
Question

Reputation: 145

Crash with Glide on android app

My application closes every time I try to see the images. Can you explain why?

I think maybe the problem is the string Glide.with(getApplicationContext()), maybe it isn't correct. But for Android Studio I don't have errors:

All other elements are ok, the problem is only 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;
            }
        }
    }

Update: The show images don't follow the list of array. Need a solution.

    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();
    }

Edit:

    @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'"

This is the console error:

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)

Upvotes: 3

Views: 3715

Answers (2)

Adnan Afzal
Adnan Afzal

Reputation: 197

In my case, it was using Glide in a static method, thus causing memory leaks. Try using it directly in activity or fragment, not in static method.

Hope this helps someone.

Upvotes: 0

Reaz Murshed
Reaz Murshed

Reputation: 24211

Please check if you have added the INTERNET permission in your application manifest?

<?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>

And yes, it always good to have your glide image loading code segment inside a try/catch statement.

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

Update:

About the images wrongly placed - I could see that in your getView function, you did not find the ImageView properly. You need to find the ImageView inside convertView like the other items in the list like this.

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);

Upvotes: 2

Related Questions