I am using endless recycler view to show multiple posts. but if my internet speed is slow and image not loaded on time when I click on imageview it crashes the app. I am sending url from activity to another through intent.
Here is the code
RecyclerAdapter.java
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String icon = jsonData.get((position)).getImg();
Intent intent = new Intent(context, Detail.class);
intent.putExtra("app_icon", icon);
}
context.startActivity(intent);
Detail.java
detail_icon = (ImageView) findViewById(R.id.detail_icon);
String appicon = getIntent().getStringExtra("app_icon");
try {
detail_icon.setImageBitmap(BitmapFactory.decodeStream((InputStream) new URL(appicon).getContent()));
}catch (IOException e2) {
e2.printStackTrace();
}
Crashing on this line while image is not loaded and I click on imageview.
detail_icon.setImageBitmap(BitmapFactory.decodeStream((InputStream) new URL(appicon).getContent()));
Logcat
Process: com.example.lenovo.connectiontest, PID: 26525
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo.connectiontest/com.example.lenovo.connectiontest.Activity.Detail}: android.os.NetworkOnMainThreadException
at com.example.lenovo.connectiontest.Activity.Detail.onCreate(Detail.java:233)
Yes its solved my problem
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your code goes here
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();