0

我正在尝试运行遍历菜单及其工作方式的示例程序之一。但是每当我尝试编译和运行该示例时,我都会在标题中看到错误,并且无法查看任何菜单选项是否正常工作。

可能是这么简单,但在这一点上我感到疲倦和沮丧:(

package com.example.context_menu_demo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

 Button m_button;
 RelativeLayout ourlayout;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ourlayout = (RelativeLayout) findViewById(R.id.layout_id);
  m_button = (Button) findViewById(R.id.menu_but);
  registerForContextMenu(m_button);
 }

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  // TODO Auto-generated method stub
  super.onCreateContextMenu(menu, v, menuInfo);
  getMenuInflater().inflate(R.menu.menu, menu);
     menu.setHeaderTitle("Context Menu");
 }

 @Override
 public boolean onContextItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
  switch (item.getItemId()) {
  case R.id.red:
   ourlayout.setBackgroundColor(Color.RED);
   break;

  case R.id.green:
   ourlayout.setBackgroundColor(Color.GREEN);
   break;
  case R.id.blue:
   ourlayout.setBackgroundColor(Color.BLUE);

   break;
  case R.id.gray:
   ourlayout.setBackgroundColor(Color.GRAY);
   break;
  case R.id.cyan:
   ourlayout.setBackgroundColor(Color.CYAN);
   break;
  case R.id.yellow:
   ourlayout.setBackgroundColor(Color.YELLOW);
   break;

  }
  return super.onOptionsItemSelected(item);
 }
}

xmls

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout_id">

    <Button
        android:id="@+id/menu_but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="94dp"
        android:text="Menu" />


    <item android:id="@+id/red" android:title="RED"></item>
    <item android:id="@+id/green" android:title="GREEN"></item>
    <item android:id="@+id/blue" android:title="BLUE"></item>
    <item android:id="@+id/gray" android:title="GRAY"></item>
    <item android:id="@+id/cyan" android:title="CYAN"></item>
    <item android:id="@+id/yellow" android:title="YELLOW"></item>

</RelativeLayout>

xmls

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/red" android:title="RED"></item>
    <item android:id="@+id/green" android:title="GREEN"></item>
    <item android:id="@+id/blue" android:title="BLUE"></item>
    <item android:id="@+id/gray" android:title="GRAY"></item>
    <item android:id="@+id/cyan" android:title="CYAN"></item>
    <item android:id="@+id/yellow" android:title="YELLOW"></item>
</menu>

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.context_menu_demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.context_menu_demo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
4

2 回答 2

3

由于您的 RelativeLayout 中的这些行,您会遇到异常..删除这些行...

 <item android:id="@+id/red" android:title="RED"></item>
<item android:id="@+id/green" android:title="GREEN"></item>
<item android:id="@+id/blue" android:title="BLUE"></item>
<item android:id="@+id/gray" android:title="GRAY"></item>
<item android:id="@+id/cyan" android:title="CYAN"></item>
<item android:id="@+id/yellow" android:title="YELLOW"></item>

因为这些台词,我想你会得到InflaterException..

于 2013-11-04T04:50:43.807 回答
1

您的活动布局中不能有项目标签 <item ... ></item>

于 2013-11-04T04:55:56.443 回答