Android Studio 选项卡和 ViewPager 渲染



我一直在开发一个简单的应用程序,它涉及Fragment。我在XML文件中使用了viewpager和TabLayout。显示一个错误:呈现问题以及 2 个警告:i( 缺少样式 ii( 无法实例化一个或多个类。 在"渲染问题"中,显示以下错误: "无法在当前主题中找到样式'选项卡样式'"。

在缺少样式中: "缺少样式。是否为此布局选择了正确的主题? 使用布局上方的"主题"组合框选择其他布局,或修复主题样式引用。

在 无法实例化一个或多个类: "无法实例化以下类: - android.support.design.widget.TabLayout (Open Class, Show Exception, Clear Cache( - android.support.v4.view.ViewPager (Open Class, Show Exception, Clear Cache( 提示: 在自定义视图中使用 View.isInEditMode(( 可以跳过代码或在 IDE 中显示示例数据。 如果这是一个意外的错误,您也可以尝试构建项目,然后手动刷新布局。 异常详细信息 java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener " 在此处输入图像描述

清单.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.utkarsh.kannadaseekhae">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" 
/>
</intent-filter>
</activity>
</application>

activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_color"
android:orientation="vertical"
tools:context=".MainActivity">

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/CategoryTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="48dp" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

款式.xml:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" 
parent="Base.Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primary_color</item>
<item name="colorPrimaryDark">@color/primary_dark_color</item>
<item name="actionBarStyle">@style/KannadaAppBarStyle</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<!-- App bar style -->
<style name="KannadaAppBarStyle" parent="@android:style/Widget.DeviceDefault.Light.ActionBar.Solid.Inverse">
<!-- Remove the shadow below the app bar -->
<item name="elevation">0dp</item>
</style>
<!-- Style for a tab that displays a category name -->
<style name="CategoryTab" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@android:color/white</item>
<item name="tabSelectedTextColor">@android:color/white</item>
<item 
name="tabTextAppearance">@style/CategoryTabTextAppearance</item>
</style>
<!-- Text appearance style for a category tab -->
<style name="CategoryTabTextAppearance" 
parent="TextAppearance.Design.Tab">
<item name="android:textColor">#A8A19E</item>
</style>
</resources>

Gradle 构建:

apply plugin: 'com.android.application'

安卓 { 编译SdkVersion 28 默认配置 { applicationId "com.example.utkarsh.kannadaseekhae" minSdkVersion 15 目标Sdk版本28 版本代码 1 版本名称"1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } 构建类型 { 释放 { 缩小启用假 proguardFiles getDefaultProguardFile('proguard-android.txt'(, "proguard-rules.pro" } } }

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint- 
layout:1.1.2'
implementation 'com.android.support:support-v4:28.0.0-rc01'
implementation 'com.android.support:design:28.0.+'
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'
}

主活动.java:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml 
layout file
setContentView(R.layout.activity_main);
// Find the view pager that will allow the user to swipe between 
fragments
ViewPager viewPager = findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on 
each page
CategoryAdapter adapter = new CategoryAdapter(this, 
getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
// Find the tab layout that shows the tabs
TabLayout tabLayout = findViewById(R.id.tabs);
// Connect the tab layout with the view pager. This will
//   1. Update the tab layout when the view pager is swiped
//   2. Update the view pager when a tab is selected
//   3. Set the tab layout's tab names with the view pager's 
adapter's titles
//      by calling onPageTitle()
tabLayout.setupWithViewPager(viewPager);
}
}

只需将支持库版本更改为alpha1

它应该看起来

implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support:design:28.0.0-alpha1'

最新更新