我在一个应用程序中一直在使用带有两个MenuItems的ABS 4.0,但是发现了一个小错误:当按下第二个MenuItem时,它的功能与第一个相同。
我已经尝试了几乎所有我能想到的东西,但是没有用。我更改了onOptionItemSelected,因为我认为这是我需要编辑的方法。
编辑:
我一直在看@Ollie的建议,但是LogCat和Debug都没有显示出奇怪的东西。也许是在代码的其他部分,还是ABS的声明?这是完整的代码,如果您可以看一遍,那就太好了!
整个Activity的代码,也许在其他地方?
package bas.sie.Antonius;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class TeacherInfo extends SherlockActivity {
String URLhome;
String Info;
String TeacherAb;
TextView mTxtvInfo;
Button mBtnTeacherStSchedule;
Button mBtnTeacherDaySchedule;
private static String mainUrl = "http://www.carmelcollegegouda.nl/site_ant/";
private static String endUrl = ".htm";
private static String[] myUrls = { "roosters/dagroosters/Doc_V1_",
"roosters/standaardroosters/Doc1_" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactinfo);
setTitle("Over deze leraar");
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
mTxtvInfo = (TextView) findViewById(R.id.TxtvTeacher);
Intent startingIntent = getIntent();
Info = startingIntent.getStringExtra("contact");
mTxtvInfo.setText(Info);
Intent startingIntent1 = getIntent();
TeacherAb = startingIntent1.getStringExtra("abbrev");
mBtnTeacherDaySchedule = (Button) findViewById(R.id.btnTeacherDaySchedule);
mBtnTeacherStSchedule = (Button) findViewById(R.id.btnTeacherStSchedule);
mBtnTeacherDaySchedule.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
URLhome = makeUrl(0);
Intent i = new Intent(TeacherInfo.this, MyWebView.class);
i.putExtra("home", URLhome);
startActivityForResult(i, 0);
}
});
mBtnTeacherStSchedule.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
URLhome = makeUrl(1);
Intent i = new Intent(TeacherInfo.this, MyWebView.class);
i.putExtra("home", URLhome);
startActivityForResult(i, 0);
}
});
}
private String makeUrl(int index) {
String s = mainUrl + myUrls[index] + TeacherAb + endUrl;
return s;
}// makeurl
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Instellingen")
.setIcon(R.drawable.ic_settings)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Over de app")
.setIcon(R.drawable.ic_about)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, AntoniusActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.settings:
Intent i = new Intent(this, About.class);
startActivity(i);
return true;
case R.id.about:
Intent about = new Intent(this, About.class);
startActivity(about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
我认为问题出在菜单项的声明中,但是我看不到任何问题...
您能看看我的menu.xml吗?发表在这里:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/settings"
android:icon="@drawable/ic_settings"
android:title="Instellingen"></item>
<item android:id="@+id/about"
android:icon="@drawable/ic_about"
android:title="Over de app"></item>
</menu>
最佳答案
像这样创建菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
然后使用switch语句处理选择:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Do stuff
return true;
case R.id.menu_item_2:
// Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
编辑:最后,您应该对每个项目执行不同的操作,如果将Intent目标Activity更改为另一个,它将按照您的期望进行:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// ... Stuff ...
case R.id.settings: // Settings item
Intent i = new Intent(this, About.class); // Start About.java Activity, but item says "settings"
// TODO: Change About to Settings?
i = new Intent(this, Settings.class);
startActivity(i);
return true;
case R.id.about: // About item
Intent about = new Intent(this, About.class); // Start About.java Activty
startActivity(about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}