0

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) 
SHAH MD IMRAN HOSSAIN
  • 2,558
  • 2
  • 25
  • 44
  • 1
    Does this answer your question? [Database Error: "NullPointerException: Attempt to invoke virtual method getApplicationInfo()... on a null object reference"](https://stackoverflow.com/questions/59122158/database-error-nullpointerexception-attempt-to-invoke-virtual-method-getappli) – Zain Jan 10 '21 at 18:00
  • 1
    The most probable reason for your issue is given by @Zain, but if it still persists you must paste some more code so that people can help you out. – rahat Jan 10 '21 at 18:09
  • @Zain yeah that is the issue. but code syntax changed now. I am writing my code solution in answer – SHAH MD IMRAN HOSSAIN Jan 10 '21 at 18:22

3 Answers3

0

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)
}
SHAH MD IMRAN HOSSAIN
  • 2,558
  • 2
  • 25
  • 44
0

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

Pejman Azad
  • 165
  • 1
  • 11
0

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"
  • this shows error. Cause **import kotlinx.android.synthetic.main.activity_main.*** is depreciated now.Can you provide a full run able code?. with import – SHAH MD IMRAN HOSSAIN Jan 11 '21 at 04:49
  • It is depreciated but you can still use it until september 2021, if you want to use view binding check out this video, it is easy to understand and very helpful: https://www.youtube.com/watch?v=MXZz438aCDM&ab_channel=Stevdza-San – Dušan Perić Jan 12 '21 at 21:21
  • this is the video from dec 4, 2020. Now, In updated kotlin sdk, I can't find it. – SHAH MD IMRAN HOSSAIN Jan 13 '21 at 19:08