Reputation: 351
I am getting a lot of exceptions in my code. The only reference it points to in my code is at line 24 which is setContentView(R.layout.activity_main). I do not understand why :
07-12 01:36:51.433 3796-3796/com.example.egi.mavisme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.egi.mavisme, PID: 3796
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.egi.mavisme/com.example.egi.mavisme.MainActivity}: android.view.InflateException: Binary XML file line #49: Binary XML file line #49: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2543)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1465)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5683)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
Caused by: android.view.InflateException: Binary XML file line #49: Binary XML file line #49: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:467)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.egi.mavisme.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1465)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5683)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
Caused by: android.view.InflateException: Binary XML file line #49: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:467)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.egi.mavisme.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1465)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5683)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
Caused by: java.lang.IllegalStateException: Fragment com.example.egi.mavisme.HeaderNav did not create a view.
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3776)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:395)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:377)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:467)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.egi.mavisme.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1465)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5683)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
My MainActivity :
public class MainActivity extends AppCompatActivity {
private TextView mavisTxt;
private Button testBtn;
private Fragment navbar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initVariables();
}
private void initVariables() {
mavisTxt = (TextView)findViewById(R.id.mavisTxt);
testBtn = (Button)findViewById(R.id.testBtn);
navbar = new HeaderNav();
testBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hideNavbar();
}
});
}
private void hideNavbar() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.hide(navbar);
ft.commit();
}
}
My fragment class :
package com.example.egi.mavisme;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.widget.Button;
public class HeaderNav extends Fragment {
private static Button homeBtn, optionsBtn, connectionBtn, micBtn, aboutBtn;
public View onViewCreated(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.navbar, container, false);
homeBtn = (Button)view.findViewById(R.id.homeBtn);
optionsBtn = (Button)view.findViewById(R.id.optionsBtn);
connectionBtn = (Button)view.findViewById(R.id.micBtn);
micBtn = (Button)view.findViewById(R.id.homeBtn);
aboutBtn = (Button)view.findViewById(R.id.aboutBtn);
return view;
}
}
main activity xml layout:
<?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"
android:background="@drawable/background"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="239dp"
android:layout_height="227dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="mainicon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.271"
app:srcCompat="@drawable/main" />
<TextView
android:id="@+id/mavisTxt"
android:layout_width="237dp"
android:layout_height="177dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/titillium_light"
android:text="Talk you bastard."
android:textColor="#ffffff"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintVertical_bias="0.063" />
<fragment
android:id="@+id/navbar"
android:name="com.example.egi.mavisme.HeaderNav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/navbar" />
<Button
android:id="@+id/testBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
here is xml file for fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="false"
android:cropToPadding="false"
app:srcCompat="@drawable/navbar" />
<Button
android:id="@+id/homeBtn"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerInParent="@+id/imageView4"
android:layout_alignTop="@+id/imageView4"
android:layout_marginTop="6dp"
android:layout_marginStart="30dp"
android:background="@drawable/home_icon" />
<Button
android:id="@+id/optionsBtn"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerInParent="@+id/imageView4"
android:layout_alignTop="@+id/imageView4"
android:layout_marginTop="6dp"
android:layout_marginStart="100dp"
android:background="@drawable/options_icon" />
<Button
android:id="@+id/micBtn"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignTop="@+id/homeBtn"
android:layout_centerHorizontal="true"
android:background="@drawable/mic_icon" />
<Button
android:id="@+id/connectionBtn"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerInParent="@+id/imageView4"
android:layout_alignTop="@+id/imageView4"
android:layout_marginTop="6dp"
android:layout_marginStart="240dp"
android:background="@drawable/con_icon" />
<Button
android:id="@+id/aboutBtn"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerInParent="@+id/imageView4"
android:layout_alignTop="@+id/imageView4"
android:layout_marginTop="6dp"
android:layout_marginStart="310dp"
android:background="@drawable/about_icon" />
</RelativeLayout>
I have tried some solutions here in SO, but none of them worked. I am currently still new to android dev so I thought i'd post this here while I research this issue.
Upvotes: 1
Views: 111
Reputation: 13019
I just copied all your code and set up a small sample app. I changed the name of the method onViewCreated()
because with the method name from your code plus the @Override
annotation the app does not compile (there is a method with that name but it does not take the same parameters). With the correct method name onCreateView()
plus @Override
, everything works.
(OK, I had to swap all the drawables for colors and I also skipped the font - this may be another error source).
With the wrong method name and without @Override
, the code compiles because of course you may have a method by that name - this is syntactically correct Java code.
But now I get the same error like you do. Somewhere in the stacktrace it even says
Caused by: java.lang.IllegalStateException: Fragment com.example.egi.mavisme.HeaderNav did not create a view.
This happens because you do not implement onCreateView()
. Because of this, the runtime is unable to tell what your Fragment
should look like and there is no View
to be added to the layout of MainActivity
, which in turn causes the InflateException
which you see at the beginning of the crash log in Logcat.
So you should change your method as follows:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.navbar, container, false);
homeBtn = (Button)view.findViewById(R.id.homeBtn);
optionsBtn = (Button)view.findViewById(R.id.optionsBtn);
connectionBtn = (Button)view.findViewById(R.id.micBtn);
micBtn = (Button)view.findViewById(R.id.homeBtn);
aboutBtn = (Button)view.findViewById(R.id.aboutBtn);
return view;
}
Upvotes: 1
Reputation: 723
The name of your function is incorrect in HeaderNav. It should be:
public class HeaderNav extends Fragment {
private static Button homeBtn, optionsBtn, connectionBtn, micBtn, aboutBtn;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.navbar, container, false);
homeBtn = (Button)view.findViewById(R.id.homeBtn);
optionsBtn = (Button)view.findViewById(R.id.optionsBtn);
connectionBtn = (Button)view.findViewById(R.id.micBtn);
micBtn = (Button)view.findViewById(R.id.homeBtn);
aboutBtn = (Button)view.findViewById(R.id.aboutBtn);
return view;
}
}
Upvotes: 1