【问题标题】:Menu not showing in app应用程序中未显示菜单
【发布时间】:2015-03-05 06:41:47
【问题描述】:

由于某种原因,我的操作菜单在我的 Android Studio 应用程序中消失了。我正在学习如何创建一个 android 应用程序的教程,但我最终遇到了这个问题。

教程我正在使用 atm:http://www.raywenderlich.com/56109/make-first-android-app-part-2

这就是我的 main.xml 的样子:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Share item -->
    <item
        android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="Share"
        android:actionProviderClass= "android.widget.ShareActionProvider" />
</menu>

menu_main.xml:

<!-- Defines the menu item that will appear on the Action Bar in MainActivity -->
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Share item -->
    <item
        android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="Share"
        android:actionProviderClass= "android.widget.ShareActionProvider"
        tools:ignore="AppCompatResource" />
</menu>

我在这段代码中做错了什么?我没有收到任何错误。我几乎从教程中复制粘贴了这个。

我不认为它是我的 java 代码,但它仍然在这里,以防有人认为问题出在该代码中。 MainActivity.java:

package android.stefan.testappnieuw;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;


public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {

    TextView mainTextView;
    Button mainButton;
    EditText mainEditText;
    ListView mainListView;
    ArrayAdapter mArrayAdapter;
    ArrayList mNameList = new ArrayList();
    android.widget.ShareActionProvider mShareActionProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 1. Access the TextView defined in layout XML
        // and then set its text
        mainTextView = (TextView) findViewById(R.id.main_textview);
        mainTextView.setText("Set in Java!");
        // 2. Access the Button defined in layout XML
        // and listen for it here
        mainButton = (Button) findViewById(R.id.main_button);
        mainButton.setOnClickListener(this);
        // 3. Access the EditText defined in layout XML
        mainEditText = (EditText) findViewById(R.id.main_edittext);
        // 4. Access the ListView
        mainListView = (ListView) findViewById(R.id.main_listview);
        // 5. Set this activity to react to list items being pressed
        mainListView.setOnItemClickListener(this);

        // Create an ArrayAdapter for the ListView
        mArrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,
                mNameList);

        // Set the ListView to use the ArrayAdapter
        mainListView.setAdapter(mArrayAdapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu.
        // Adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        // Access the Share Item defined in menu XML
        MenuItem shareItem = menu.findItem(R.id.menu_item_share);

        // Access the object responsible for
        // putting together the sharing submenu
        if (shareItem != null) {
            mShareActionProvider = (android.widget.ShareActionProvider)shareItem.getActionProvider();
        }

        // Create an Intent to share your content
        setShareIntent();

        return true;
    }

    private void setShareIntent() {

        if (mShareActionProvider != null) {

            // create an Intent with the contents of the TextView
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Android Development");
            shareIntent.putExtra(Intent.EXTRA_TEXT, mainTextView.getText());

            // Make sure the provider knows
            // it should work with that Intent
            mShareActionProvider.setShareIntent(shareIntent);
        }
    }

    @Override
    public void onClick(View v) {
        // Take what was typed into the EditText
        // and use in TextView
        mainTextView.setText(mainEditText.getText().toString()
                + " Hallo");
        // Also add that value to the list shown in the ListView
        mNameList.add(mainEditText.getText().toString());
        mArrayAdapter.notifyDataSetChanged();
        // 6. The text you'd like to share has changed,
        // and you need to update
        setShareIntent();
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        // Log the item's position and contents
        // to the console in Debug
        Log.d("omg android", position + ": " + mNameList.get(position));
    }
}

我真的不知道出了什么问题我是 java/android 新手,出于某种原因,菜单栏出现在教程中,但不适合我。

感谢阅读/帮助!

【问题讨论】:

    标签: java android xml android-studio


    【解决方案1】:

    转到您的清单文件并添加或更改此应用程序属性,如果不可用查找类似的东西还有更多使用操作栏

    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" >
    

    【讨论】:

    • 这解决了它,但是为什么@style/AppTheme 在 Eclipse 上工作而不是在工作室上工作.. 无论如何谢谢
    【解决方案2】:
        <item
        android:id="@+id/menu_item_share"
        android:showAsAction="always"
        android:title="Share"
        android:actionProviderClass= "android.widget.ShareActionProvider"
        tools:ignore="AppCompatResource" />
    

    ifRoom 表示是否有可显示的空间。让它永远显示。

    【讨论】:

    • 我将两者都更改为“始终”,但这并没有改变任何东西,它仍然没有出现。你确定这是问题所在?
    • 是的,看起来这就是问题所在。您是否尝试按手机上的选项按钮(左侧(大多数手机)软/硬按钮)。你的分享按钮至少会出现吗?
    • 选项按钮应位于共享按钮旁边的菜单栏中。出于某种原因,nexus 5 的底部没有选项按钮,只有后退、主页和任务管理器按钮。所以这是不可能的
    【解决方案3】:
    1. select_patient xml 包含如下菜单:-

       <item
          android:id="@+id/action_aboutus"
          android:orderInCategory="100"
          android:title="@string/action_about_us"
          android:icon="@drawable/help_icon"
          app:showAsAction="always"
         /> 
      
    2. 在您的活动中扩展“ActionBarActivity”。

    3. 确保您在活动中也添加了以下代码行

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      
          SharedMenu.onCreateOptionMenu(menu, this, R.menu.select_patient);
      
          return true;
      }
      
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      
          if (SharedMenu.onOptionsItemSelected(item, this) == false) {
              // handle local menu items here or leave blank
          }
          return super.onOptionsItemSelected(item);
      
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-02
      • 2013-06-19
      • 2018-09-28
      • 2015-08-12
      • 2019-10-06
      • 2011-11-01
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多