Home » Java » java – No Such Field Error in android project using kotlin-Exceptionshub

java – No Such Field Error in android project using kotlin-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I get a NoSuchField runtime error in my android app using Kotlin whereas the field exists
I’ve tried renaming as stated here No static field list_container of type I in class Landroid/support/v7/preference/R$id yet no solution

This is my MainActivityWarrant class

package com.android.example.cameraxbasic

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.android.example.cameraxbasic.helpers.PH
import com.android.example.cameraxbasic.utils.FLAGS_FULLSCREEN
import java.io.File


const val KEY_EVENT_ACTION = "key_event_action"
const val KEY_EVENT_EXTRA = "key_event_extra"
private const val IMMERSIVE_FLAG_TIMEOUT = 500L


/**
 * Main entry point into our app. This app follows the single-activity pattern, and all
 * functionality is implemented in the form of fragments.
 */
class MainActivityWarrant : AppCompatActivity() {
    private lateinit var container: FrameLayout


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        container = this.findViewById(R.id.fragment_contain)


        Zonedata =  PH.get().getString(this.applicationContext, "ZoneData", "")
        Statedata = PH.get().getString(this.applicationContext, "StateData", "")
        Prisondata = PH.get().getString(this.applicationContext, "PrisonData", "")

//         = intent.getStringExtra("Zonedata")
//        intent.getStringExtra("Statedata")
//        intent.getStringExtra("Prisondata")

        val currentDBPath = getDatabasePath("your_db_name").absolutePath
        println()
        println("$Zonedata <<<<<<<>>>>>>>>>> $Statedata <<<<<<<<>>>>>>> $Prisondata")
    }

    override fun onResume() {
        super.onResume()
        // Before setting full screen flags, we must wait a bit to let UI settle; otherwise, we may
        // be trying to set app to immersive mode before it's ready and the flags do not stick
        container.postDelayed({
            container.systemUiVisibility = FLAGS_FULLSCREEN
        }, IMMERSIVE_FLAG_TIMEOUT)
    }

    /** When key down event is triggered, relay it via local broadcast so fragments can handle it */
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return when (keyCode) {
            KeyEvent.KEYCODE_VOLUME_DOWN -> {
                val intent = Intent(KEY_EVENT_ACTION).apply { putExtra(KEY_EVENT_EXTRA, keyCode) }
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
                true
            }
            else -> super.onKeyDown(keyCode, event)
        }
    }

    companion object {

        var Zonedata: String = ""
        var Statedata: String = ""
        var Prisondata: String = ""
        /** Use external media if it is available, our app's file directory otherwise */
        fun getOutputDirectory(context: Context): File {
            val appContext = context.applicationContext
            val mediaDir = context.externalMediaDirs.firstOrNull()?.let {
                File(it, appContext.resources.getString(R.string.app_name) + "/" + Zonedata + "/" + Statedata + "/" + Prisondata).apply { mkdirs() }
            }
            return if (mediaDir != null && mediaDir.exists())
                mediaDir else appContext.filesDir
        }


        //send selected prison values to be used as file name

        //        var ZoneNameInFull:String = Zonedata
//        var StateNameInFull:String = Statedata
//        var PrisonNameInFull:String = Prisondata
        @SuppressLint("StaticFieldLeak")
        var openCameraForNewDocument = SelectPrisonActivity()


    }

//    fun openCamera(view: View) {}
}

and this is the log

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.virtusync.scanningtool, PID: 21098
    java.lang.NoSuchFieldError: No static field fragment_contain of type I in class Lcom/android/example/cameraxbasic/R$id; or its superclasses (declaration of 'com.android.example.cameraxbasic.R$id' appears in /data/app/com.virtusync.scanningtool-ud2au9eFKL3JmFqFoLWYtQ==/base.apk!classes2.dex)
        at com.android.example.cameraxbasic.MainActivityWarrant.onCreate(MainActivityWarrant.kt:49)
        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:2990)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3148)
        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:1861)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6819)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
How to&Answers:

I believe your issue is because there is no element in any of your xml that has the id of “fragment_contain”. Could you double check that your id matches whichever element you are looking for? Should look something like:

<android.widget.FrameLayout
        android:id="@+id/fragment_contain"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />