Android:在apk中添加菜单资源会使其崩溃

时间:2015-10-27 05:53:19

标签: android ant

我正在尝试学习android,我使用基于ant的CLI创建,部署应用程序。

我的清单文件如下

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

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />    

<application android:label="@string/app_name" android:icon="@drawable/ic_launcher"  android:theme="@android:style/Theme.Holo">
    <activity android:name="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>

我通过创建res \ menu \ items.xml

添加了一个菜单xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/action_share"
    android:title="Share it"
    >
</item>
</menu> 

如果我现在使用ant debug编译,然后运行应用程序,应用程序崩溃。有什么想法吗?

以下是活动代码,它现在不使用Menu。

package com.example.frag;

import android.app.Activity;
import android.app.ActionBar;
import android.os.Bundle;

import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

//import android.support.v4.view.Menu;
//import android.support.v4.view.MenuInflater;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
    private ActionBar actionBar;
    private ViewPager viewPager;
    private TabPagerAdapter tabPagerAdapter;

    private String[] tabs = { "Mouse", "Keyboard"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        viewPager = (ViewPager) findViewById (R.id.pager);
        tabPagerAdapter = new TabPagerAdapter (getSupportFragmentManager());
        viewPager.setAdapter (tabPagerAdapter);

        actionBar = getActionBar();
        //actionBar.setHasOptionsMenu (true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }


        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             * on swipe select the respective tab
             * */
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) { }

            @Override
            public void onPageScrollStateChanged(int arg0) { }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) { }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

    /*@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate (R.menu.items, menu);
        return super.onCreateOptionsMenu (menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        return true;
    }*/
}

更新

我删除了菜单资源,然后尝试在values /中添加styles.xml,重新编译,启动并再次崩溃。

0 个答案:

没有答案