在Kotlin中的自定义类中访问TextView

时间:2017-07-03 10:28:04

标签: android kotlin kotlin-android-extensions kotlin-extension

我是Android和Kotlin的新手,我遇到了一个我无法解决的问题。我有一个activity_main.xml文件,其中包含seekBarTextView。当我更改seekBar时,我想实时显示TextView中更改的值。 为了学习新东西并练习Kotlin,我创建了一个包含Seekbar.OnSeekBarChangeListener的新类,该类会对seekBar更改做出反应。但问题是,当我将TextView设置为新的seekBar值时,会导致findViewById NullPointException

FATAL EXCEPTION: main
Process: kotlindemo1.kristof.kotlin_1demo, PID: 24410 
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2090)
at kotlindemo1.kristof.kotlin_1demo.seekB._$_findCachedViewById(seekB.kt:0)
at kotlindemo1.kristof.kotlin_1demo.seekB.onProgressChanged(seekB.kt:15)
at android.widget.SeekBar.onProgressRefresh(SeekBar.java:93)

这是MainActivity类:

package kotlindemo1.kristof.kotlin_1demo

import android.app.Activity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*


open class MainActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    sB1.setOnSeekBarChangeListener(seekB())
}
}

处理SeekBar更改的类:

package kotlindemo1.kristof.kotlin_1demo

import android.app.Activity
import android.os.Bundle
import android.widget.SeekBar
import kotlinx.android.synthetic.main.activity_main.*

class seekB : SeekBar.OnSeekBarChangeListener, Activity(){
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}

override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
    if(fromUser) tW1.text = getString(R.string.max_gen_number, progress) //java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {
}

override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cL"
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="kotlindemo1.kristof.kotlin_1demo.MainActivity"
>

<SeekBar
    android:id="@+id/sB1"
    android:progress="100"
    android:max="200"
    android:layout_width="182dp"
    android:layout_height="21dp"
    android:layout_marginTop="60dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintLeft_creator="1"
    />

<TextView
    android:id="@+id/tW1"
    android:layout_width="139dp"
    android:layout_height="wrap_content"
    android:text=""
    android:textAlignment="center"
    android:layout_marginTop="20dp"
    app:layout_constraintTop_toBottomOf="@+id/sB1"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintLeft_creator="1" />

</android.support.constraint.ConstraintLayout>

在Java中它很容易,使用FindViewById和SetContentView方法来引用一个小部件,但是如果我们导入kotlinx.android.synthetic.main.activity_main。*而在Kotlin中我们不需要使用findViewById 。我试图从MainActivity传递上下文,但这没有帮助。

2 个答案:

答案 0 :(得分:1)

我最简单的方式为你做了。 我的Android Studio版本是3.0 Canary 5.如果你没有更新你的Android Studio,你可能会遇到异常。

<强> MainActivity.kt 在onCreate

testget(){
    console.log("testget");
    this.http.get('http://url/api/url').map(res=>res.json()).subscribe(data=>{
       this.items=data.stores;
       console.log(data.image);
    });   
}

<强> activity_main.xml中

val textView = findViewById<TextView>(R.id.tv)
    val seekBar = findViewById<SeekBar>(R.id.seekBar)

    seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
        override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {

            textView.text = p1.toString()

        }

        override fun onStartTrackingTouch(p0: SeekBar?) {
        }

        override fun onStopTrackingTouch(p0: SeekBar?) {
        }
    })

答案 1 :(得分:0)

我尝试了它的工作。我添加了对findB&#39的构造函数的TextView引用。

<强> MainActivity

类MainActivity:AppCompatActivity(){

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val textView = findViewById<TextView>(R.id.tv)
    val seekBar = findViewById<SeekBar>(R.id.seekBar)

    seekBar.setOnSeekBarChangeListener(seekB(textView))


}
}

seekB class

class seekB(val tv: TextView) : SeekBar.OnSeekBarChangeListener{

override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: 
Boolean) {
    if(fromUser) tv.text = progress.toString()
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {
}

override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
}