This is my first question please bear with me.
I am trying to drawing over other app
my code is working fine on Mobile but when i am trying to run this app on emulator my app get crash when i click on button.
actually i am intenting from Main activty with itemclick listner
package com.shahabtech.ytbooster;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Popup2 extends AppCompatActivity {
private WindowManager.LayoutParams mParams;
private LayoutInflater layoutInflater;
private View mView;
private FirebaseAuth mAuth;
private Context context;
private WindowManager mWindowManager;
int UserCoins2 = 0;
ArrayList<model>List;
SelectListner selectListner;
int Reward;
int Views;
String YT_ID;
String UID;
DatabaseReference databaseReference;
FirebaseDatabase firebaseDatabase;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup2);
mAuth = FirebaseAuth.getInstance(FirebaseApp.initializeApp(this));
UID = mAuth.getInstance().getCurrentUser().getUid();
getCoins();
String yT_LINK = getIntent().getExtras().getString("LINK");
Reward = getIntent().getIntExtra("WATCHTIME",0);
Views = getIntent().getIntExtra("VIEWS",0);
YT_ID = getYouTubeId(yT_LINK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// set the layout parameters of the window
mParams = new WindowManager.LayoutParams(
// Shrink the window to wrap the content rather
// than filling the screen
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,
// Display it on top of other application windows
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
// Don't let it grab the input focus
// WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON,
PixelFormat.TRANSLUCENT);
}
// getting a LayoutInflater
layoutInflater = (LayoutInflater) Popup2.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflating the view with the custom layout we created
mView = layoutInflater.inflate(R.layout.activity_popup2, null);
getCoins();
MycountDown mycountDown = new MycountDown(Reward*1000,1000);
mycountDown.start();
TextView TvRecord =mView.findViewById(R.id.reward_2);
TextView TV_Views = mView.findViewById(R.id.tv_announsment);
// TextView TV_PlayTime = mView.findViewById(R.id.play_time2);
TV_Views.setText(String.valueOf(Views));
TvRecord.setText(String.valueOf(Reward));
// TV_PlayTime.setText(String.valueOf(Reward)+" Seconds");
// Tv_Coins.setText(String.valueOf(UserCoins2));
mView.findViewById(R.id.stop_watch2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Popup2.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
mycountDown.cancel();
close2(Popup2.this);
}
});
mParams.gravity = Gravity.BOTTOM;
mWindowManager = (WindowManager)Popup2.this.getSystemService(WINDOW_SERVICE);
Open(Popup2.this);
// Toast.makeText(Popup2.this, "Link is "+UserCoins2, Toast.LENGTH_SHORT).show();
openYoutube();
}
public void Open(Context context) {
try {
getCoins();
if(mView.getWindowToken()==null) {
if(mView.getParent()==null) {
mWindowManager.addView(mView, mParams);
// Toast.makeText(context, "Opend", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Log.d("Error1",e.toString());
}
}
public void close2(Context context) {
try {
((WindowManager)context.getSystemService(WINDOW_SERVICE)).removeView(mView);
mView.invalidate();
((ViewGroup)mView.getParent()).removeAllViews();
} catch (Exception e) {
Log.d("Error2",e.toString());
}
}
private void openYoutube(){
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+YT_ID));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v="+YT_ID));
try {
Popup2.this.startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
// Popup2.this.startActivity(webIntent);
Toast.makeText(context, "Install YouTube First "+ex, Toast.LENGTH_SHORT).show();
}
}
private void openYoutube2(){
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v="));
try {
Popup2.this.startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
// Popup2.this.startActivity(webIntent);
Toast.makeText(context, "Install YouTube First "+ex, Toast.LENGTH_SHORT).show();
}
}
private void getCoins(){
String UID = mAuth.getCurrentUser().getUid();
DatabaseReference reference;
reference = FirebaseDatabase.getInstance().getReference("USERS_DATA");
reference.child(UID).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()){
if (task.getResult().exists()){
// Toast.makeText(Popup2.this, "Successfully found", Toast.LENGTH_SHORT).show();
DataSnapshot dataSnapshot = task.getResult();
UserCoins2 = Integer.parseInt(String.valueOf(dataSnapshot.child("coins").getValue()));
TextView TV_Coins = mView.findViewById(R.id.tv_coins2);
TV_Coins.setText(String.valueOf(UserCoins2));
// Toast.makeText(Popup2.this, "User Coins are "+UserCoins2, Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(Popup2.this, "Empty", Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(Popup2.this, "Data Not Found", Toast.LENGTH_SHORT).show();
}
}
});
}
public String getYouTubeId(String youTubeUrl) {
String pattern = "(?<=youtu.be/|watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(youTubeUrl);
if (matcher.find()) {
return matcher.group();
} else {
return "error";
}
}
public class MycountDown extends CountDownTimer{
public MycountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long l) {
TextView TV_PlayTime = mView.findViewById(R.id.play_time2);
TV_PlayTime.setText(String.valueOf(l/1000+" Seconds"));
}
@Override
public void onFinish() {
update_coins();
update_views();
close2(Popup2.this);
Intent intent = new Intent(Popup2.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
private void update_coins() {
UserCoins2 = UserCoins2 + Reward;
HashMap Update_coins = new HashMap();
Update_coins.put("coins", UserCoins2);
databaseReference = firebaseDatabase.getInstance().getReference("USERS_DATA");
databaseReference.child(UID).updateChildren(Update_coins).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Toast.makeText(Popup2.this, "Reward Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Popup2.this, "Coins Not Updated", Toast.LENGTH_SHORT).show();
}
}
});
}
private void update_views() {
Views = Views + 1;
HashMap Update_Views = new HashMap();
Update_Views.put("V_COMPLETED", Views);
databaseReference = firebaseDatabase.getInstance().getReference("LINKS").child("LINK");
databaseReference.child(YT_ID).updateChildren(Update_Views).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
// Toast.makeText(Popup2.this, "Views Updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Popup2.this, "Views Not Updated", Toast.LENGTH_SHORT).show();
}
}
});
}
}
// My XML is as follow
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:foregroundGravity="bottom"
android:gravity="bottom"
android:padding="0dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/material_dynamic_neutral80"
android:foregroundGravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="69dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coins"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_coins2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="0dp"
android:layout_height="69dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout293"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout">
<TextView
android:id="@+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Time"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/play_time2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout293"
android:layout_width="404dp"
android:layout_height="69dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout2">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout23"
android:layout_width="0dp"
android:layout_height="69dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView62"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reward"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.32" />
<TextView
android:id="@+id/reward_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout293">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Announcment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.545" />
<TextView
android:id="@+id/tv_announsment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coming Soon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout4"
android:layout_width="0dp"
android:layout_height="72dp"
android:layout_marginStart="20dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout3">
<Button
android:id="@+id/window_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Skip Video"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/stop_watch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Watching"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
//Here is my logcat
Sir here is my logcat when i trying to run my android 11 app running smoothly but on lower version of android and also on emulator app got crash when i click on recyclerview item view. i try to changed my layout and comment out all code of pop2 activity then not crashing. actually in my pop2 activty i am inflating a view and also setting content view
2022-09-11 11:59:55.342 3279-3327/com.shahabtech.ytbooster D/EGL_adreno: eglGetConfigAttrib: bad attrib 0x3339
2022-09-11 11:59:55.342 3279-3327/com.shahabtech.ytbooster E/EGL_adreno: tid 3327: eglGetConfigAttrib(1207): error 0x3004 (EGL_BAD_ATTRIBUTE)
2022-09-11 11:59:55.377 3279-3327/com.shahabtech.ytbooster E/EGL_adreno: [getAttribValue] Bad attribute idx
2022-09-11 11:59:55.377 3279-3327/com.shahabtech.ytbooster D/EGL_adreno: eglGetConfigAttrib: bad attrib 0x3339
2022-09-11 11:59:55.377 3279-3327/com.shahabtech.ytbooster E/EGL_adreno: tid 3327: eglGetConfigAttrib(1207): error 0x3004 (EGL_BAD_ATTRIBUTE)
2022-09-11 12:00:23.134 3279-3279/com.shahabtech.ytbooster W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@b8b0d00
2022-09-11 12:00:23.164 3279-3279/com.shahabtech.ytbooster D/AndroidRuntime: Shutting down VM
2022-09-11 12:00:23.166 3279-3279/com.shahabtech.ytbooster E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shahabtech.ytbooster, PID: 3279
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shahabtech.ytbooster/com.shahabtech.ytbooster.Popup2}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:710)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.shahabtech.ytbooster.Popup2.onCreate(Popup2.java:59)
at android.app.Activity.performCreate(Activity.java:7197)
at android.app.Activity.performCreate(Activity.java:7188)