Android Snackbar充气异常

Android Snackbar充气异常,android,android-snackbar,Android,Android Snackbar,我一直在苦苦挣扎,因为工作室抱怨Dialog不受欢迎, 所以我又去看了一遍文档,似乎Toast也被弃用了。 所以我试着从Toast迁移到Snackbar,但现在失败了……我 我补充说 访问我的gradle依赖项,以从docu访问所需的类 我将原始的LinearLayout包装为CoordinatorLayout将主布局更改为 <android.support.design.widget.CoordinatorLayout android:id="@+id/myCoordinatorLa

我一直在苦苦挣扎,因为工作室抱怨
Dialog
不受欢迎, 所以我又去看了一遍文档,似乎
Toast
也被弃用了。 所以我试着从
Toast
迁移到
Snackbar
,但现在失败了……我 我补充说

访问我的gradle依赖项,以从docu访问所需的类

我将原始的
LinearLayout
包装为
CoordinatorLayout
将主布局更改为

<android.support.design.widget.CoordinatorLayout
  android:id="@+id/myCoordinatorLayout"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <FrameLayout
      android:layout_height="match_parent"
      android:layout_width="match_parent"
      android:orientation="vertical"
      android:id="@+id/shellView"
      android:padding="10dp">
   </FrameLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
稍后,在我正在调试的一个菜单点中的选项项Selected中:

Snackbar mySnackbar = Snackbar.make(coordinatorView, "Launched import", Snackbar.LENGTH_LONG);
mySnackbar.show();
一旦这两行输入,应用程序就会崩溃:

android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error         inflating class android.widget.Button
    Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class     android.widget.Button
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at         com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.support.design.widget.Snackbar.make(Snackbar.java:188)
        at com.nohkumado.nohfibu.MainActivity.onOptionsItemSelected(MainActivity.java:187)
        at android.app.Activity.onMenuItemSelected(Activity.java:3450)
它需要哪个xml文件?我的main.xml只有22行。。。如上所示,它没有按钮??centerview(shellview)由可交换片段填充,该片段显示实际的 功能上,它以一个简单的TextView+EditView启动,因此Startupfragment不可能也是罪魁祸首,那里没有按钮

因此,欢迎提出任何解决这个问题的想法

如所问,我添加整个OnOptions ItemSelected

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
FragmentManager fm = getFragmentManager();
Log.d(TAG,"got item clicked "+item.getItemId());

switch (item.getItemId())
    {
  case R.id.menuLsbook:
    Log.d(TAG,"starting up ls book frag");
    LsBookFragment lbook = new LsBookFragment().shell(shell);
    lbook.show(fm, getResources().getString(R.string.mlsbook));
    Log.d(TAG,"ls book running");
    break;
  case R.id.menuShowKpl:
    Log.d(TAG,"starting up kpl frag");

    EditorFrag editorFrag = (EditorFrag) fm.findFragmentByTag(KPLFRAG);
      if(editorFrag == null)
      {
        editorFrag = new EditKplFrag().shell(shell);
      }
      fm.beginTransaction().replace(R.id.shellView,editorFrag,KPLFRAG).commit();
    Log.d(TAG,"kpl running");
    break;
        case R.id.menuShowJrl:
            Log.d(TAG,"starting up jrl frag");

            editorFrag = (EditorFrag) fm.findFragmentByTag(JRLFRAG);
            if(editorFrag == null)
            {
                editorFrag = new EditJrlFrag().shell(shell);
            }
            fm.beginTransaction().replace(R.id.shellView,editorFrag,JRLFRAG).commit();
            Log.d(TAG,"jrl running");
            break;
  case R.id.menu_import:
    Log.d(TAG,"selecting a source for importation of a new book book frag");
    ImportBookFragment ibook = new ImportBookFragment().shell(shell);
    //ibook.show(fm, getResources().getString(R.string.mlsbook));
    Log.d(TAG,"import book running");
    Snackbar mySnackbar = Snackbar.make(coordinatorView, "Launched import", Snackbar.LENGTH_LONG);
    mySnackbar.show();

    break;

  case R.id.menu_show_shell:
    Log.d(TAG,"starting shell frag");
    ShellFragment shellfrag = (ShellFragment) fm.findFragmentByTag(SHELLFRAG);
    if (shellfrag == null)
    {
      //Log.d(TAG, "no shell frag");
      shellfrag = new ShellFragment();
      shellfrag.callback(this);
      fm.beginTransaction().add(R.id.shellView, shellfrag, SHELLFRAG).commit();
      //Log.d(TAG, "shellfrag added");
      if(shell != null) shellfrag.shell(shell);
    }//if (shellfrag == null)
    shell = shellfrag.shell();
    Log.d(TAG,"shell displaying");
    break;


  case R.id.menu_settings:
            Log.d(TAG, "case is settings, creating intent");
            //Intent intent = new Intent();
            //intent.setClassName(this, "com.nohkumado.nohfibu.SettingsActivity");
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            //startActivityForResult(i, RESULT_SETTINGS);
            Log.d(TAG, "fired intent");
            break;
        case R.id.mabout:
            AboutDialogFragment about = new AboutDialogFragment();
            about.setVersion(new VersionCommand(shell).execute());
            about.show(fm, getResources().getString(R.string.mabout));
            break;
  case R.id.menu_manual:
    about = new AboutDialogFragment();
    about.setVersion(new VersionCommand(shell).execute());
    about.show(fm, getResources().getString(R.string.empty));
    break;
    }

    return true;
}
二进制XML文件行#41:膨胀类时出错
android.widget.Button

它来自
按钮
但是

加:

到您的
build.gradle
依赖项,然后重试

您不需要:

implementation 'com.android.support:coordinatorlayout:28.0.0-alpha1'

更新:检查您添加的代码后,似乎您实际上是在尝试使用
shellView
id来显示
Fragment
s内部
LinearLayout
。将其更改为
FrameLayout
,我希望它会起作用。

您是否在
snackbar
中添加了任何操作,现在我只是想用一个Snackbar来代替Toast…将实现更改为appcompat,认为我只包含真正需要的东西。。。但是,重建,重新运行,同样的错误?仍然挂在按钮上…Studio不喜欢这种类型的强制转换,在我进行代码分析时将其删除,但添加它不会改变任何内容:(刚刚检查了您的代码。有趣的是,您的布局中甚至在java端都没有
按钮
。但是,您正在尝试在
线性布局中显示片段
->shellView
。将其更改为
框架布局
,然后再试一次。框架布局作为片段的占位符更好吗?一切都变了其他方法可以工作,但蛇形条仍会产生相同的崩溃….
FrameLayout
是这种情况下的最佳选择。对其进行注释,然后检查它是否在没有蛇形条的情况下工作如何?
蛇形条
?可能在导入方面存在一些问题?您可以通过注释蛇形条
来轻松检查是否存在c因为这个问题。还有,检查一下进口,这是非常罕见和奇怪的!
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
FragmentManager fm = getFragmentManager();
Log.d(TAG,"got item clicked "+item.getItemId());

switch (item.getItemId())
    {
  case R.id.menuLsbook:
    Log.d(TAG,"starting up ls book frag");
    LsBookFragment lbook = new LsBookFragment().shell(shell);
    lbook.show(fm, getResources().getString(R.string.mlsbook));
    Log.d(TAG,"ls book running");
    break;
  case R.id.menuShowKpl:
    Log.d(TAG,"starting up kpl frag");

    EditorFrag editorFrag = (EditorFrag) fm.findFragmentByTag(KPLFRAG);
      if(editorFrag == null)
      {
        editorFrag = new EditKplFrag().shell(shell);
      }
      fm.beginTransaction().replace(R.id.shellView,editorFrag,KPLFRAG).commit();
    Log.d(TAG,"kpl running");
    break;
        case R.id.menuShowJrl:
            Log.d(TAG,"starting up jrl frag");

            editorFrag = (EditorFrag) fm.findFragmentByTag(JRLFRAG);
            if(editorFrag == null)
            {
                editorFrag = new EditJrlFrag().shell(shell);
            }
            fm.beginTransaction().replace(R.id.shellView,editorFrag,JRLFRAG).commit();
            Log.d(TAG,"jrl running");
            break;
  case R.id.menu_import:
    Log.d(TAG,"selecting a source for importation of a new book book frag");
    ImportBookFragment ibook = new ImportBookFragment().shell(shell);
    //ibook.show(fm, getResources().getString(R.string.mlsbook));
    Log.d(TAG,"import book running");
    Snackbar mySnackbar = Snackbar.make(coordinatorView, "Launched import", Snackbar.LENGTH_LONG);
    mySnackbar.show();

    break;

  case R.id.menu_show_shell:
    Log.d(TAG,"starting shell frag");
    ShellFragment shellfrag = (ShellFragment) fm.findFragmentByTag(SHELLFRAG);
    if (shellfrag == null)
    {
      //Log.d(TAG, "no shell frag");
      shellfrag = new ShellFragment();
      shellfrag.callback(this);
      fm.beginTransaction().add(R.id.shellView, shellfrag, SHELLFRAG).commit();
      //Log.d(TAG, "shellfrag added");
      if(shell != null) shellfrag.shell(shell);
    }//if (shellfrag == null)
    shell = shellfrag.shell();
    Log.d(TAG,"shell displaying");
    break;


  case R.id.menu_settings:
            Log.d(TAG, "case is settings, creating intent");
            //Intent intent = new Intent();
            //intent.setClassName(this, "com.nohkumado.nohfibu.SettingsActivity");
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            //startActivityForResult(i, RESULT_SETTINGS);
            Log.d(TAG, "fired intent");
            break;
        case R.id.mabout:
            AboutDialogFragment about = new AboutDialogFragment();
            about.setVersion(new VersionCommand(shell).execute());
            about.show(fm, getResources().getString(R.string.mabout));
            break;
  case R.id.menu_manual:
    about = new AboutDialogFragment();
    about.setVersion(new VersionCommand(shell).execute());
    about.show(fm, getResources().getString(R.string.empty));
    break;
    }

    return true;
}
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:coordinatorlayout:28.0.0-alpha1'