Call a method that throws Exception / try catch block - java

so I have method that looks like this :
public void testSingleGames() throws InterruptedException {
try {
Log.d("TEST","JSON");
} catch (JSONException e) {
e.printStackTrace();
fail("JSONException!!");
}
I simply want to call that method elsewhere :
testSingleGames();
though it says : Unhandled Exception.java.lang.InterruptedException.
I tried the suggested fix android studio gave me , which is :
try {
testSingleGames();
} catch (InterruptedException e) {
e.printStackTrace();
}
my app crashes when I do though.
So how do I call this mehod properly ?
EDIT :
here some
STACKTRACES :
I think this one is the "main cause" since text was marked red:
Process: com.test.test, PID: 4097
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.MainActivity}: java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
at android.support.test.InstrumentationRegistry.getContext(InstrumentationRegistry.java:75)
at com.test.test.MainActivity.setUp(MainActivity.java:61)
at com.test.test.MainActivity.testSingleGames(MainActivity.java:69)
at com.test.test.MainActivity.onCreate(MainActivity.java:122)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Edit 2 : Ok I googled this line : No instrumentation registered! Must run under a registering instrumentation.
Previously I had a problem dependencies:
Conflict with dependency 'com.android.support.test:runner' in project ':app'. Resolved versions for app (0.5) and test app (1.0.1) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.
I "fixed" it by changeing :
androidTestImplementation 'com.android.support.test:runner:1.0.1'
to
Implementation 'com.android.support.test:runner:1.0.1'
so the error went away .
Maybe this is the root cause ?
I hope someone can make sense of this o.O , since I just tried to dabble abit with some api which is throwing all kinds of problems at me -.- ...

Related

How to get path to current class file in Kotlin in Android app without getProtectionDomain

I need to get 2 things logged from the current line of code:
the current class which is currently executed
the full path to the file of this class
In Kotlin, I can get class by
this::class
or
this::class.java
Then, I should be able to get path to the file of the class via
class.getProtectionDomain().getCodeSource().getLocation().getPath()
As is advised e. g. here.
The problem is getProtectionDomain returns null for Android Activity:
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:7288)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7258)
at android.view.View.access$4000(View.java:808)
at android.view.View$PerformClick.run(View.java:28019)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7615)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7288) 
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
at android.view.View.performClickInternal(View.java:7258) 
at android.view.View.access$4000(View.java:808) 
at android.view.View$PerformClick.run(View.java:28019) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7615) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 
Caused by: java.lang.NullPointerException: this::class.java.protectionDomain must not be null
Meanwhile the class is of course not null because I can successfully see its name
log(this::class.java.name)
So how can i get class file path without getProtectionDomain?

"java.lang.IllegalStateException: Cannot perform this action on a sealed instance." FATAL EXCEPTION: main

I am getting this error on pre-launch report from playstore console.
I am not able to generate this crash on any real device I try.
The device from pre-launch report is Samsung s9 (Android 8.0).
This is the stacktrace :
FATAL EXCEPTION: main
java.lang.IllegalStateException: Cannot perform this action on a sealed instance.
at android.view.accessibility.AccessibilityNodeInfo.enforceNotSealed(AccessibilityNodeInfo.java:2992)
at android.view.accessibility.AccessibilityNodeInfo.setSource(AccessibilityNodeInfo.java:818)
at android.view.accessibility.AccessibilityNodeInfo.setSource(AccessibilityNodeInfo.java:795)
at android.view.accessibility.AccessibilityNodeInfo.obtain(AccessibilityNodeInfo.java:3008)
at android.view.View.createAccessibilityNodeInfoInternal(View.java:7897)
at android.view.View.createAccessibilityNodeInfo(View.java:7883)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1051)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchAccessibilityNodeInfos(AccessibilityInteractionController.java:876)
at android.view.AccessibilityInteractionController.findAccessibilityNodeInfoByAccessibilityIdUiThread(AccessibilityInteractionController.java:199)
at android.view.AccessibilityInteractionController.-wrap1(Unknown Source:0)
at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:1225)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Without seeing more code, I'd recommend looking through the following links to try and determine an exact cause.
https://stackoverflow.com/a/57964014/5779200
When updating values for AccessibilityNodeInfo it has been seen that calling .getClassName() to see if the value is NULL can help prevent this error.
https://stackoverflow.com/a/32829963/5779200
Similar to the previous, If you are changing the text of an accessibilityNodeInfo you can check to see if it's sealed by calling isSealed(). on the AccessibilityNodeInfo object

Can we use Truecaller SDK if truecaller isnt installed in device

If I try to use the following code, I am able to use Truecaller's SDK and fetch the user's profile if he has truecaller installed.
//has_truecaller = TrueSDK.getInstance().isUsable(); //doesnt work before init() methof
TrueSdkScope trueScope;
TrueSdkScope.Builder sdkBuilder = new TrueSdkScope.Builder(this, sdkCallback)
.consentMode(TrueSdkScope.CONSENT_MODE_POPUP )
.consentTitleOption( TrueSdkScope.SDK_CONSENT_TITLE_VERIFY )
.footerType( TrueSdkScope.FOOTER_TYPE_SKIP );
if(has_truecaller) {
trueScope = sdkBuilder.sdkOptions( TrueSdkScope.SDK_OPTION_WITH_OTP ).build();
}else{
//TODO get mobile number
BaseActivity.USER_MOBILE_NUMBER = "12345677";
trueScope = sdkBuilder.sdkOptions( TrueSdkScope.SDK_OPTION_WITH_OTP ).build();
}
TrueSDK.init(trueScope);
However If i remove Truecaller from the device i get the following error:
2-06 10:38:45.437 25694-25694/com.project.xyz.userclientapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.xyz.userclientapp, PID: 25694 java.lang.NoClassDefFoundError:
Failed resolution of: Lretrofit2/converter/gson/GsonConverterFactory;
at com.truecaller.android.sdk.b.c.a(Unknown Source:9)
at com.truecaller.android.sdk.clients.a.<init>(Unknown Source:10)
at com.truecaller.android.sdk.a.<init>(Unknown Source:46)
at com.truecaller.android.sdk.a.a(Unknown Source:2)
at com.truecaller.android.sdk.TrueSDK.init(Unknown Source:5)
at com.project.xyz.userclientapp.login.LoginActivity.setupTrueSdk(LoginActivity.java:49)
at com.project.xyz.userclientapp.login.LoginActivity.onCreate(LoginActivity.java:30)
at android.app.Activity.performCreate(Activity.java:7149)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)
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:1906)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
You can check if the Truecaller app is present on the device or not by using the following line -
TrueSDK.getInstance().isUsable()
You do need to initialize the SDK using the init() method before checking for this. It will return a boolean true / false depending on wether Truecaller is installed or not.
Also, the logs what you shared are because of missing a library dependency in your android project. Can you try add the following dependency in your gradle. This should help solve the error -
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

Why does app crash on start only on api 26

I have an app working on every api completely but in android 8 api 26 it crashes and I don't know why this happend .
The app use sms sending and receiving permissions and I put all requestPermissions in activity and manifests. Target sdk is on 26
Here the logcat of android studio error when app crash:
hapital.com.BitCar E/AndroidRuntime: FATAL EXCEPTION: main Process: hapital.com.BitCar, PID: 25271
java.lang.RuntimeException: Unable to start activity ComponentInfo{hapital.com.BitCar/hapital.com.BitCar.MainActivity}: java.lang.IllegalArgumentException: Invalid destinationAddress
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalArgumentException: Invalid destinationAddress
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:320)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:312)
at hapital.com.BitCar.MainActivity.onCreate(MainActivity.java:194)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Invalid destinationAddress ?? what does it mean??
For example it has an error on SmsManager.java when open it just like pic below has error in import libraries
enter image description here
Could someone help what should i do?
According to the source code of SmsManager, that exception is thrown when you call sendTextMessage with a destination address parameter that is null or an empty String.
If your code to send the SMS (as per your comment) is this:
SharedPreferences panelnumber = getSharedPreferences("Prefs", MODE_PRIVATE);
String string_from_sp = panelnumber.getString("myStringName", "");
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(string_from_sp.toString(), null,
smsBody.toString(), null, null);
then it follows that string_from_sp is a null or empty String.
Given the way you are initializing that variable, either:
the "mystringName" field retrieved from the preferences is empty, or
you have the preference field name incorrect, and you are getting the default value which you have specified to be an empty String.
There is not sufficient info to explain why this happens with API 26 only ... if that is what you are saying.
finally i found the problem
problem was in layout of activity that i used NavigationView like belo :
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="230dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main2"
app:menu="#menu/activity_main2_drawer" />
and in api 26 and higher not support

IncompatibleClassChangeError: Class 'java.lang.VirtualMachineError' does not implement interface 'java.lang.CharSequence'

I am having weird problems with my code and I cannot explain anything that's happening. It's crashing on Marshmallow but not on Nougat which makes this more bizarre.
I can point to a line where the error occurs but the error itself is not clear. I am not sure what is crashing and how to prevent it. Here are the details:
The crash happens when I try to make a call to the server. To do this, I use Retrofit alongside RxJava and I do this in Kotlin. Here is my code:
fun login(username: String, password: String, callback: Login.OnLoginListener) {
RestClient.getInstance().service
.loginUser( //<- This line is crashing for no apparent reason
Utils.RequestBodyParser.parseString(username),
Utils.RequestBodyParser.parseString(password)
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ success ->
(
if (success.status)
callback.onLoginSuccess()
else
callback.onLoginError()
)
},
{ error ->
GenericErrorHandler.handleError(error, callback.retrieveContext())
}
)
}
Here is the logcat output:
java.lang.IncompatibleClassChangeError: Class 'java.lang.VirtualMachineError' does not implement interface 'java.lang.CharSequence' in call to 'java.lang.String java.lang.CharSequence.toString()' (declaration of 'java.lang.Throwable' appears in /system/framework/core-libart.jar)
at fi.hohtolabs.kuuratablet.network.WebController$Companion.login(WebController.kt:19)
at fi.hohtolabs.kuuratablet.presenter.LoginPresenter.login(LoginPresenter.kt:31)
at fi.hohtolabs.kuuratablet.dialog.LoginDialog.lambda$onResume$2$LoginDialog(LoginDialog.java:74)
at fi.hohtolabs.kuuratablet.dialog.LoginDialog$$Lambda$2.onClick(Unknown Source)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Does anybody have any suggestions on what I should do? (I tried cleaning, rebuilding, restarting and invalidating caches, deleted the .gradle folder and all other stuff like that, nothing helps)
The problem had occurred only on Marshmellow devices and later was different one. Later i got the ArrayIndexOutOfBoundsException and finally found out what the problem was.
The problem was that my libraries were over 64K methods and because Instant Run was on, there was no multidex error and whole application did not compile well.

Categories

Resources