0

我是 android 开发(eclipse)的新手。我正在尝试做以下事情。1.单击eclipse中的菜单按钮时生成菜单列表 2.单击菜单项时导航另一个页面(xml页面)。我使用以下代码。

菜单.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/about" android:title="About"/>
<item android:id="@+id/help" android:title="Help" />
</menu>

MainActivity.java

package com.example.menuoptions;

import com.example.menuoptions.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.about:
    startActivity(new Intent(this, About.class));
    return true;
    case R.id.help:
    startActivity(new Intent(this, Help.class));
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
}
}

关于.java

 package com.example.menuoptions;
 import android.app.Activity;
 import android.os.Bundle;
 public class About extends Activity {
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);

   }
  }

关于.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="About Page" />

</LinearLayout>

问题是我的第二个需要。列出了菜单项,但是当单击 about 项时,它不会导航到 about.xml。

4

3 回答 3

1

你应该使用这个:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
    return super.onOptionsItemSelected(item);
}
}
于 2013-09-20T06:32:29.667 回答
0

您是否在清单文件中提供了权限?

 <activity android:name=".About" />
于 2013-09-20T06:29:42.837 回答
0
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

选择菜单的简单代码

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.about:
        startActivity(new Intent(this, About.class));
        return true;
    case R.id.help:
        startActivity(new Intent(this, Help.class));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

尝试使用这个

于 2013-09-20T06:34:29.737 回答