android 中使用 Kotlin 的文本视图内容未出现在 phone 屏幕上
Text view contents not appearing on the phone screen in android using Kotlin
我无法在屏幕上显示文本视图及其内容。该应用程序不会崩溃,但调试器控制台会显示此错误。说明有以下原因。
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 之外。
感谢任何帮助。
尝试以下代码希望这会有所帮助:
<?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>
这个问题的解决方法是在onCreate
或OnCreateview
中不生成视图。如果我们查看片段的生命周期,我们会发现还有另一种方法onViewcreated
。将值分配给文本视图文本的静态代码必须放在 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 的支持。这对我有用。
我无法在屏幕上显示文本视图及其内容。该应用程序不会崩溃,但调试器控制台会显示此错误。说明有以下原因。
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 之外。
感谢任何帮助。
尝试以下代码希望这会有所帮助:
<?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>
这个问题的解决方法是在onCreate
或OnCreateview
中不生成视图。如果我们查看片段的生命周期,我们会发现还有另一种方法onViewcreated
。将值分配给文本视图文本的静态代码必须放在 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 的支持。这对我有用。