RuntimeException : when calling BillingClient function in in app purchase - android

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();

Related

Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference issue

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)
}
}

Android BiometricPrompt throws NPE

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) 

kotlin.Unit cannot be cast to "Class" Dagger 2 in Android Kotlin

I am developing an Android app using Kotlin. I am trying to understand how Dagger 2 works to perform dependency injection. I am very new to Dagger 2. I am following this tutorial,https://medium.com/#elye.project/dagger-2-for-dummies-in-kotlin-with-one-page-simple-code-project-618a5f9f2fe8 to understand how it works and applying it to my project too. But I am getting an error. Following is what I have done so far.
I created an interface called AppComponent with the following code
package com.example.utils
import com.example.memento.LoginActivity
import dagger.Component
#Component
interface AppComponent
{
fun inject(app: LoginActivity)
}
I also created a class called AuthService with the following code.
package com.example.services
import javax.inject.Inject
class AuthService #Inject constructor()
{
public fun login(): String
{
return "I am logged in";
}
}
This is my LoginActivity class
class LoginActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val authService = DaggerAppComponent.create().inject(this) as AuthService
tv_testing.text = authService.login();
}
}
When I run my app, I get the following error
java.lang.ClassCastException: kotlin.Unit cannot be cast to com.example.services.AuthService
at com.example.memento.LoginActivity.onCreate(LoginActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
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)
E/MonitoringInstr: Exception encountered by: com.example.memento.LoginActivity#39cc00e. Dumping thread state to outputs and pining for the fjords.
java.lang.ClassCastException: kotlin.Unit cannot be cast to com.example.services.AuthService
at com.example.memento.LoginActivity.onCreate(LoginActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
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)
E/THREAD_STATE: Thread[MonitoringInstrumentation,5,main]
java.lang.Object.wait(Native Method)
android.app.Instrumentation.startActivitySync(Instrumentation.java:496)
android.app.Instrumentation.startActivitySync(Instrumentation.java:435)
androidx.test.runner.MonitoringInstrumentation.access$101(MonitoringInstrumentation.java:98)
androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:443)
androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:440)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
java.lang.Thread.run(Thread.java:764)
Thread[Binder:11719_2,5,main]
Thread[magnifier pixel copy result handler,5,main]
android.os.MessageQueue.nativePollOnce(Native Method)
android.os.MessageQueue.next(MessageQueue.java:326)
android.os.Looper.loop(Looper.java:160)
android.os.HandlerThread.run(HandlerThread.java:65)
Thread[FinalizerWatchdogDaemon,5,system]
java.lang.Object.wait(Native Method)
java.lang.Daemons$FinalizerWatchdogDaemon.sleepUntilNeeded(Daemons.java:297)
java.lang.Daemons$FinalizerWatchdogDaemon.runInternal(Daemons.java:277)
java.lang.Daemons$Daemon.run(Daemons.java:103)
java.lang.Thread.run(Thread.java:764)
Thread[InstrumentationConnectionThread,5,main]
android.os.MessageQueue.nativePollOnce(Native Method)
android.os.MessageQueue.next(MessageQueue.java:326)
android.os.Looper.loop(Looper.java:160)
android.os.HandlerThread.run(HandlerThread.java:65)
Thread[Profile Saver,5,system]
Thread[main,5,main]
dalvik.system.VMStack.getThreadStackTrace(Native Method)
java.lang.Thread.getStackTrace(Thread.java:1538)
java.lang.Thread.getAllStackTraces(Thread.java:1588)
androidx.test.runner.MonitoringInstrumentation.getThreadState(MonitoringInstrumentation.java:643)
androidx.test.runner.MonitoringInstrumentation.dumpThreadStateToOutputs(MonitoringInstrumentation.java:638)
androidx.test.runner.MonitoringInstrumentation.onException(MonitoringInstrumentation.java:632)
androidx.test.runner.AndroidJUnitRunner.onException(AndroidJUnitRunner.java:522)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
android.os.Handler.dispatchMessage(Handler.java:106)
android.os.Looper.loop(Looper.java:193)
android.app.ActivityThread.main(ActivityThread.java:6669)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Thread[Binder:11719_1,5,main]
Thread[ReferenceQueueDaemon,5,system]
java.lang.Object.wait(Native Method)
java.lang.Daemons$ReferenceQueueDaemon.runInternal(Daemons.java:178)
java.lang.Daemons$Daemon.run(Daemons.java:103)
java.lang.Thread.run(Thread.java:764)
Thread[Jit thread pool worker thread 0,5,main]
Thread[FinalizerDaemon,5,system]
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:422)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:188)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:209)
java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:232)
java.lang.Daemons$Daemon.run(Daemons.java:103)
java.lang.Thread.run(Thread.java:764)
Thread[HeapTaskDaemon,5,system]
Thread[Instr: com.example.memento.MockTestRunner,5,main]
java.lang.Object.w
E/MonitoringInstr: Dying now...
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.memento, PID: 11719
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memento/com.example.memento.LoginActivity}: java.lang.ClassCastException: kotlin.Unit cannot be cast to com.example.services.AuthService
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: java.lang.ClassCastException: kotlin.Unit cannot be cast to com.example.services.AuthService
at com.example.memento.LoginActivity.onCreate(LoginActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
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) 
E/MonitoringInstr: Exception encountered by: Thread[main,5,main]. Dumping thread state to outputs and pining for the fjords.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memento/com.example.memento.LoginActivity}: java.lang.ClassCastException: kotlin.Unit cannot be cast to com.example.services.AuthService
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: java.lang.ClassCastException: kotlin.Unit cannot be cast to com.example.services.AuthService
at com.example.memento.LoginActivity.onCreate(LoginActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
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) 
E/THREAD_STATE: Thread[MonitoringInstrumentation,5,main]
java.lang.Object.wait(Native Method)
android.app.Instrumentation.startActivitySync(Instrumentation.java:496)
android.app.Instrumentation.startActivitySync(Instrumentation.java:435)
androidx.test.runner.MonitoringInstrumentation.access$101(MonitoringInstrumentation.java:98)
androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:443)
androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:440)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
java.lang.Thread.run(Thread.java:764)
Thread[Binder:11719_2,5,main]
Thread[magnifier pixel copy result handler,5,main]
android.os.MessageQueue.nativePollOnce(Native Method)
android.os.MessageQueue.next(MessageQueue.java:326)
android.os.Looper.loop(Looper.java:160)
android.os.HandlerThread.run(HandlerThread.java:65)
Thread[FinalizerWatchdogDaemon,5,system]
java.lang.Object.wait(Native Method)
java.lang.Daemons$FinalizerWatchdogDaemon.sleepUntilNeeded(Daemons.java:297)
java.lang.Daemons$FinalizerWatchdogDaemon.runInternal(Daemons.java:277)
java.lang.Daemons$Daemon.run(Daemons.java:103)
java.lang.Thread.run(Thread.java:764)
Thread[InstrumentationConnectionThread,5,main]
android.os.MessageQueue.nativePollOnce(Native Method)
android.os.MessageQueue.next(MessageQueue.java:326)
android.os.Looper.loop(Looper.java:160)
android.os.HandlerThread.run(HandlerThread.java:65)
Thread[Profile Saver,5,system]
Thread[main,5,main]
dalvik.system.VMStack.getThreadStackTrace(Native Method)
java.lang.Thread.getStackTrace(Thread.java:1538)
java.lang.Thread.getAllStackTraces(Thread.java:1588)
androidx.test.runner.MonitoringInstrumentation.getThreadState(MonitoringInstrumentation.java:643)
androidx.test.runner.MonitoringInstrumentation.dumpThreadStateToOutputs(MonitoringInstrumentation.java:638)
androidx.test.runner.MonitoringInstrumentation.onException(MonitoringInstrumentation.java:632)
androidx.test.runner.AndroidJUnitRunner.onException(AndroidJUnitRunner.java:522)
androidx.test.runner.MonitoringInstrumentation$3.uncaughtException(MonitoringInstrumentation.java:268)
java.lang.Thread.dispatchUncaughtException(Thread.java:1955)
Thread[Binder:11719_1,5,main]
Thread[ReferenceQueueDaemon,5,system]
java.lang.Object.wait(Native Method)
java.lang.Daemons$ReferenceQueueDaemon.runInternal(Daemons.java:178)
java.lang.Daemons$Daemon.run(Daemons.java:103)
java.lang.Thread.run(Thread.java:764)
Thread[Jit thread pool worker thread 0,5,main]
Thread[FinalizerDaemon,5,system]
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:422)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:188)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:209)
java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:232)
java.lang.Daemons$Daemon.run(Daemons.java:103)
java.lang.Thread.run(Thread.java:764)
Thread[HeapTaskDaemon,5,system]
Thread[Instr: com.example.memento.MockTestRunner,5,main]
java.lang.Object.wait(Native Method)
java.lang.Thread.parkFor$(Thread.java:2137)
sun.misc.Unsafe.park(Unsafe.java:358)
java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:230)
java.util.concurrent.FutureTask.awaitDone(FutureTask.java:447)
java.util.concurrent.FutureTask.get(FutureTask.java:205)
androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:448)
androidx.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:354)
androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:525)
org.junit.rules.RunRules.evaluate(RunRules.java:20)
org.junit.runners.ParentRunner.runLeaf(ParentRunner
E/MonitoringInstr: Dying now...
W/MonitoringInstr: Invoking uncaught exception handler java.lang.ThreadGroup[name=main,maxpri=10] (a class java.lang.ThreadGroup)
I/Process: Sending signal. PID: 11719 SIG: 9
Test running failed: Instrumentation run failed due to 'Process crashed.'
What is wrong with my code?
I tried updating the LoginActivity to this
class LoginActivity : AppCompatActivity()
{
lateinit var authService: AuthService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
DaggerAppComponent.create().inject(this)
tv_testing.text = authService.login()
}
}
Then I got this error
kotlin.UninitializedPropertyAccessException: lateinit property authService has not been initialized
at com.example.memento.LoginActivity.onCreate(LoginActivity.kt:27)
DaggerAppComponent.create().inject(this) doesn't return anything but you're trying to cast it to AuthService. Therefore, you get ClassCastException. Instead, you should first declare AuthService as lateinit var and annotate it with #Inject. Then you can call inject() function. You can see full code below:
class LoginActivity : AppCompatActivity()
{
#Inject
private lateinit var authService: AuthService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
DaggerAppComponent.create().inject(this)
tv_testing.text = authService.login();
}
}
I hope my answer helps.

java.lang.NullPointerException: Attempt to invoke virtual method parse

Trying to use parse server with AWS
I have tried several tutorials seems like I am missing somehing simple just don't know what it is
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.davidavila.myapplication, PID: 6492
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.davidavila.myapplication/com.example.davidavila.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
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: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
at com.parse.ParseCorePlugins.getCurrentInstallationController(ParseCorePlugins.java:267)
at com.parse.ParseInstallation.getCurrentInstallationController(ParseInstallation.java:56)
at com.parse.ParseInstallation.getCurrentInstallation(ParseInstallation.java:62)
at com.example.davidavila.myapplication.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
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) 
I/Process: Sending signal. PID: 6492 SIG: 9
package com.example.davidavila.myapplication;
import com.parse.Parse;
import com.parse.ParseInstallation;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseInstallation.getCurrentInstallation().saveInBackground();
Parse.enableLocalDatastore(this);
Parse.initialize (new Parse.Configuration.Builder(this)
.applicationId("1b5f82b054262c169a8bcc4b5a1f379cbac8bfd5")
.clientKey("f9f3b809dbada7fb5ed7793630e21893fb80fea3")
.server("http://13.58.197.143:80/parse/")
.build()
);
}
public void buttonMain (View view) {
Intent intent = new Intent(this, landingPage.class);
startActivity(intent);
}
}
Expect not to have an issue
Please try as follows
Parse.enableLocalDatastore(MainActivity.this);
Parse.initialize(new Parse.Configuration.Builder(MainActivity.this)
.applicationId("1b5f82b054262c169a8bcc4b5a1f379cbac8bfd5")
.clientKey("f9f3b809dbada7fb5ed7793630e21893fb80fea3")
.server("http://13.58.197.143:80/parse/")
.enableLocalDataStore()
.build());

android.content.res.Resources android.content.Context.getResources() error when using github repository

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);

Categories

Resources