I'm trying to add Layout with this code:
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE);
mYouTubePlayerLayout = (ConstraintLayoutKeyDispatchWrapper) layoutInflater.inflate(R.layout.background_player_layout, null, false);
mYouTubePlayerLayout.setOnDispatchKeyEventListener(this);
mWindowManager = (WindowManager) activity.getSystemService(WINDOW_SERVICE);
createWindowParams(true);
mWindowManager.addView(mYouTubePlayerLayout, mWindowParams);
I understand that I need to get permissions from the user for 23 version and above, so I'm asking the user for the permission with this code:
if (Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
isAppPlayStorePermission = true;
AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
dialog.setCancelable(false);
dialog.setIcon(R.drawable.ic_launcher);
dialog.setTitle(R.string.dialog_need_playstore_permissions_title);
dialog.setMessage(R.string.dialog_need_playstore_permissions_description);
dialog.setPositiveButton(R.string.button_exit, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
AndroidUtils.exitApp(activity);
}
});
dialog.setNegativeButton(R.string.button_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0);
}
});
dialog.show();
}
but now I'm getting a crash from Fabric:
Fatal Exception: java.lang.RuntimeException
Unable to create service ***: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W#c7450fc -- permission denied for this window type
The crash is only for users with Android OS of 7 and below.
Any idea what is the problem?
Related
I have uploaded a signed app on the play store. and today when I opened my firebase pannel in crashlytics it's showing 2 crashes in the last 24 hours. Both the crashes were on the same java class and line and also both on the Samsung phone only.
Here is the error: Fatal Exception: java.lang.RuntimeException
Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4200010....
It's in my receiver class: below is the code for the same
public void onReceive(final Context context, Intent intent) {
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
if (alertdialog != null) {
alertdialog.dismiss();
}
} else {
builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(R.layout.internet_dialouge, null);
builder.setView(view);
builder.create();
builder.setCancelable(true);
alertdialog = builder.show();
view.findViewById(R.id.btnDismiss)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertdialog.dismiss();
}
});
view.findViewById(R.id.btnSetting)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
context.startActivity(new Intent(Settings.ACTION_SETTINGS));
}
});}}}}
Crashlytics showing error in this line: alertdialog = builder.show();
And I use this receiver class in the main activity like this:
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
myReceiver = new MyReceiver();
registerReceiver(myReceiver, intentFilter);
I am not understanding why this error in crashlytics.
"context" may be not initialized when your application reaches the line
builder = new AlertDialog.Builder(context);
Please check context is always initialized. You can check like this.
if(context!=null) {
//build alertdialog. }
I am creating a code that checks for the current location, however, when I check for the current location, it crashes. I looked into Stack Trace and saw this. I read other forums but I am not familiar with their solutions. I am also a beginner in Java. I used an Amazon Fire Tablet to download this app. When I tried testing it on the emulator, the app did not crash.
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.LOCALE_SETTINGS }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1797)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
at android.app.Activity.startActivityForResult(Activity.java:3761)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:3722)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:4032)
at android.app.Activity.startActivity(Activity.java:4000)
at com.example.calculatorandlocationfinderfinal.MainActivity$7.onClick(MainActivity.java:181)
at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Below is the code where it happened.
private void onGPS() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_LOCALE_SETTINGS));
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Always use this method to start intents other than your app's -
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
if(intent.resolveActivity(context.getPackageManager()) != null){
startActivity(intent);
}else{
//handle activity not found
}
this way you won't get ActivityNotFoundException.
You can also use try catch as -
try {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
} catch (e: ActivityNotFoundException) {
//handle activity not found
}
I suppose you want to navigate to GPS settings screen.
try this.
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
The phone/device might be missing locale settings screen altogether which leads to the crash.
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //This line solve my issue
startActivity(viewIntent);
I'm trying to make a dialogue box with a positive bottom that changes the activity from SignupAcitvity to MainActivity. So I used the intent method to do this. However, once I tried this, I got the following error message:Cannot Resolve constructor 'Intent(com.androidcodefinder.loginscreendemo.Profile.ExampleDialogue, java.lang.Class<com.androidcodefinder.loginscreendemo.MainActivity>)'. Can you help me fix this?
My code:
public class ExampleDialogue extends AppCompatDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialayout,null);
builder.setView(view)
.setTitle("Confirm Your Email")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(SignUpActivity.class, MainActivity.class);
startActivity(intent);
}
});
return builder.create();
}
}
The reason that you are getting this error is because you are in the ExampleDialogue activity, but you are starting an intent from SignUpActivity.class to MainActivity.class. If you want to go to MainActivity.class, you will have to do:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Also, it can't resolve the constructor so just add this default constructor, which you can change as you wish:
public ExampleDialogue(){
//Constructor code.
}
You are trying
Intent intent = new Intent(SignUpActivity.class, MainActivity.class)
However, Intent has no constructor with 2 classes as arguments. Hence the error.
What you want, is to use the constructor Intent(Context, Class<?>). Specifially, in your case you do this:
Intent intent = new Intent(getActivity(), MainActivity.class);
Google Developer Console indicates me that my app got java.lang.ClassCastException error on 2 different devices (P10 (HWVTR)-Android 7.0 and LG G3 (g3) - Android 6.0) at the same lane. I don't know what's the problem, I have tested it on Samsung Galaxy A3 2014 & 2017 and Huawei P9 Lite and works great.
Here is the code:
SharedPreferences prefs = this.getSharedPreferences(
"toast", Context.MODE_PRIVATE);
final int hasVisited = prefs.getInt("HAS_VISISTED_BEFORE", 0);
if(hasVisited == 0) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(this);
}
builder.setTitle("My Text")
.setMessage("My Text, dsajh jghjsahjhsda dsa dsa")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_info)
.show();
prefs.edit().putInt("HAS_VISISTED_BEFORE", 1).apply();
}
The error is here final int hasVisited = prefs.getInt("HAS_VISISTED_BEFORE", 0);
I want to display this AlertDialog only once when the app if run for the first time.
Error log:
Caused by: java.lang.ClassCastException:
at android.app.SharedPreferencesImpl.getInt (SharedPreferencesImpl.java:242)
at com.doggedness_dev.pubgcratessimulator.MenuActivity.onCreate (MenuActivity.java:40)
at android.app.Activity.performCreate (Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2387)
It is possible that my SharedPreferences with the key HAS_VISISTED_BEFORE to interfere with another app that has the same key?
Every application has their own shared preferences location, so there is no way to interfere with another app that has the same key.
If your application's older versions have the same key with HAS_VISISTED_BEFORE and its value is not integer, you will have this problem. Because you have already have the value for this key and types are just different.
You can use try-catch like below, for this exception.
try {
}catch (ClassCastException ex){
ex.printStackTrace();
}
Remove this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(this);
}
and write below line;
builder = new AlertDialog.Builder(MainActivity.this);
I have a alert dialog in android that called from javascript interface.
In alert dialog I have two button that when you click on the one of them I want start other alert dialog that include list.but when I click app crashed.
this is my java code :
public void SaveDialog(final String SaveID) {
final SQLiteDatabase mydatabase1 = openOrCreateDatabase("CopyCollection", MODE_PRIVATE, null);
Cursor c = mydatabase1.rawQuery("SELECT * FROM Details WHERE ID="+SaveID+";", null);
if(c.moveToFirst()) {
do {
appName = c.getString(1);
txtClip = c.getString(2);
text_Date = c.getString(3);
} while (c.moveToNext());
}
AlertDialog.Builder myDialog = new AlertDialog.Builder(AndroidHTMLActivity.this);
myDialog.setTitle("ذخیره");
myDialog.setPositiveButton("ذخیره", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//======================================================
String names[] ={"A","B","C","D"};
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(AndroidHTMLActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.main, null);
alertDialog.setView(convertView);
alertDialog.setTitle("List");
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidHTMLActivity.this,android.R.layout.simple_list_item_1,names);
lv.setAdapter(adapter);
alertDialog.show();
}
});
myDialog.setNegativeButton("اشتراک گذاری", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = txtClip;
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
myDialog.show();
}
and this is logcat :
03-24 02:22:58.146 5499-5499/com.exercise.AndroidHTML W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0cf3b20)03-24 02:22:58.156 5499-5499/com.exercise.AndroidHTML E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.exercise.AndroidHTML, PID: 5499
java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
at com.android.webview.chromium.WebViewChromium.createThreadException(WebViewChromium.java:268)
at com.android.webview.chromium.WebViewChromium.checkThread(WebViewChromium.java:284)
at com.android.webview.chromium.WebViewChromium.setLayoutParams(WebViewChromium.java:1618)
at android.webkit.WebView.setLayoutParams(WebView.java:2099)
at android.view.ViewGroup.addViewInner(ViewGroup.java:3577)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.view.ViewGroup.addView(ViewGroup.java:3391)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.exercise.AndroidHTML.AndroidHTMLActivity$MyJavaScriptInterface.SaveDialog(AndroidHTMLActivity.java:198)
at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
JavaScript runs on a separate thread other than main thread, which you can't show dialog directly on. Instead, you can show your dialog as following:
First, add a method to your MyJavaScriptInterface class:
public void showDialog(final String SaveID){
// create a Handler with the main Looper and
// post the dialog showing work to main thread.
new Handler(Looper.getMainLooper()).post(new Runnable(){
SaveDialog(SaveID);
});
}
Then, call showDialog() method instead of SaveDialog().