【问题标题】:Implementing Floating Action Button on a Fragment Class在片段类上实现浮动操作按钮
【发布时间】:2017-02-16 10:25:42
【问题描述】:

您好,我想在 Fragment 类上添加浮动操作按钮,但是当我添加实现 AppCompatActivity 时,我收到此错误:

“android.support.v4.app.Fragment”中的“getLoaderManager()” 与 'android.app.Activity' 中的 'getLoaderManager()' 冲突;试图使用不兼容的返回类型”

public class home extends Fragment implements AppCompatActivity{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.home, container, false);
    return rootView;

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}

}

【问题讨论】:

  • remove implements AppCompatActivity 这是一个你没有扩展活动的片段

标签: android android-fragments floating-action-button appcompatactivity


【解决方案1】:

只需从片段中删除“implements AppCompatActivity”,并将“extends AppCompatActivity”添加到包含该片段的活动中。

【讨论】:

    【解决方案2】:

    我知道这个问题很老,但对于其他正在寻找这个解决方案的人来说, 只需在最后一个中写返回视图,就在 onCreateView 的右括号之前。

    【讨论】:

    • (这篇文章似乎没有为问题提供quality answer。请编辑您的答案,或者将其作为对问题的评论发布)。
    【解决方案3】:

    您在实际使用工厂之前返回根视图。

    View rootView = inflater.inflate(R.layout.home, container, false);
    
    
        FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    
        return rootView;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-25
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      相关资源
      最近更新 更多