使用Kotlin在Android手机屏幕上未显示文本视图内容

时间:2018-07-26 09:45:26

标签: android kotlin bottomnavigationview inflate-exception layout-animation

我无法在屏幕上显示文本视图及其内容。该应用程序不会崩溃,但调试器控制台会显示此错误。显示原因如下。

Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.RuntimeException: Unknown layout animation name: accelerateInterpolator

此外,错误指向MainActivity中的setcontentview。我尝试解决issue的问题,但没有任何进展。实际上,我的版本更高,但是错误仍然存​​在。

build.gradle(Module:app)的内容是

//noinspection GradleCompatible
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.vishwa.imaginators"
    minSdkVersion 16
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        debuggable false
        shrinkResources false
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        zipAlignEnabled true
    }
  }
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
//noinspection GradleCompatible
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:27.1.1'
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'
//noinspection GradleCompatible
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.squareup.okhttp:okhttp:2.4.0'
implementation 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'

//retrofit
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.0'
//    implementation "com.squareup.retrofit2:converter-moshi:2.0.0"
//rxjava
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
//download sdk
implementation 'com.facebook.android:facebook-android-sdk:4.34.0'
implementation 'com.facebook.android:facebook-login:[4,5)'
implementation 'com.squareup.picasso:picasso:2.3.2'
}

activity_main.xml如下所示。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
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="@mipmap/bg"
tools:context=".MainActivity">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/nav_item_colors"
    app:itemTextColor="@color/nav_item_colors"
    app:menu="@menu/bottom_navigation">

</android.support.design.widget.BottomNavigationView>

<FrameLayout
    android:id="@+id/main_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_nav"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_Main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
  </FrameLayout>

</RelativeLayout>

我将内容添加到此片段的代码片段。

bottomNavigationView.setOnNavigationItemSelectedListener { item ->
        when(item.itemId){
            R.id.nav_progress->
                progressData()
            R.id.nav_discover->
                discoverData(arrItems)
            R.id.nav_concept->
                fetchData(arrItems, indices)
           }
        true
    }

    private fun progressData() {

    var titles = listOf("first","second", "third","fourth")
    val text1=findViewById<TextView>(R.id.quadrant1_textview1)
    val text2=findViewById<TextView>(R.id.quadrant2_textview1)
    val text3=findViewById<TextView>(R.id.quadrant3_textview1)
    val text4=findViewById<TextView>(R.id.quadrant4_textview1)
    text1?.text=titles[0]
    text2?.text=titles[1]
    text3?.text=titles[2]
    text4?.text=titles[3]
    }

ProgressData()函数在oncreate之外。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试以下代码,希望对您有所帮助:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    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">


    <FrameLayout
        android:id="@+id/main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_nav"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_Main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </android.support.v7.widget.RecyclerView>
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@color/nav_item_colors"
        app:itemTextColor="@color/nav_item_colors"
        app:menu="@menu/bottom_navigation">

    </android.support.design.widget.BottomNavigationView>


</RelativeLayout>

答案 1 :(得分:0)

此问题的解决方案是在onCreateOnCreateview中不生成视图。如果看到片段的生命周期,则会发现还有另一种方法onViewcreated。将值分配给textview的文本的静态代码必须放在Progress片段的onViewcreated方法中。这样就解决了问题。

代码段:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    progress_progressbar2?.setProgress(12)
    progress_progressbar2?.max=15
    val currentProgress = progress_progressbar2?.progress ?: 0
    if (currentProgress == 0){
        started_image?.visibility = View.VISIBLE
        progress_scrollview?.invalidate()
    } else {
        started_image?.visibility = View.GONE
        progress_scrollview?.visibility = View.VISIBLE
        navigation_header_container?.setImageResource(R.drawable.header_pink)
    }
}

感谢YoLo的支持。这对我有用。