1

我在使用 Kotlin 的 android 应用程序中收到NoSuchField 运行时错误 ,而该字段存在我已尝试重命名,如此处所述No static field list_container of type I in class Landroid/support/v7/preference/R$id but no solution

这是我的 MainActivityWarrant 课程

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

这是日志

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

1 回答 1

0

我相信您的问题是因为您的任何 xml 中都没有 id 为“fragment_contain”的元素。您能否仔细检查您的 id 是否与您要查找的任何元素匹配?应该看起来像:

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

于 2020-02-19T15:06:37.167 回答