Amrmsmb
Amrmsmb

Reputation: 1

toolbar layout is undefined

I am learning how to use viewpager with actiontabs through a tutorial, and the main layout that hosts the Toolbar, Tablayout and viewpager is posted below in act_main.xml. at run time i receive the below posted logcat errors and it is about line 21 in the posted xml layout which contains

<android.support.v7.widget.Toolbar

how to fix it

main activitiy class:

public class MainActivity extends AppCompatActivity {

private final String TAG = this.getClass().getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w(TAG, "onCreate()");

    setContentView(R.layout.act_main);
}

act_main.xml

<RelativeLayout
android:id="@+id/main_layout"
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=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

</RelativeLayout>

gradle:

    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.com.vpager_00"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
}

logcat:

FATAL EXCEPTION: main
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime: Process: com.example.com.vpager_00, PID: 23204
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.vpager_00/com.example.com.vpager_00.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class android.support.design.widget.TabLayout
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5938)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
02-08 13:59:35.031 23204-23204/com.example.com.vpager_00 E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class android.support.design.widget.TabLayout

Upvotes: 1

Views: 133

Answers (1)

David Passmore
David Passmore

Reputation: 6099

Add the Google Design Library import into your dependencies {}

compile 'com.android.support:design:23.1.1'

You will also need to add the v4 Support Library

compile 'com.android.support:support-v4:23.1.1'

to prevent a similar error with your <android.support.v4.view.ViewPager />


Your app.gradle's dependencies {} should look something like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
}

Upvotes: 4

Related Questions