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);
}
}
This solution is just the same as @Ehsan_Haghdoust solution but letting Android Studio do that for me instead of doing that my self.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With