Reputation: 35
I am new to android studio.
I have an error using intent in android studio. I followed the steps on youtube (https://www.youtube.com/watch?v=5rQILkqDpWU&t=5s)
I have a button that when clicked will be directed to an Activity but its not working.
Here are my codes
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void addItem(View view)
{
Intent intent1 = new Intent(this, AddFood.class);
startActivity(intent1);
}
Upvotes: 1
Views: 1845
Reputation: 1343
You should add an attribute in XML android:onClick
<Button android:id="@+id/button_really"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Awesome!"
android:onClick="addItem" />
Now you need to define method with same name in your JAVA file
public void addItem(View v) {
Intent intent1 = new Intent(this, AddFood.class);
startActivity(intent1);
}
Last and most important part, make sure both Activities are defined in your Manifest file :)
Upvotes: 1