菜单xml文件导致活动立即崩溃

时间:2017-11-30 04:48:26

标签: android xml android-fragments

我已经被困在这几天了。每当我打开ChannelActivity时,一旦达到xmlns:android="http://schemas.android.com/apk/res/android"就会出错 我看到的其他线程通过修复菜单xml中的问题或将菜单xml移动到菜单资源类型目录中来解决。

即使我的菜单类里面没有任何内容,我仍然会得到相同的错误,除了11-29 21:43:36.610 15965-15965/edu.illinois.finalproject E/AndroidRuntime: FATAL EXCEPTION: main Process: edu.illinois.finalproject, PID: 15965 java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.illinois.finalproject/edu.illinois.finalproject.ChannelActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #2: Error inflating class menu at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6688) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #2: Error inflating class menu Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class menu Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.menu" on path: DexPathList[[zip file "/data/app/edu.illinois.finalproject-2/base.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_dependencies_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_0_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_1_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_2_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_3_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_4_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_5_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_6_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_7_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_8_apk.apk", zip file "/data/app/edu.illinois.finalproject-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/edu.illinois.finalproject-2/lib/arm64, /system/lib64, /vendor/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:380) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at android.view.LayoutInflater.createView(LayoutInflater.java:616) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:707) at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:724) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:792) at android.view.LayoutInflater.parseInclude(LayoutInflater.java:976) at android.view.LayoutInflater.rInflate(LayoutInflater.java:861) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) at android.view.LayoutInflater.inflate(LayoutInflater.java:525) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:378) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at edu.illinois.finalproject.ChannelActivity.onCreate(ChannelActivity.java:45) at android.app.Activity.performCreate(Activity.java:6912) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 11-29 21:43:36.610 15965-15965/edu.illinois.finalproject E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6688) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 之外我还有双重确定我的菜单xml已正确放入菜单资源类型目录。

任何有关指出原因或修正错误的帮助都会非常感激,因为我不知道此时该怎么做。

致命异常错误:

The following classes could not be found:
 - menu
 - item

渲染错误:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="edu.illinois.finalproject.ChannelActivity">

    <include layout="@menu/channel_toolbar"
             android:id="@+id/channelToolbar"/>
</android.support.constraint.ConstraintLayout>

channel_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/create_channel"
          android:icon="@drawable/ic_create"
          android:title="@string/create"
          app:showAsAction="always"/>

</menu>

channel_menu.xml :(位于res /菜单中,资源类型设置为菜单)

public class ChannelActivity extends AppCompatActivity {

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.channel_activity);   //<--------------- ERROR
  }

  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.channel_toolbar, menu);

    return true;
  }
}

ChannelActivity:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
npm install -g npm-windows-upgrade
npm-windows-upgrade

4 个答案:

答案 0 :(得分:2)

您无法在布局中添加菜单。因此,请删除以下内容:

<include layout="@menu/channel_toolbar"
             android:id="@+id/channelToolbar"/>

答案 1 :(得分:0)

channel_activity替换为channel_layout,将channel_toolbar替换为channel_menu

public class ChannelActivity extends AppCompatActivity {

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.channel_layout);   
 }

 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.channel_menu, menu);

  return true;
 }
}

并从channel_layout.xml文件中删除该行。

    <include layout="@menu/channel_toolbar"  
         android:id="@+id/channelToolbar"/>

答案 2 :(得分:0)

尝试此代码我希望您不会收到此错误     公共类ChannelActivity扩展了AppCompatActivity {

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.channel_layout);   //<--------------- ERROR
  }

  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.channel_menu, menu);

    return true;
  }
}

答案 3 :(得分:0)

您在onCreateOptionsMenu()

中夸大了错误的菜单xml文件

改变这个:

getMenuInflater().inflate(R.menu.channel_toolbar, menu);

要:

getMenuInflater().inflate(R.menu.channel_menu, menu);

快乐的编码!!