“Unresolved Reference: activity_main” 可以翻译为:”未解决的引用:activity_main”

huangapple go评论83阅读模式
英文:

Android Studio, "Unresolved Reference: activity_main"

问题

R.layout.activity_mainR.string.app_name 是有问题的代码行。

在你的代码中,这些引用是使用资源标识符来获取布局和字符串资源的。问题可能是由于以下几种原因之一导致的:

  1. 请确保你的布局文件 activity_main.xml 和字符串资源在正确的位置,并且它们具有与代码中引用的名称相匹配的资源标识符。

  2. 请确保你的资源文件位于正确的目录中。通常,布局文件应该位于 res/layout 目录下,而字符串资源应该位于 res/values/strings.xml 中。

  3. 检查你的项目中是否存在任何资源命名冲突。如果你的项目中有多个模块或库,并且它们具有相同的资源标识符,可能会导致冲突。

如果你仍然遇到问题,可能需要手动检查并确保资源文件的名称与代码中的引用匹配,并且资源文件位于正确的位置。希望这能帮助你解决 "Unresolved Reference: activity_main" 和 "Unresolved Reference: app_name" 的问题。

英文:

I am following this tutorial and cannot figure out how to resolve the errors "Unresolved Reference: activity_main" and "Unresolved Reference: app_name". The tutorial was created fairly recently, so I don't think it's deprecated, but I have only recently reinstalled Android Studio, so everything should be default.

When trying to create a resource with that name, both resources fail to create because they already exist.

Here is the MainActivity.kt:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.Manifest
import android.content.pm.PackageManager
import android.net.Uri
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.util.concurrent.Executors
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.nio.ByteBuffer
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.ExecutorService
typealias LumaListener = (luma: Double) -> Unit

class MainActivity : AppCompatActivity() {
   private var imageCapture: ImageCapture? = null

   private lateinit var outputDirectory: File
   private lateinit var cameraExecutor: ExecutorService

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

       // Request camera permissions
       if (allPermissionsGranted()) {
           startCamera()
       } else {
           ActivityCompat.requestPermissions(
               this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
       }

       // Setup the listener for take photo button
       camera_capture_button.setOnClickListener { takePhoto() }

       outputDirectory = getOutputDirectory()

       cameraExecutor = Executors.newSingleThreadExecutor()
   }

   private fun takePhoto() {}

   private fun startCamera() {}

   private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
       ContextCompat.checkSelfPermission(
           baseContext, it) == PackageManager.PERMISSION_GRANTED
   }

   private fun getOutputDirectory(): File {
       val mediaDir = externalMediaDirs.firstOrNull()?.let {
           File(it, resources.getString(R.string.app_name)).apply { mkdirs() } }
       return if (mediaDir != null && mediaDir.exists())
           mediaDir else filesDir
   }

   override fun onDestroy() {
       super.onDestroy()
       cameraExecutor.shutdown()
   }

   companion object {
       private const val TAG = "CameraXBasic"
       private const val FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS"
       private const val REQUEST_CODE_PERMISSIONS = 10
       private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
   }
}

R.layout.activity_main and R.string.app_name are the lines in question.

Here is the Module: app build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "com.example.cameraxapp"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    def camerax_version = "1.0.0-beta07"
// CameraX core library using camera2 implementation
    implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX Lifecycle Library
    implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
    implementation "androidx.camera:camera-view:1.0.0-alpha14"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

I've seen people suggest removing import android.R, but I cannot find anything in my project with this import statement. Any suggestions as to which file this statement might be in would be appreciated.

答案1

得分: 3

在MainActivity.kt的第0行添加[NAME_PROJECT]包,例如package com.example.myapplication

英文:

You need to add in line 0 of MainActivity.kt package [NAME_PROJECT] with project name, for example package com.example.myapplication

答案2

得分: 1

把这行代码添加到 MainActivity.kt 的顶部,以恢复包名:

package com.example.myapplication

但要将 com.example.myapplication 替换为您的包名。

英文:

The example that you pasted in didn't include the package name on the first line. To restore the package name, add this line to the top of MainActivity.kt:

package com.example.myapplication

Except replace com.example.myapplication with your package name.

答案3

得分: 1

我遇到了这个问题,原因是我不小心编辑了加载此信息的代码行。确保你的MainActivity.kt文件中包含以下行:

import android.R.*
英文:

I had this issue and it was because I accidentally edited the line that loads this info. Make sure you have the line below in your MainActivity.kt

import android.R.*

huangapple
  • 本文由 发表于 2020年8月6日 22:32:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63285854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定