【问题标题】:Snackbar InflateExceptionSnackbar InflateException
【发布时间】:2019-03-14 11:54:59
【问题描述】:

我一直在苦苦挣扎,因为工作室抱怨 Dialog 已被弃用, 所以我再次阅读文档,似乎Toast 也已被弃用。 所以我尝试从Toast迁移到Snackbar,但目前失败了......我 我加了

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'

到我的 gradle 依赖项以访问文档所需的类。

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

<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>

然后,在 MainActivity 的 onCreate 中,我检索到该 CoordinatorLayout 的引用

 coordinatorView = findViewById(R.id.myCoordinatorLayout);

稍后在onOptionsItemSelected,在我正在调试的菜单点之一中:

Snackbar mySnackbar = Snackbar.make(coordinatorView, "Launched import", Snackbar.LENGTH_LONG);
mySnackbar.show();

只要这 2 行出现,应用程序就会崩溃:

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 行......如上所示,它没有按钮??中心视图(shellview)由显示实际的可交换片段填充 功能上,它以一个简单的 TextView+EditView 启动,所以 Startupfragment 也不是罪魁祸首,那里没有按钮....

欢迎任何关于如何解决这个问题的想法!

按照要求,我添加了整个 onOptionsItemSelected

@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;
}

【问题讨论】:

  • 您是否向您的snackbar 添加任何操作?
  • 不,目前我只是用 Snackbar 替换 Toast....

标签: android android-snackbar


【解决方案1】:

二进制 XML 文件第 41 行:膨胀类时出错
android.widget.Button

它来自Button 类。 但是

添加:

implementation 'com.android.support:appcompat-v7:28.0.0'

到你的build.gradle 依赖然后试试。

你不需要:

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

更新:检查您添加的代码后,您似乎实际上是在尝试在 LinearLayout 中显示 Fragments 和 shellView id。将其更改为 FrameLayout 然后我希望它应该可以工作。

【讨论】:

  • 将实现更改为 appcompat,以为我只包含真正需要的东西......但是,重建,重新运行,同样的错误?仍在挂断按钮...
  • Studio 不喜欢这种类型的演员表,当我进行代码分析时将其删除,但好吧,添加了它,并没有改变任何东西:(
  • 刚刚检查了您的代码。有趣的是,您的布局中甚至 java 端都没有 Button。但是,您正试图在LinearLayout 中显示片段。 -&gt; shellView。改成FrameLayout再试试。
  • FrameLayout 作为片段的占位符是否更好?改得很好,其他一切正常,但小吃店仍然会产生同样的崩溃......
  • FrameLayout 在这种情况下是最好的选择。如何评论它然后检查它是否在没有SnakeBar 的情况下工作? SnakeBar 中的导入可能存在一些问题?您可以通过评论SnakeBar 轻松检查是否会导致问题。另外,还要检查导入,这非常罕见和奇怪!
猜你喜欢
  • 2017-11-28
  • 2015-10-19
  • 1970-01-01
  • 2017-11-16
  • 2012-06-10
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多