2017-07-03 14 views
-3

私はAndroidとKotlinの初心者ですが、私は解決できないという問題に直面しています。私はactivity_main.xmlファイルにseekBarTextViewを含んでいます。 seekBarを変更すると、変更した値をTextViewにリアルタイムで表示したいと考えています。 新しいことを学び、Kotlinを練習するには、Seekbar.OnSeekBarChangeListenerを含む新しいクラスを作成し、そのクラスはseekBarの変更に反応します。しかし、問題は、私がを新しいseekBarの値に設定すると、findViewByIdNullPointExceptionという結果になります。KotlinのカスタムクラスでTextViewにアクセスする

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()) 
} 
} 

クラス:

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では、それは我々がkotlinx.android.synthetic.main.activity_mainインポート場合はウィジェットを参照するが、KotlinにFindViewByIdとSetContentViewの方法を使用して、簡単です。*我々はfindViewByIdを使用する必要はありません以上。私はMainActivityからコンテキストを渡そうとしましたが、それは助けにはなりません。

答えて

0

Bro私はあなたにとって最も簡単な方法でした。 私のAndroidスタジオのバージョンは3.0です。あなたのAndroid Studioを更新しない場合、例外が発生することがあります。 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?) { 
     } 
    }) 

のonCreate

MainActivity.kt

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.kt.seekbar.MainActivity"> 

<TextView 
    android:id="@+id/tv" 
    android:layout_centerInParent="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<SeekBar 
    android:id="@+id/seekBar" 
    android:layout_centerInParent="true" 
    android:max="100" 
    android:layout_below="@id/tv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 


</RelativeLayout> 
+0

ありがとうございますが、問題はどのようにこの問題を解決するということではありません最も簡単な方法。私はカスタムクラスからウィジェットにアクセスする方法を練習しているだけなので、 'seekBar'リスニングクラスとそのクラスが' NullPointException'を引き起こしているのです。私は最新のAndroid Studioを使用しています。 – Kristof

+0

私は10分を与えることを理解しています。 –

0

その私が働いてtried.IはseekBのコンストラクタへの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 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?) { 
} 
} 
+0

まだ動作していません。 'SeekBar'値を変更すると、エラーが発生します。java.lang.NullPointerException:ヌルオブジェクトリファレンスで、仮想メソッド 'android.content.res.Resources android.content.Context.getResources()'を呼び出そうとしています' – Kristof

+0

あなたのコードをここに貼り付けることができますか? –

+0

あなたのものとまったく同じ、それはあなたが上に貼り付けるものです。 – Kristof

関連する問題