片段错误android

时间:2018-07-11 17:54:12

标签: java android android-fragments

我的代码中出现了很多异常。它在我的代码中指向的唯一参考是在第24行,即setContentView(R.layout.activity_main)。我不明白为什么:

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) 

我的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();
    }
}

我的片段类:

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

主要活动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"
    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>

这是片段的xml文件:

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

我在这里尝试了一些解决方案,但是没有一个起作用。我目前对android开发人员还是陌生的,所以我想在研究此问题时将其发布在这里。

2 个答案:

答案 0 :(得分:1)

在HeaderNav中,函数的名称不正确。应该是:

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

答案 1 :(得分:1)

我只是复制了所有代码并设置了一个小示例应用程序。我更改了方法onViewCreated()的名称,因为使用您代码中的方法名称加上@Override注释,该应用无法编译(有一个具有该名称的方法,但是它没有相同的参数) 。使用正确的方法名称onCreateView()@Override,一切正常。

(好的,我必须将所有可绘制对象交换为颜色,并且我也跳过了字体-这可能是另一个错误源)。

使用错误的方法名称而没有@Override时,代码会编译,因为您当然可以使用该名称的方法-这在语法上是正确的Java代码。

但是现在我得到和您一样的错误。在堆栈跟踪中的某处甚至说

  

原因:java.lang.IllegalStateException:com.example.egi.mavisme.HeaderNav片段未创建视图。

发生这种情况是因为您没有实现onCreateView()。因此,运行时无法确定Fragment的外观,并且没有View添加到MainActivity的布局中,这反过来导致{{1 }},您会在Logcat的崩溃日志开头看到它。

因此,您应该按以下方式更改方法:

InflateException