Pergunta

I've had problems with my app and eclipse so I saved my code, deleting eclipse, and re-downloaded and extracted. Immediately after I added the XML file to res/menu, I got a windows message saying aapt.exe has stopped working; I'd been getting this message constantly before. I've researched it before so I know that if an XML file isn't written correctly, aapt.exe will keep crashing. Only thing is, I don't see the problem with the file.

createlgmenu.xml (used as a popup menu for a button event):

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

    <item
        android:id="@+id/Create_Food_Group"
        android:title="@string/Create_Food_Group"/>

</menu>

|

|

Other files:

I have practically no code in my mainactivity.java:

package com.example.groceryrunner;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void onCreateLGClick(View v) {
        final int id = v.getId();
        switch (id) {
        case R.id.CreateLG:
            //findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
            createLGPopup(v);
            break;
        /*case R.id.ListsButton:
            findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
            createLGMenu(v);
            break;*/
        }
    }

    public void createLGPopup(View v) {
        PopupMenu LGMenu = new PopupMenu(this, v);
        LGMenu.getMenuInflater().inflate(R.menu.createlgmenu, LGMenu.getMenu());
        LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                String choice = new String((String) item.getTitle());
                if (choice == "Create_List") {
                    //createListDialog();
                }
                else if (choice == "Create_Group") {
                    //createListDialog();
                }
                return false;
            }
        });
        LGMenu.show();
    }

}

Only one button so far in my activity_main.xml:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/CreateLG"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="37dp"
        android:layout_marginTop="21dp"
        android:text="+"
        android:textSize="40sp" />

</RelativeLayout>

In case you want to see my menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>
Foi útil?

Solução 2

I think I've found my solution.

I had copied the xml and changed the items originally myself. So I deleted it and created it through Eclipse (right click on the res/menu folder > New > Other > Android XML File > Menu). I no longer seem to be getting the appt.exe error. I then added the items using Eclipse instead of writing or copying them myself.

The only difference in the two files is that the old one ended each item with this:

 \>

And the one created by Eclipse uses this at the end of an item declaration:

></item>

As for Eclipse not recognizing my xml files, AdamM helped me find the solution to that problem as well: 1) I deleted the R import

2) Commented out the lines that were giving me errors in recognizing my xml files

3) Restarted Eclipse & Built All

Outras dicas

If you are using the support library and a library project, then you need a custom namespace for the "showAsAction" tag for older platform versions.

So change this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

To this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:custom="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        custom:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

Note that "custom" can be anything you want.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top