[FIXED] I facing the problem in logcat Caused by : java.lang.reflect.InvocationTargetException

Issue

wehen debuging this app showing erorr in logcat and not running, what should i do ,how fix this erorr ?
this erorr in line for MainActivity class Caused by: java.lang.reflect.InvocationTargetException

Line Number 16 is:

setContentView (R.layout.activity_main);

Logcat:

Caused by: android.view.InflateException: Binary XML file line #35 in com.example.myapplication:layout/activity_main: Binary XML file line #35 in com.example.myapplication:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar
     Caused by: android.view.InflateException: Binary XML file line #35 in com.example.myapplication:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:864)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1016)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:971)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1133)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1094)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1136)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1094)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:692)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:536)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:720)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:196)
        at com.example.myapplication.MainActivity.onCreate(MainActivity.java:16)
        at android.app.Activity.performCreate(Activity.java:8085)
        at android.app.Activity.performCreate(Activity.java:8073)
        

this is Xml code:

<androidx.constraintlayout.widget.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:id="@+id/main_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="end">

 <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/teal_200"
        app:headerLayout="@layout/header_navigator"
        app:menu="@menu/nav_menu">
    </com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/teal_700"
            android:minHeight="@attr/actionBarSize"
            app:navigationIcon="@drawable/ic_baseline_menu_24"
            app:title="Navigator Drawer">
        </androidx.appcompat.widget.Toolbar>

    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

bulid.gradle.(app):

    plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 31
    buildToolsVersion '30.0.3'

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 27
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"    
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.0-alpha01'
    implementation 'com.google.android.material:material:1.7.0-alpha01'
    implementation 'androidx.constraintlayout:constraintlayout:2.2.0-alpha01'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4-alpha01'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-alpha01'
}

MainActivity.java file :

    package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.View;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {
    MainActivity mainActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById (R.id.toolbar);
        DrawerLayout drawerLayout = (DrawerLayout) findViewById (R.id.drawer_view);
        NavigationView navigationView = (NavigationView) findViewById (R.id.nav_view); 
    }    
}

Solution

Can you post the code of these, the problem iis inside one of them :

        android:background="@color/teal_700"
        android:minHeight="@attr/actionBarSize"
        app:navigationIcon="@drawable/ic_baseline_menu_24"

Answered By – mokhtar halim

Answer Checked By – Mildred Charles (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published