【问题标题】:How do I resolve a Redeclaration Error in Android Studio如何解决 Android Studio 中的重新声明错误
【发布时间】:2019-08-01 06:58:03
【问题描述】:

此代码适用于 android 模拟器上的按钮。但是,当我将此代码放在主要的 activity.kt 中时,它会给我带来多个错误。我遇到的第一个错误是主活动第 9 行的重新声明错误

package com.example.android.justjava

import android.R
import android.os.Bundle
import android.support.v7.app.ActionBarActivity
import android.view.View
import android.widget.TextView

// This activity displays an order form to order coffee.
class MainActivity : ActionBarActivity() {
    protected fun onCreate(savedInstanceState: Bundle) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
    }

    // this method is called when the order button is clicked.
    fun submitOrder(view: View) {
      display(1)
    }

    // This method displays the given quantity value on the screen.
    private fun display(number: Int) {
      val quantityTextView = findViewById(R.id.quantity_text_view as TextView
      quantityTextView.text = "" + number
    }
}

此活动显示订购咖啡的订单。

public class MainActivity extends ActionBarActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }

   // This method is called when the order button is clicked.
   public void submitOrder(View view) {
       display(1);
   }

   // This method displays the given quantity value on the screen.
   private void display(int number) {
      TextView quantityTextView =(TextView) findViewById (R.id.quantity_text_view);
      quantityTextView.setText("" + number);
   }
}

【问题讨论】:

  • 为什么不提供错误信息?
  • 下次请把问题格式改好,我现在给你解决了。欢迎使用 StackOverflow!

标签: java android kotlin


【解决方案1】:
  1. 文件选项卡 -> 使缓存无效/重新启动(然后从将出现的对话框中选择无效并重新启动)
  2. 构建选项卡 -> 清理项目
  3. 构建选项卡 -> 重建项目

此解决方案与 @Ehsan_Haghdoust 解决方案相同,但让 Android Studio 为我做这件事,而不是我自己做。

【讨论】:

    【解决方案2】:

    这个问题是 6 个月前的问题,但我写我的答案是为了其他人将来会遇到这个问题。我遇到了这个错误的挑战,我检查了其他人建议的所有可能方式,最后我不得不删除

    中的构建文件夹

    项目文件夹/

    项目文件夹/应用程序/

    手动重新构建项目。

    【讨论】:

      【解决方案3】:

      您面临的问题是您有 2 个具有相同名称的活动 MainActivity - 一个在 Java 中,第二个在 Koltin 中。两个类(在本例中为活动)都编译到同一个应用程序中 - 您有 2 个同名符号。

      是的,Koltin 和 Java 在编译器完成后看起来是一样的 :)

      【讨论】:

        【解决方案4】:

        在我的例子中,我在调试模式下运行应用程序,并为调试生成了一个方向类。然后我尝试在发布模式下生成一个签名的 APK。然后在同一包中为导致问题的发布模式生成了一个类似的类。我手动删除了Java文件夹(根)中的调试文件夹,构建成功。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-11
          • 2017-07-23
          • 1970-01-01
          • 2020-08-05
          • 2018-01-15
          • 1970-01-01
          • 2019-04-05
          • 2020-12-11
          相关资源
          最近更新 更多