I'm using BiometricPrompt on my app but this code throws NullPointerException on some devices (for instance Redmi 5 Plus)
if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
biometricPrompt.authenticate(getPromptInfo(), cryptoObject)
}
private fun getPromptInfo(): BiometricPrompt.PromptInfo {
return BiometricPrompt.PromptInfo.Builder()
.setTitle("My App's Authentication")
.setNegativeButtonText("Cancel")
.setSubtitle("Please login to get access")
.setDescription("My App is using Android biometric authentication")
.build()
}
Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setVisibility(int)' on a null object reference
How can I fix this?
UPDATE (Added logs) :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
at androidx.biometric.FingerprintDialogFragment.onCreateDialog(FingerprintDialogFragment.java:199)
at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:419)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1484)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1335)
at android.app.Activity.performStart(Activity.java:7108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
Related
I created method in other class than MainActivity (not in onCreate) which includes val textView = TextView(MainActivity()). It compiles without errors but I get this exception on runtime exactly on this line. Full error list:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
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:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:91)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:607)
at android.view.View.<init>(View.java:4796)
at android.view.View.<init>(View.java:4948)
at android.widget.TextView.<init>(TextView.java:892)
at android.widget.TextView.<init>(TextView.java:886)
at android.widget.TextView.<init>(TextView.java:882)
at android.widget.TextView.<init>(TextView.java:878)
at currency.exchanger.UI.createTextViews(UI.kt:26)
at currency.exchanger.MainActivity.onCreate(MainActivity.kt:57)
at android.app.Activity.performCreate(Activity.java:7144)
at android.app.Activity.performCreate(Activity.java:7135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
Any ideas how can I successfully instantiate TextView from other class?
Try this way may help you
Class MainActivity: AppCompatActivity{
Other.init(this)
}
object Other{
fun init(mainActivity:Activity(or context)){
val textView = TextView(mainActivity)
}
}
private BillingClient billingClient = BillingClient.newBuilder(activity)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
I am integrating billing google api for purchase but when I call this method, I get an error in place of activity.
The error is:
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxxxx.xxxxx, PID: 9433
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xxxxx.xxxx/com.xxxx.coins_history.activity.CoinsHistoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2876)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
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:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:126)
at com.android.billingclient.api.BillingClientImpl.initialize(com.android.billingclient:billing##4.0.0:1)
at com.android.billingclient.api.BillingClientImpl.<init>(com.android.billingclient:billing##4.0.0:4)
at com.android.billingclient.api.BillingClientImpl.<init>(com.android.billingclient:billing##4.0.0:9)
at com.android.billingclient.api.BillingClient$Builder.build(com.android.billingclient:billing##4.0.0:4)
at com.jetmedianetwork.coins_history.activity.CoinsHistoryActivity.<init>(CoinsHistoryActivity.java:133)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1216)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
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:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
E/FirebaseCrashlytics: Error handling uncaught exception
java.util.concurrent.TimeoutException
at com.google.firebase.crashlytics.internal.common.Utils.awaitEvenIfOnMainThread(Utils.java:122)
at com.google.firebase.crashlytics.internal.common.CrashlyticsController.handleUncaughtException(CrashlyticsController.java:231)
at com.google.firebase.crashlytics.internal.common.CrashlyticsController$1.onUncaughtException(CrashlyticsController.java:153)
at com.google.firebase.crashlytics.internal.common.CrashlyticsUncaughtExceptionHandler.uncaughtException(CrashlyticsUncaughtExceptionHandler.java:54)
at com.facebook.internal.instrument.crashreport.CrashHandler.uncaughtException(CrashHandler.java:65)
at org.chromium.base.JavaExceptionReporter.uncaughtException(chromium-Monochrome.aab-stable-432418123:6)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1068)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1063)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1955)
I think the issue here is that there is a Null Pointer in your code since the billing client has not bee instantiated.
Try modifying the code as follows
private BillingClient billingClient = BillingClient!!.newBuilder(activity)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
My app works with different listviews. Acts normally, but when memory fills (during simulation or naturally) and system dumps it, app crashes with nullpointerexception on one of the declared ListViews.
Views are declared onCreate()
a_images = (ListView) findViewById(R.id.ascii_images);
a_emotes = (ListView) findViewById(R.id.ascii_emotes);
and then called onSectionAttached()
public void onSectionAttached(int number) {
switch (number) {
case 1:
status = 1;
mTitle = getString(R.string.title_section1);
a_images.invalidateViews();
a_images.refreshDrawableState();
Here's the logcat.
05-21 20:16:54.162 11230-11230/com.joudoviny.jonda.copypasta E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.joudoviny.jonda.copypasta, PID: 11230
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joudoviny.jonda.copypasta/com.example.jonda.copypasta.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.invalidateViews()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
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.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.invalidateViews()' on a null object reference
at com.example.jonda.copypasta.MainActivity.onSectionAttached(MainActivity.java:303)
at com.example.jonda.copypasta.MainActivity$PlaceholderFragment.onAttach(MainActivity.java:530)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1363)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:3166)
at android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:181)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
at com.example.jonda.copypasta.MainActivity.onCreate(MainActivity.java:99)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2784)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
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)
This happens whenever I use the ListView, in this case in invalidateViews() method.
Errors...
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vaidish.myapplication, PID: 12727
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6567)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6567)
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.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:224)
at android.app.AlertDialog$Builder.<init>(AlertDialog.java:454)
at com.example.vaidish.myapplication.Background.<init>(Background.java:25)
at com.example.vaidish.myapplication.MainActivity.userLogin(MainActivity.java:56)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6567)
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)
The root cause of your crash is this bit:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:224)
at android.app.AlertDialog$Builder.<init>(AlertDialog.java:454)
at com.example.vaidish.myapplication.Background.<init>(Background.java:25)
This says that you're passing null to the AlertDialog.Builder constructor. You could be doing that explicitly, like:
new AlertDialog.Builder(null)
or you could be doing it unintentionally, like:
new AlertDialog.Builder(mContext)
Check line 25 of your Background class, and make sure you're passing a non-null Context instance to the builder.
I am using https://github.com/BlacKCaT27/CurrencyEditText to enter currency into my application and I am using this code:
CurrencyEditText cet = new CurrencyEditText(this, null);
Here is the logcat:
Process: com.dharquissandas.budget, PID: 17620
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dharquissandas.budget/com.dharquissandas.budget.add_expense}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at android.view.View.<init>(View.java:4306)
at android.view.View.<init>(View.java:4454)
at android.widget.TextView.<init>(TextView.java:995)
at android.widget.EditText.<init>(EditText.java:79)
at android.widget.EditText.<init>(EditText.java:75)
at android.widget.EditText.<init>(EditText.java:71)
at com.blackcat.currencyedittext.CurrencyEditText.<init>(CurrencyEditText.java:33)
at com.dharquissandas.budget.add_expense.<init>(add_expense.java:29)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
What am I doing wrong I don't know, I am fairly new to android programming can someone help me out. What am I supposed to put in the attribute part of the code?
Here is my code: https://pastebin.com/57mTUMWX
You should not be creating view elements in your Code.
Add the view for CurrencyText to your XML.
Then you make sure it has an #+id/myName and find it by doing
CurrencyText myText = findViewById(R.id.myName);