我有问题。为什么当我尝试将值设置为TextView
时,调试器显示NullPointerException吗?
这是我的调试器中的日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: bjd.adrian.sebaclicker, PID: 7258
java.lang.RuntimeException: Unable to start activity ComponentInfo{bjd.adrian.sebaclicker/bjd.adrian.sebaclicker.Views.ClickerView}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at bjd.adrian.sebaclicker.Views.ClickerView.setScore(ClickerView.kt:33)
at bjd.adrian.sebaclicker.Presenters.ClickerPresenter.getScoreandSetInTextView(ClickerPresenter.kt:19)
at bjd.adrian.sebaclicker.Views.ClickerView.onCreate(ClickerView.kt:22)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
这是我的ClickerView:
class ClickerView : AppCompatActivity()
{
val context: Context = this@ClickerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_clicker_view)
val clickerPresenter = ClickerPresenter(context)
clickerPresenter.getScoreandSetInTextView()
}
fun setScore(score: Int)
{
var score = score
Log.v("CLICKER VIEW", "SCORE: "+score)
var scoreS = score.toString()
val scoreTV: TextView = findViewById(R.id.scoreTV)
scoreTV.text = scoreS
}
}
这是我的ClickerPresenter:
class ClickerPresenter(contextP: Context)
{
val context = contextP
fun getScoreandSetInTextView()
{
val ClickerView = ClickerView()
val ClickerModel = ClickerModel(context)
var score = ClickerModel.getScore()
ClickerView.setScore(score)
}
}
这是我的ClickerModel:
class ClickerModel(contextP: Context)
{
var context = contextP
fun getScore() : Int
{
var prefs: SharedPreferences? = null
prefs = context.getSharedPreferences("PREFS", 0)
var score = prefs.getInt("sebolary", 1)
Log.v("ClickerModel", "SCORE: "+score.toString())
return score
}
}
变量score
不是 null (我在日志中对此进行了检查)。
我当时正在互联网上寻求解决此问题的方法,但是我什么也没发现。
这是.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Views.ClickerView">
<ImageView
android:src="@drawable/background"
android:layout_width="998dp"
android:layout_height="993dp"
android:id="@+id/backgroungIV" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:layout_marginBottom="9dp" app:layout_constraintBottom_toBottomOf="parent"
android:adjustViewBounds="false" android:cropToPadding="true" app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintVertical_bias="0.893"/>
<Button
android:layout_width="143dp"
android:background="@drawable/seba"
android:layout_height="200dp"
android:id="@+id/sebaBTT" android:layout_marginTop="144dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="120dp"/>
<Button
android:layout_width="100dp"
android:layout_height="54dp"
android:id="@+id/shopBTT" android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="284dp"
android:background="@drawable/shoppingcart"
/>
<TextView
android:text="Sebolary: "
android:layout_width="74dp"
android:layout_height="22dp"
android:id="@+id/textView" android:layout_marginTop="44dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp" android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/scoreTV" app:layout_constraintVertical_bias="0.0"/>
<TextView
android:text="0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scoreTV" android:textSize="18sp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.068"
app:layout_constraintStart_toEndOf="@+id/textView" android:layout_marginStart="16dp"/>
<Button
android:text="Osignięcia"
android:layout_width="390dp"
android:layout_height="56dp"
android:id="@+id/achevementsBTT" android:layout_marginTop="516dp"
android:background="@drawable/achevementsbar"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
/>
<Button
android:text="Resetuj licznik"
android:layout_width="105dp"
android:layout_height="62dp"
android:id="@+id/resetBTT"
android:background="@drawable/background"
android:layout_marginTop="432dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp"/>
<Button
android:text="Zapisz wynik"
android:layout_width="105dp"
android:layout_height="62dp"
android:id="@+id/saveBTT" android:layout_marginTop="440dp"
android:background="@drawable/background"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp"/>
答案 0 :(得分:1)
ClickerView
是一项活动:
class ClickerView : AppCompatActivity()
但是您要自己实例化它:
val ClickerView = ClickerView()
,然后在其上调用方法:
ClickerView.setScore(score)
您不应该自己实例化Activity
类-它们对您需要Activity
进行的任何操作都不会有好处。
常见的MVP方法是Presenter持有对其View的引用-将ClickerView
作为ClickerPresenter
的参数。