Android TextView Showing Fatal Error in Kotlin - android

I am trying to initialize TextView in Kotlin as following
val textSelectedFood = findViewById<TextView>(R.id.textSelectedFood) as TextView
I also tried this
val textSelectedFood: TextView = findViewById<TextView>(R.id.textSelectedFood) as TextView
And also this
val textSelectedFood: TextView = findViewById(R.id.textSelectedFood) as TextView
And this
val textSelectedFood: TextView = findViewById<TextView>(R.id.textSelectedFood)
But still getting following errors. Don't know why
2021-01-10 23:38:26.640 29636-29636/com.imran.android.dinnerdecider E/d.dinnerdecide: Unknown bits set in runtime_flags: 0x8000
2021-01-10 23:38:27.091 29636-29636/com.imran.android.dinnerdecider E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.imran.android.dinnerdecider, PID: 29636
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.imran.android.dinnerdecider/com.imran.android.dinnerdecider.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:738)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
at com.imran.android.dinnerdecider.MainActivity.<init>(MainActivity.kt:12)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
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:930) 

I got hints from this question.
but the code changed a lot over the year.
That's why I am writing the current solution code in the below which solved my problem.
private lateinit var textSelectedFood: TextView // withoud lateinit code will show error
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textSelectedFood = findViewById(R.id.textSelectedFood)
}

If you do not have to "findViewById" have many better and safer options:
1.synthetic (deprecated)
Call the view directly in your code.
2.view binding
For more information check this link:https://developer.android.com/topic/libraries/view-binding

This technique is old now, just use "View binding".
Or you can just call the view by it's id and that'll be it.
In your case:
textSelectedFood.text = "Example of doing something"

Related

Android Studio Fatal Exception: main counter app

Hey guys I wanted to build a counter app. But this error pops up when I want to test it:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.change, PID: 7804
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.change/com.example.change.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property fighter1 has not been initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property fighter1 has not been initialized
at com.example.change.MainActivity.onCreate(MainActivity.kt:21)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
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:930) 
I don't know if I have coded everything correctly because I just started learning Android Studio, so I still don't know a lot about it. This is my code (I'm new to Kotlin):
package com.example.change
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private var counter1 = 0;
private lateinit var counterRed1: TextView
private lateinit var fighter1: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
counterRed1 = findViewById(R.id.counterRed1);
fighter1.setOnClickListener {
counter1 ++
counterRed1.text = (Integer.toString(counter1))
}
}
}
You have missed initializing your fighter1 button field. You need to initialize it just like you've initialized the counterRed1 text field.
counterRed1 = findViewById(R.id.counterRed1);
fighter1 = findViewById(R.id.fighter1);
Also make sure your activity_main layout file has a Button with the id fighter1.

How can I remove my NullPointerException? [duplicate]

This question already exists:
Attempt to invoke virtual method on a null object reference -> resolve?
Closed 1 year ago.
I retry to post my question because I don't have receive an answer : my app close itself when i click, an I find as cause that :
Process: fr.amseu.mystretching, PID: 15005
java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.amseu.mystretching/fr.amseu.mystretching.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3556)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
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:1075)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at fr.amseu.mystretching.MainActivity.onCreate(MainActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:7955)
at android.app.Activity.performCreate(Activity.java:7944)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:237) 
at android.app.ActivityThread.main(ActivityThread.java:7948) 
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:1075) 
As context, I give my Main activity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val exerciseButton = findViewById<ImageButton>(R.id.imageButton)
exerciseButton.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
My layout which contain the Image button. It is a part of a recycler view if it change anything ? :
<ImageButton
android:id="#+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/lower_legs"
android:contentDescription="#string/home_page_first_button" />
And I have finally my second activity (as a kotlin class file), where the click should redirect me :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_exercices)
}
}
Thanks for any help...
My layout which contain the Image button. It is a part of a recycler view if it change anything ?
It does change a lot. You're trying to find a view that does not exist when onCreate is called because you have not initialized the recycler view or its adapter (as far as the code you showed).
See this post for handling a click on a RecyclerView item: RecyclerView onClick

How to use citeproc-java on Android

Im investigating de.undercouch:citeproc-java:2.0.0 within my current Android application.
I cannot identify a ScriptEngine that works on Android with citeproc-java.
my Gradle resembles this:-
ext {
compileSdkVersion = 30
minSdkVersion = 26
targetSdkVersion = 30
}
dependencies {
implementation 'io.apisense:rhino-android:1.1.1'
implementation 'de.undercouch:citeproc-java:2.0.0'
implementation 'org.citationstyles:styles:20.11'
implementation 'org.citationstyles:locales:20.11'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.10"
}
Citeproc requires javax.script.ScriptEngineManager which is not available on the Android platform therefore I have substituted io.apisense:rhino-android:1.1.1
I am now facing this exception...
2020-11-19 10:02:22.325 4426-4426/com.google.android.gms.location.sample.basiclocationsample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.android.gms.location.sample.basiclocationsample, PID: 4426
java.lang.RuntimeException: Unable to start activity ComponentInfo{----}: java.lang.IllegalArgumentException: Could not make bibliography
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: java.lang.IllegalArgumentException: Could not make bibliography
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:797)
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764)
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750)
at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86)
at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
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:930) 
Caused by: de.undercouch.citeproc.script.ScriptRunnerException: Could not call method
at de.undercouch.citeproc.script.JREScriptRunner.callMethod(JREScriptRunner.java:87)
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:784)
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764) 
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750) 
at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86) 
at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66) 
at android.app.Activity.performCreate(Activity.java:7802) 
at android.app.Activity.performCreate(Activity.java:7791) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
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:930) 
Caused by: javax.script.ScriptException: org.mozilla.javascript.EcmaError: TypeError: Cannot read property "strings" from undefined (<Unknown source>#14312) in <Unknown source> at line number 14312
at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:330)
at com.sun.script.javascript.RhinoScriptEngine.invokeMethod(RhinoScriptEngine.java:296)
at de.undercouch.citeproc.script.JREScriptRunner.callMethod(JREScriptRunner.java:84)
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:784) 
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764) 
at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750) 
at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86) 
at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66) 
at android.app.Activity.performCreate(Activity.java:7802) 
at android.app.Activity.performCreate(Activity.java:7791) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
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:930) 
Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot read property "strings" from undefined (<Unknown source>#14312)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4198)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4176)
at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4209)
at org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:4228)
at org.mozilla.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:4240)
at org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1570)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1336)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:911)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:146)
at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:139)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3508)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:107)
2020-11-19 10:02:22.325 4426-4426/com.google.android.gms.location.sample.basiclocationsample E/AndroidRuntime: at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:324)
... 22 more
The citeproc code that causes this exception is taken straight from their examples...
private fun what() {
val citeproc = CSL(MyItemProvider(), "ieee")
citeproc.setOutputFormat("text")
citeproc.registerCitationItems("ID-1", "ID-2", "ID-3")
val s1 = citeproc.makeCitation("ID-1")
println(s1[0].text)
//=> [1] (for the "ieee" style)
//=> [1] (for the "ieee" style)
val s2 = citeproc.makeCitation("ID-2")
println(s2[0].text)
//=> [2]
//=> [2]
val bibl = citeproc.makeBibliography() //EXCEPTION RAISED HERE!!!!!!
for (entry in bibl.entries) {
println(entry)
}
}
I've created an AndroidJUnit to investigate this issue.
While stepping through the code I have found the issue is an arrayOutOfBoundsException accessing the strings[]
The array has only 12 entries however the index is 39
as shown in the AS image
As I saw values shown in debugger its shows 12 items in iCode[] array .
And from.pc has value 39 that is the cause of array index out of exception.
As par your code from.pc value increase by pre increments by one condition and in other value increase by post increments adding 2 each time.
And you are using continue with out checking size of array that is always creates exception.
You should check your conditions because it look like array does initialize properly if expect value at 39th position or increments in index are improper.

My android app crashes on Android 10 after upgrading the compileSdkVersion to 29

My android app crashes on Android 10 after upgrading the compileSdkVersion to 29
This is the crash message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.app.FragmentManagerImpl.getTargetSdk(FragmentManager.java:2975)
at android.app.FragmentManagerImpl.attachController(FragmentManager.java:2964)
at android.app.FragmentController.attachHost(FragmentController.java:88)
at android.app.Activity.attach(Activity.java:7710)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3224)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
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:930) 
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.epicsyst.ibc/com.epicsyst.ibc.view.activities.Splash}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.app.FragmentManagerImpl.getTargetSdk(FragmentManager.java:2975)
at android.app.FragmentManagerImpl.attachController(FragmentManager.java:2964)
at android.app.FragmentController.attachHost(FragmentController.java:88)
at android.app.Activity.attach(Activity.java:7710)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3224)
I searched the error message in Google and it gave me this on Github:
https://github.com/takahirom/DownloadableCalligraphy/pull/4/commits/3707383200749b385cc77adb2bb1aaf447420d62 but i couldn't relate!!
This is the activity xml file
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.activities.Splash">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The java file
package com.****.utils;
import android.content.Context;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.******.R;
import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
applyAppFont();
}
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
/**
* This method to apply custom font in activity using third-party library.
*/
private void applyAppFont() {
CalligraphyConfig.initDefault(
new CalligraphyConfig.Builder().setDefaultFontPath("fonts/DroidKufi-Regular.ttf")
.setFontAttrId(R.attr.fontPath)
.build());
}
}
It seems that it's a knowed issue for the Calligraphy library : https://github.com/chrisjenx/Calligraphy/issues/475
Could you try to migrate to the Caliligraphy 3 : https://github.com/InflationX/Calligraphy ?

Can't initialise Visualizer on Android using Kotlin

I am currently having trouble while trying to display a "blast" view from the media playing, using this library: https://github.com/gauravk95/audio-visualizer-android
I am working in an activity coded using Kotlin
I did as advised in the readme, but am having an issue when my activity starts:
E/AudioEffect: set(): AudioFlinger could not create effect e46b26a0-dddd-11db-8afd-0002a5d5c51b / `�?�v, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.codex_d.dev.android, PID: 31116
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codex_d.dev.android/com.codex_d.dev.android.runningActivities.AudioPlayerActivity}: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.media.audiofx.Visualizer.<init>(Visualizer.java:221)
at com.gauravk.audiovisualizer.base.BaseVisualizer.setAudioSessionId(BaseVisualizer.java:196)
at com.codex_d.dev.android.runningActivities.AudioPlayerActivity.onCreate(AudioPlayerActivity.kt:26)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
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:930) 
I/Process: Sending signal. PID: 31116 SIG: 9
Process 31116 terminated.
I have declared both RECORD_AUDIO and MODIFY_AUDIO_SETTINGS permissions in the manifest.
Here is my code for the activity:
class AudioPlayerActivity : BaseAdventureActivity(R.layout.activity_audio_player, true) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val dataLocation = intent.getSerializableExtra(EXTRA_LOCATION) as DataLocation
val path = "sample_audio_happy"
var mediaPlayer = MediaPlayer.create(this, dataLocation.getUri(path, this, adventure))
mediaPlayer.start()
if (mediaPlayer.audioSessionId != -1) blast.setAudioSessionId(mediaPlayer.audioSessionId)
}
override fun onDestroy() {
super.onDestroy()
blast.release()
}
When I comment the line with setAudioSessionId everything works fine (but of course without showing the visualisation).
Would anyone know where this error could come from?
Indeed, #Tenfour04 is right. Here are the two lines of code that were required:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, Array(1){Manifest.permission.RECORD_AUDIO}, 0)
}
Just add before stting audioSessionId, and it should work.
Thanks for your help!

Categories

Resources