工具栏布局未定义

时间:2016-02-08 13:08:33

标签: android android-fragments android-viewpager android-toolbar

我正在学习如何通过教程使用viewpager和actiontabs,托管工具栏,Tablayout和viewpager的主要布局是 发布在act_main.xml下面。在运行时我收到以下发布的logcat错误,它在发布的xml布局中的第21行包含

<android.support.v7.widget.Toolbar

如何解决它

主要活动课程

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

1 个答案:

答案 0 :(得分:4)

将Google Design Library导入添加到dependencies {}

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

您还需要添加v4支持库

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

以防止您的<android.support.v4.view.ViewPager />

出现类似错误

您的app.gradle dependencies {}应该是这样的:

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