Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve a Redeclaration Error in Android Studio

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);
   }
}
like image 721
Jame Avatar asked Aug 01 '19 06:08

Jame


2 Answers

  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 Project

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

like image 97
Mohammad Elsayed Avatar answered Oct 05 '22 22:10

Mohammad Elsayed


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.

like image 44
Ehsan Haghdoust Avatar answered Oct 05 '22 22:10

Ehsan Haghdoust