【问题标题】:Action button on 1 activity?1个活动的操作按钮?
【发布时间】:2014-02-22 22:52:11
【问题描述】:

我正在为包含片段的应用程序使用导航抽屉。我也在使用操作按钮。但是当我使用抽屉更改活动时,操作按钮在所有活动上。我只希望它在其中一个片段上。这是我的代码

package com.colourity.snatsh;

import com.colourity.snatsh.R;
import java.util.ArrayList;
import com.colourity.snatsh.adapter.NavDrawerListAdapter;
import com.colourity.snatsh.model.NavDrawerItem;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


public class HomeFragment extends Fragment {

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

这些都是错误。

The method onCreateOptionsMenu(Menu) of type HomeFragment must override or implement a supertype method HomeFragment.java
The method getMenuInflater() is undefined for the type HomeFragment HomeFragment.java /Snatsh/src/com/colourity/snatsh
The method onCreateOptionsMenu(Menu, MenuInflater) in the type Fragment is not applicable for the arguments (Menu)
当我在 MainActivity 中执行此操作时,它工作正常,但是当我将其移至我的 HomeFragment 时,错误显示。

【问题讨论】:

  • The method onCreateOptionsMenu(Menu, MenuInflater) in the type Fragment is not applicable for the arguments (Menu)不够清楚吗?这是一个提示:您需要像错误所说的那样更改方法签名。
  • 然后我会先阅读一些 Java 书籍,然后再深入 Android 开发。基本上,FragmentActivity 有一个onCreateOptionsMenu(),但它们有不同的签名;一个需要ManuInflater,另一个只需要Menu

标签: java android xml android-fragments


【解决方案1】:

但是当我将它移到我的 HomeFragment 时,会显示错误。

您需要确保您使用的是与Fragment 类对应的onCreateoptionsMenu() 签名。您可以在 Fragment 中删除此方法,然后开始输入其名称,然后使用您的 IDE 自动完成功能来使用正确的方法签名。

【讨论】:

  • 我是新手,所以任何帮助都会很棒。所以我会把它添加到我的HomeFragment? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
  • 没有。您不能在FragmentonCreate() 中扩展布局;您为此使用onCreateView()。您发布的方法是ActivityonCreate()FragmentonCreate()的签名是public void onCreate (Bundle savedInstanceState);它是public
  • 那我该如何开始呢?
  • 正如我所说,我是新手,我想出最好的学习方法是找到解决方案,因为这是我拥有的唯一片段,我正在努力最后期限。 @Emmanuel
  • 我不同意你所说的最好的学习方式是获得解决方案。如果你想看看Fragment的工作原理,新建一个空白项目,看看它是如何实现的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多