Occasional "Fragment$InstantiationException: Trying to instantiate a class androidx.lifecycle.y that is not a Fragment" - android

I want to emphasize first that this exception is extremely rare. I am asking out of curiosity more than anything else. It occurs no more than 0.01% of the time. Since the app has a large installation base, we see it in analytics.
public class AppCompatFoo extends AppCompatActivity {
...
}
public class AppFooActivity
extends AppCompatFoo {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
}
}
super.onCreate(savedInstanceState) occasionally throws:
android.app.Fragment$InstantiationException
Stack trace: android.app.Fragment$InstantiationException: Trying to instantiate a class androidx.lifecycle.y that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:538)
at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
at android.app.FragmentState.instantiate(FragmentState.java:77)
at android.app.FragmentManagerImpl.restoreAllState(FragmentManagerImpl.java:2867)
at android.app.FragmentController.restoreAllState(FragmentController.java:142)
at android.app.Activity.onCreate(Activity.java:1501)
at androidx.core.app.ComponentActivity.onCreate(ComponentActivity.java:88)
at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:363)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
Wonder if anyone could offer a hint about the possible causes. Again, it is extremely rare. It is really not a problem.

Related

Nothing shows up while calling a method

I was developing an exam score calculator app.
When I want to call AD methods,advertisements don't show up.
Calculation process happens in OnCreate method:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
/*Calculation...*/}
and other voids like:
public void requestAd() {
/*AD RQUESTING PROCESS...*/
}
and
public void showAd() {
/*AD SHOWING PROCESS...*/
}
AD team gave me this code to call the method and it works well:
requestButton.setOnClickListener(v -> requestAd());
showButton.setOnClickListener(v -> showAd());
But the Problem is I don't have buttons to call them so I tried this:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
requestAd();
showAd();
/*Calculation...*/}
But when the activity starts ads don't show up!
The whole question is I want this methods to be called while this activity starts.
thank you.
Try building up the release version APK and test on it. Maybe your Ad-provider have some restrictions in debug version?
I made another class and moved request Ad and showAd there. Then, I made an object and called the method through object.
I have to mention that I changed a minor thing in requestAd but the main job was done by the object.
Thank You All.

How to prevent the Screentshot in the entire android app without repeating the same code

Hi everyone i want to block the Screenshot in my app. I got the first problem solve from here.
But now the thing is I have more than 10 activity and 10 + fragment.
Is there any way to do this just by writing in the one class and giving it reference to the entire app.
Just like we make one Application class and in the AndroidMainfest.xml give that application class refrence.
You can implement a BaseActivity, and make all your activities extend this BaseActivity. In onCreate() of this activity set the flag. You need to ensure all your activities call super.onCreate() as follows:
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your flag here
...
}
}
Activity1.java
public class Activity1 extends BaseActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
}

Compilation Error: onActivityResult(int,int,Intent) in 'package.fool' clashes with onActivityResult in 'android.support.v4.app.FragmentActivity'

I have a simple Activity that i need to implement the method 'onActivityResult' but when I do that it's returned this message about the method i've implemented.
onActivityResult(int,int,Intent) in 'package.fool' clashes with 'onActivityResult(int,int,Intent)' in 'android.support.v4.app.FragmentActivity';
My Activity:
public class Agenda extends AppCompatActivity implements RecycleViewAdapter.Listener, FragmentDrawer.FragmentDrawerListener, PreferenceManager.OnActivityResultListener {
protected void onCreate(Bundle savedInstanceState) {
Log.d("OnCreate", "----------------------------------------");
super.onCreate(savedInstanceState);
}
}
I think that there are the same override method for more than two classes, according to this information:
Overrided Methods
I would be thankfull if someone help me =D.
Problem Solved!
Actually there was no need to implement:
PreferenceManager.OnActivityResultListener
Because this method is already implicity in the Activity and the fact that i implemented again the same method was causing the error.
Anyway, thanks for the comunity help.

How to log crash reports in Production build from 1 place

I found this answer, It helped me a lot in understanding the scenario. But, what if i have 100 Activities in my application, and crash can happen in any of them, Is there a possibility that I log all crashes from 1 single place, instead of writing this code in every single activity.
I want to save the stacktrace in some file, whenever a crash happen in application, and i want to do this globally, not by going into each activity.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Sets the default uncaught exception handler. This handler is invoked
// in case any Thread dies due to an unhandled exception.
Thread.setDefaultUncaughtExceptionHandler(new CustomizedExceptionHandler(
"/mnt/sdcard/"))
;
You can do that in your Application class.
public class MyDemoApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
if (!BuildConfig.DEBUG) {
Thread.setDefaultUncaughtExceptionHandler( <YOUR_CUSTOM_EXCEPTION_HANDLER> );
}
}
}

getApplicationContext returning null in TabActivity

public class MyTabActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataManager = DataManager.getInstance(getApplicationContext());
}
}
I have a tab activity as shown above. It works fine on the initial run. After some time in the background where it's activity is deleted from memory, when you reopen the app, it will crash. The reason is because getApplicationContext() returns null.
I use this same setup in other Activities with no problems. I can't find anywhere in the documentation that says when/why/if it would return null.
You can simply pass "this" to your DataManager.getInstance because your MyTabActivity inherits from Context (three levels up)

Categories

Resources