开发者

creating an options menu with quit application function

I'm writing an application for android and I would like to insert an options menu, that would have only one function and that is to quit an application.

My options_menu.xml looks like this (I created a new folder under res called menu - just like the instructions said):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:i开发者_Python百科d="@+id/quit"
          android:icon="@drawable/ic_quit"
          android:title="@string/quit" />          
</menu>

Than I added the following code to my java class file:

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

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

I also defined the @string/quit in values - strings.xml file, but it still says that there is an error:

C:\Users\Domen\workspace\Lovne Dobe1.1\res\menu\options_menu.xml:3: error: Error: No resource found that matches the given name (at 'title' with value '@string/quit')

So if anybody can help I would be very grateful.


your strings.xml file should look like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="quit">Quit</string>
    ...
</resources>

Verify it is so.


My string.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LovneDobe!</string>
<string name="app_name">Lovne dobe</string>
<string name="quit">Quit</string>
<string-array name="zivali_array">
    <item>Alpski svizec</item>
    <item>Damjak</item>
    ...
</string-array>

I fixed the string like you said but it's still reporting error. Is says that is an Unparsed aapt error(s)! Check the console for output. Lovne Dobe1.1 Unknown Android Packaging Problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜