How do I resolve a Redeclaration Error in Android Studio

Related searches

This code is for a button that would be on the android emulator. However, when I place this code in the main activity.kt it gives me multiple errors. The first error I experience is a redeclaration error on line 9 on the main activity

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

This activity displays an order form to order coffee.

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);
   }
}
  1. file tab -> Invalidate Caches/Restart (then choose invalidate and restart from the dialog that will appear)
  2. Build tab -> Clean Project 3.Build tab -> Rebuild

This should fix the problem. this solution is just the same as @Ehsan_Haghdoust solution but letting Android Studio do that for me instead of doing that my self.

Kotlin file changes result in Redeclaration error, Using beta Android Studio; Reordering the plugins in my gradle file. The current build is using: org.jetbrains. To make matters worse, when Android Studio encounters the R.layout error, it’ll usually flag all your layout resource files as containing errors, which makes it difficult to know where to start

The problem you face is that you have 2 activities with the same name MainActivity - one in Java, and second in Koltin. Both classes (activities in this case) are compiled into the same application - you have 2 symbols with the same name.

yes, Koltin and Java look the same after the compiler finish with them :)

Kotlin Redeclaration Error with Maven Project � Issue #168 � spring , Now it's broken again and I can't seem to fix it by removing the nature and reinstating it. I've also tried to uninstall and then reinstall the kotlin� To make matters worse, when Android Studio encounters the R.layout error, it’ll usually flag all your layout resource files as containing errors, which makes it difficult to know where to start looking for the source of the error. Often, the most effective solution is the simplest: clean and rebuild your project.

The question is for 6 months ago, but I write my answer for others face this problem in the future. I have had challenge with this error, I checked every possible way suggested by others and I FINALLY had to delete build folders in

projectFolder/

and

projectFolder/app/

manually and rebuild the project again.

Doesn't work with Android Studio 2.0 preview � Issue #36 � Kotlin , I get 237 errors and failure at compilation, all along the lines of Error:(5, 1) Kotlin: ' public fun todoTask0(): Doesn't work with Android Studio 2.0 preview #36 Error:(7, 5) Kotlin: Redeclaration: idea I was able to solve this by using the ' VCS -> Checkout from Version Control' option in Android Studio. Sometimes, you may encounter strange errors about the R.class, like package R dost not exist orR cannot be resolved as a type. You can fix these errors by using the following steps: Click Build—>

how to turn off the "redeclaration" error? - NI Community, I can't seem to get rid of the redeclaration error. especially if you're using some CVI standard headers and some Windows SDK headers. Ben Jakuben I just started Android Studio and I got this "Gradle Sync Failed" error, I fixed it. I uninstalled Android Studio, deleted all the files that might have been on my PC, everything including the project files in my documents. Then I re installed Android Studio and realized it started downloading few gradle files on its own.

Usually the multi Dex issue occurs when the application uses more memory than the allocated memory(65k) and memory exceeds when the project uses additional dependencies .

Android Studio comes with various tools that can help you analyze the way your app is using memory, including Memory Monitor, Allocation Tracker, HPROF Viewer, and the Android Device Monitor’s ‘Heap’ tab.

Comments
  • why didn't you provide the error messages?
  • Please format the question better next time, I fixed it for you now. Welcome to StackOverflow!