选项菜单上的android开发

发布于 2024-12-22 13:33:48 字数 2771 浏览 6 评论 0原文

我是使用 PhoneGap 进行 Android 开发的新手,我尝试通过 http://developer.android.com/guide/topics/ui/menus.html,但它总是给出如下错误消息:

[2011-12-20 16:45:28 - HelloPhoneGap] W/ResourceType(23444):错误的 XML 块:标头大小 > 84 或总大小 0 大于数据大小 0 [2011-12-20 16:45:28 - HelloPhoneGap] C:..\workspace\HelloPhoneGap\res\menu\menu.xml:3: >错误:错误:找不到与给定名称匹配的资源(位于 '标题',其值>'@string/new_game')。 [2011-12-20 16:45:28 - HelloPhoneGap] C:..\workspace\HelloPhoneGap\res\menu\menu.xml:5: >错误:错误:找不到与给定名称匹配的资源(位于 ' title' 的值 >'@string/help')。

这是控制台代码:

package com.phonegap.helloworld;

import android.os.Bundle;
import com.phonegap.*;

public class App extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/jqm/index.htm");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.new_game:
        newGame();
        return true;
    case R.id.help:
        showHelp();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onKeyDown(int keyCode,KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_MENU) {
           return false;
       }else{
           return super.onKeyDown(keyCode, event);
       }

}

}

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/new_game"
      android:title="@string/new_game" />
    <item android:id="@+id/help"
      android:title="@string/help" />
</menu>

有人告诉我导致错误的原因吗?

多谢!


在 string.xml 中定义字符串后,错误消失了,但我遇到了如下新错误

KeyEvent cannot be resolved to a type   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 39 Java Problem
KeyEvent cannot be resolved to a variable   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 40 Java Problem
Menu cannot be resolved to a type   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 17 Java Problem
MenuInflater cannot be resolved to a type   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 18 Java Problem
MenuItem cannot be resolved to a type   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 24 Java Problem

I am new to Android development by using PhoneGap, I try to build an options menu through a tutorial from http://developer.android.com/guide/topics/ui/menus.html, but it always gave error messages like below:

[2011-12-20 16:45:28 - HelloPhoneGap] W/ResourceType(23444): Bad XML block: header size >84 or total size 0 is larger than data size 0
[2011-12-20 16:45:28 - HelloPhoneGap] C:..\workspace\HelloPhoneGap\res\menu\menu.xml:3: >error: Error: No resource found that matches the given name (at 'title' with value >'@string/new_game').
[2011-12-20 16:45:28 - HelloPhoneGap] C:..\workspace\HelloPhoneGap\res\menu\menu.xml:5: >error: Error: No resource found that matches the given name (at 'title' with value >'@string/help').

Here is console code:

package com.phonegap.helloworld;

import android.os.Bundle;
import com.phonegap.*;

public class App extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/jqm/index.htm");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.new_game:
        newGame();
        return true;
    case R.id.help:
        showHelp();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onKeyDown(int keyCode,KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_MENU) {
           return false;
       }else{
           return super.onKeyDown(keyCode, event);
       }

}

}

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/new_game"
      android:title="@string/new_game" />
    <item android:id="@+id/help"
      android:title="@string/help" />
</menu>

Did anybody tell what cause the errors?

Thanks a lot!


After define string in string.xml, the error gone away but I've meet new errors like below

KeyEvent cannot be resolved to a type   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 39 Java Problem
KeyEvent cannot be resolved to a variable   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 40 Java Problem
Menu cannot be resolved to a type   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 17 Java Problem
MenuInflater cannot be resolved to a type   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 18 Java Problem
MenuItem cannot be resolved to a type   App.java    /HelloPhoneGap/src/com/phonegap/helloworld  line 24 Java Problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒夜笙歌凉 2024-12-29 13:33:48

您必须在参考值中定义字符串 new_game

<resources>
<string name="new_game">Your String</string>
 <string name="help">Your String</string>
</resources>

您可以创建值 xml 文件-

如果您使用的是 eclipse,则右键单击该项目并选择其他,然后选择 android xml 文件,然后在给定窗口中选择资源类型作为值,

you must have define Strings new_game in values like

<resources>
<string name="new_game">Your String</string>
 <string name="help">Your String</string>
</resources>

refer this

You can create values xml file -If you are using eclipse then right click on the project and select other and there select android xml file,then given window select resource type as value,

狂之美人 2024-12-29 13:33:48

似乎您缺少 /res/values/strings.xml
您的文本应该有字符串标签,例如:

<string name="help">Help</string>
<string name="new_game">New game</string>

Seems like you are missing your /res/values/strings.xml
There should be the string tags for your texts like:

<string name="help">Help</string>
<string name="new_game">New game</string>
浮生未歇 2024-12-29 13:33:48

看起来您在字符串 new_gamehelp 中的 strings.xml 文件中没有或有一些错误

It looks like you don't have or have some errors in strings.xml file in strings new_game and help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文