Xephyrik
Xephyrik

Reputation: 3

Menu xml file causing activity to immediately crash

I have been stuck on this for a few days now. Whenever I open ChannelActivity it errors once it reaches setContentView(R.layout.channel_activity) Other threads I've seen were resolved by fixing an issue inside of the menu xml or by moving the menu xml into a menu resource type directory.

I still get the same errors even if my menu class has nothing inside of it, aside from xmlns:android="http://schemas.android.com/apk/res/android" and i have doubly amde sure that my menu xml was correctly placed into a menu resource type directory.

Any help as to pointing out the cause of, or fixing the error would be greaty appreciated as I'm not sure what to do at this point.

FATAL EXCEPTION Error:

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)

Render Error:

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

channel_layout.xml:

<?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_menu.xml: (located in res/menu, with resource type set as menu)

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

ChannelActivity:

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

Upvotes: 0

Views: 220

Answers (4)

Hemant Parmar
Hemant Parmar

Reputation: 3976

You inflate wrong menu xml file in onCreateOptionsMenu()

change this:

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

To:

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

Happy coding!!

Upvotes: 0

global_warming
global_warming

Reputation: 843

Replace channel_activity with channel_layout and channel_toolbar with 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;
 }
}

and remove that line from channel_layout.xml file.

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

Upvotes: 0

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

You can't include menu in layout. So remove the following:

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

Upvotes: 2

Jackey kabra
Jackey kabra

Reputation: 199

Try this code I hope you will be not getting this error.
public class ChannelActivity extends 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;
  }
}

Upvotes: 0

Related Questions