Reputation: 3
in my action bar I put a button that goes to another activity but the code that does not work I'm testing and the application fails.
This is the code file menu (main.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.hello.turidf.MainActivity" >
<item
android:id="@+id/action_about"
android:icon="@drawable/ic_action_about"
android:orderInCategory="100"
android:title="@string/action_about"
app:showAsAction="always"
android:onClick="enterInfo" />
</menu>
And this is the code of the Java file (MainActivity.java):
package com.hello.turidf;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_about) {
Intent intent = new Intent(this, InfoActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Views: 2477
Reputation: 766
Your code is correct. I guess isn't working because you use the line in your menu.xml
android:onClick="enterInfo"
The program is expecting that you has a enterInfo method that is called when you click the button, and the OnOptionsItemSelected doesn't get called. Just take out the line and try.
Upvotes: 1