I have made an app that fetches/displays videos from the phone's storage. There is this button that when clicked goes to an activity that displays the videos from the phone's storage.
When it's clicked for the first time after launching it from my IDE it opens the activity but when you exit the app and come back later and click the button again it crushes. I have the logcat errors written below. I don't know what's going on. I'll be glad if you guys helped me.
Thank you in advance.
This is my RecyclerView adapter.
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {
private Context context;
private ArrayList<VideoModel> videoModels;
public VideoAdapter(Context context, ArrayList<VideoModel> videoModels) {
this.context = context;
this.videoModels = videoModels;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.lists_of_videos, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final VideoModel vidModel = videoModels.get(position);
Glide.with(context).load(vidModel.getData()).into(holder.video_thumb);
holder.duration.setText(vidModel.getDuration());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (vidModel.getData() != null){
Intent intent = new Intent(context, SelectedVideoActivity.class);
intent.putExtra("videoURL", vidModel.getId());
context.startActivity(intent);
}else {
Toast.makeText(context, "Invalid video", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return videoModels.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView video_thumb;
TextView duration;
public ViewHolder(@NonNull View itemView) {
super(itemView);
video_thumb = itemView.findViewById(R.id.video_thumb);
duration = itemView.findViewById(R.id.duration);
}
}
}
And this is the Activity
public class PostActivity extends AppCompatActivity {
private ArrayList<VideoModel> videoModelList = new ArrayList<>();
private VideoAdapter videoAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
checkvariables();
findViewById(R.id.goback).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
checkPermissions();
}
private void checkvariables() {
RecyclerView recyclerView = findViewById(R.id.justarecyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 3));
videoAdapter = new VideoAdapter(getApplicationContext(), videoModelList);
recyclerView.setAdapter(videoAdapter);
}
private void checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 123);
} else {
loadVideos();
}
} else {
loadVideos();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 123) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadVideos();
} else {
Toast.makeText(this, "Permission Denied:(", Toast.LENGTH_SHORT).show();
finish();
}
}
}
private void loadVideos() {
new Thread(){
@Override
public void run() {
super.run();
String[] projection = new String[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DURATION};
}
String sortOrder = MediaStore.Video.Media.DATE_ADDED + " DESC";
Cursor cursor = getApplication().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null , sortOrder);
if (cursor != null){
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
int duration = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
while (cursor.moveToNext()){
long id = cursor.getLong(idColumn);
int vid_length = cursor.getInt(duration);
Uri data = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
String duration_format;
int sec = (vid_length / 1000) % 60;
int min = (vid_length / (1000 * 60)) % 60;
int hrs = vid_length / (1000 * 60 * 60);
if (hrs == 0){
duration_format = String.valueOf(min).concat(":".concat(String.format(Locale.UK, "%02d", sec)));
}else {
duration_format = String.valueOf(hrs).concat(":".concat(String.format(Locale.UK, "%02d", min)
.concat(":".concat(String.format(Locale.UK, "%02d", sec)))));
}
videoModelList.add(new VideoModel(id, data, duration_format));
runOnUiThread(new Runnable() {
@Override
public void run() {
videoAdapter.notifyItemInserted(videoModelList.size() -1);
}
});
}
}
}
}.start();
}
}
These are the logcat errors
2020-11-19 13:39:12.585 15872-15887/com.example.oqpyd E/System: Uncaught exception thrown by finalizer
2020-11-19 13:39:12.587 15872-15887/com.example.oqpyd E/System: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.android.org.conscrypt.SslWrapper.isClosed()' on a null object reference
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.free(ConscryptFileDescriptorSocket.java:994)
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.finalize(ConscryptFileDescriptorSocket.java:1022)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:254)
at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:241)
at java.lang.Daemons$Daemon.run(Daemons.java:107)
at java.lang.Thread.run(Thread.java:764)
2020-11-19 13:39:12.587 15872-15887/com.example.oqpyd E/System: Uncaught exception thrown by finalizer
2020-11-19 13:39:12.588 15872-15887/com.example.oqpyd E/System: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.android.org.conscrypt.SslWrapper.isClosed()' on a null object reference
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.free(ConscryptFileDescriptorSocket.java:994)
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.finalize(ConscryptFileDescriptorSocket.java:1022)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:254)
at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:241)
at java.lang.Daemons$Daemon.run(Daemons.java:107)
at java.lang.Thread.run(Thread.java:764)
2020-11-19 13:39:12.589 15872-15887/com.example.oqpyd E/System: Uncaught exception thrown by finalizer
2020-11-19 13:39:12.590 15872-15887/com.example.oqpyd E/System: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.android.org.conscrypt.SslWrapper.isClosed()' on a null object reference
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.free(ConscryptFileDescriptorSocket.java:994)
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.finalize(ConscryptFileDescriptorSocket.java:1022)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:254)
at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:241)
at java.lang.Daemons$Daemon.run(Daemons.java:107)
at java.lang.Thread.run(Thread.java:764)
2020-11-19 13:39:12.590 15872-15887/com.example.oqpyd E/System: Uncaught exception thrown by finalizer
2020-11-19 13:39:12.591 15872-15887/com.example.oqpyd E/System: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.android.org.conscrypt.SslWrapper.isClosed()' on a null object reference
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.free(ConscryptFileDescriptorSocket.java:994)
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.finalize(ConscryptFileDescriptorSocket.java:1022)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:254)
at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:241)
at java.lang.Daemons$Daemon.run(Daemons.java:107)
at java.lang.Thread.run(Thread.java:764)
2020-11-19 13:39:12.704 15872-15877/com.example.oqpyd I/zygote64: Do partial code cache collection, code=125KB, data=82KB
2020-11-19 13:39:12.705 15872-15877/com.example.oqpyd I/zygote64: After code cache collection, code=125KB, data=82KB
2020-11-19 13:39:12.705 15872-15877/com.example.oqpyd I/zygote64: Increasing code cache capacity to 512KB
2020-11-19 13:39:12.963 15872-15872/com.example.oqpyd D/AndroidRuntime: Shutting down VM
2020-11-19 13:39:12.966 15872-15872/com.example.oqpyd E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.oqpyd, PID: 15872
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{f7adb0f position=23 id=-1, oldPos=11, pLpos:11 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{3a1e42c VFED..... .F....ID 0,92-720,1464 #7f0901a4 app:id/justarecyclerview}, adapter:adapter.VideoAdapter@57bbef5, layout:androidx.recyclerview.widget.GridLayoutManager@5d248a, context:com.example.oqpyd.PostActivity@c4c3f1d
at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5974)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6158)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:561)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4085)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3849)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:981)
at android.view.Choreographer.doCallbacks(Choreographer.java:793)
at android.view.Choreographer.doFrame(Choreographer.java:720)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:967)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6892)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
2020-11-19 13:39:12.990 15872-15872/com.example.oqpyd I/Process: Sending signal. PID: 15872 SIG: 9
`