I have two buttons on my main Activity, which both should lead to another activity, but when I run the code it is showing an error.
MainActivity.java is this:
package com.assignment2.courier;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void second (View v){
Intent i = new Intent(this, adelivery.class);
startActivity(i);
}
}
}
This is the button code in activity_main.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="85dp"
android:layout_marginBottom="234dp"
android:onClick="second"
android:text="Arrange Delivery"
android:textSize="20sp" />
Error: Failed to compile values file
What is the problem here?
CodePudding user response:
The problem here is that the second
function is inside the onCreate
You need to adapt the code as follows:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void second (View v){
Intent i = new Intent(this, adelivery.class);
startActivity(i);
}
}
CodePudding user response:
You should move second
method out of the onCreate
method; this is the problem in your code.