user2121 February 2016

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"
           

Answers


David Passmore February 2016

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

Post Status

Asked in February 2016
Viewed 1,161 times
Voted 10
Answered 1 times

Search




Leave an answer