xml二进制文件应用程序因错误导致导航视图膨胀而崩溃

时间:2018-07-02 19:04:27

标签: android android-layout navigation-drawer android-inflate navigationview

在开始编辑自定义主题和颜色之前,我的应用程序运行良好。此后,应用程序崩溃了,我不知道为什么。我一开始尝试将其更改为默认主题,但不要工作。 我一直在寻找有关此错误的信息,但建议的解决方案对我而言不起作用,以查看是否可以帮助我:

这是错误的日志

07-02 18:34:14.313 19972-19972/com.pjcdev.huntingday E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pjcdev.huntingday, PID: 19972
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pjcdev.huntingday/com.pjcdev.huntingday.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
 Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.pjcdev.huntingday.MainActivity.onCreate(MainActivity.java:47)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.RuntimeException: Unexpected end of document
    at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:214)
    at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
    at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:248)
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:172)

首先,我的活动主要代码

package com.pjcdev.huntingday;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.facebook.login.LoginManager;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

  public class MainActivity extends AppCompatActivity
       implements NavigationView.OnNavigationItemSelectedListener, 
   GoogleApiClient.OnConnectionFailedListener {

   private GoogleApiClient mGoogleApiClient;
   private View headerview;
   private FirebaseAuth auth;
   private FirebaseUser user;
   private ImageView ivProfile;
   private TextView tvName,tvCity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Registrar nueva actividad", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //create a headerview to conect to header of left menu
    headerview=navigationView.getHeaderView(0);

    ivProfile=(ImageView)headerview.findViewById(R.id.imageProfile);
    tvName=(TextView)headerview.findViewById(R.id.fullName);
    tvCity=(TextView)headerview.findViewById(R.id.City);

    auth=FirebaseAuth.getInstance();

    //get user profile
    user=FirebaseAuth.getInstance().getCurrentUser();
    //get user profile

    if (user != null) {

        // Name, date birth,adress,email, and profile photo Url
        String name = user.getDisplayName();
        String email = user.getEmail();
        Uri photoUrl = user.getPhotoUrl();

        // The user's ID, unique to the Firebase project. Do NOT use this value to
        // authenticate with your backend server, if you have one. Use
        // FirebaseUser.getToken() instead.
        String uid = user.getUid();

        Glide.with(this).load(photoUrl).into(ivProfile);
        tvName.setText(name);
    }

    //handle gso objet to logout
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();




}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.addDogs) {

    } else if (id == R.id.addWeapons) {

    } else if (id == R.id.newActivity) {

    } else if (id == R.id.History) {

    } else if (id == R.id.editProfile) {

    } else if (id == R.id.about) {

    }else if (id == R.id.Logout) {
        signOut();
        goLoginScreen();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = auth.getCurrentUser();
    if(currentUser==null) {
        goLoginScreen();
    }
}

//method to go LoginScreen
private void goLoginScreen() {
    Intent intent=new Intent(this,LoginScreen.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

//method to log out Google,Facebook & Firebase
private void signOut() {
    //signout google
    if(mGoogleApiClient!=null) {
        if (mGoogleApiClient.isConnected()) {

            Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.d("LOGOUT", "Logout correcto");
                        goLoginScreen();
                    } else {
                        Toast.makeText(getApplicationContext(), "Error al hacer logout", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            Toast.makeText(getApplicationContext(), "no estas conectado", Toast.LENGTH_SHORT).show();
        }
    }
    //signout facebook
    LoginManager.getInstance().logOut();
    //signout firebase
    auth.signOut();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_huntingday"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_huntingday_round"
    android:supportsRtl="true"
    android:theme="@style/Base.Theme.AppCompat">
    <activity android:name=".LoginScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme"></activity>
</application>

现在,这是我的activity_main.xml文件的代码

  <?xml version="1.0" encoding="utf-8"?>
  <android.support.v4.widget.DrawerLayout 
   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:id="@+id/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true"
     tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"/>

 </android.support.v4.widget.DrawerLayout>

这是nav_header xml的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@dimen/nav_header_height"
   android:background="@drawable/side_nav_bar"
   android:gravity="bottom"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageProfile"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:contentDescription="@string/nav_header_desc"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:id="@+id/fullName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="@string/nav_header_title"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/City"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/nav_header_subtitle" />

最后,我的礼物

  apply plugin: 'com.android.application'

 android {
    compileSdkVersion 27
    defaultConfig {
       applicationId "com.pjcdev.huntingday"
       minSdkVersion 21
       targetSdkVersion 27
       versionCode 1
       versionName "1.0"
       testInstrumentationRunner 
  "android.support.test.runner.AndroidJUnitRunner"
     }
     buildTypes {
          release {
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
      }
   }

}

  dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:27.1.1'
   implementation 'com.android.support.constraint:constraint-layout:1.1.2'
   implementation 'com.android.support:design:27.1.1'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
      androidTestImplementation 'com.android.support.test.espresso:espresso- 
  core:3.0.2'
    implementation 'com.google.android.gms:play-services-auth:15.0.1'
   implementation 'com.google.firebase:firebase-core:15.0.0'
   implementation 'com.google.firebase:firebase-auth:15.0.0'
   implementation 'com.github.bumptech.glide:glide:3.7.0'
   implementation 'com.facebook.android:facebook-login:[4,5)' 
 }
 apply plugin: 'com.google.gms.google-services'

谢谢!

1 个答案:

答案 0 :(得分:0)

最后,由于我的朋友@Irampazzo,我发现了我的错误。 我的menu / activity_main_drawer.xml中缺少该行

<item android:title="">

打开第二个菜单及其崩溃的原因

感谢大家的帮助!