对不起,我只是想试试Kotlin有点困惑。 从MainActivity即时通讯尝试与回收站调用对话框。回收站的一部分工作正常,因为我在没有对话框的情况下进行了尝试。但是,当我试图在对话框中调用它时,它使用空指针崩溃。我猜它不能识别视图,但是我将上下文作为示例从堆栈中传递出来,但它不起作用
对话框类
class LanguageDialog(context: Context) : Dialog(context), LanguageAdapter.ItemClickListener{
private var mRussianFlagLink = "https://cdn3.iconfinder.com/data/icons/142-mini-country-flags-16x16px/32/flag-russia2x.png"
private var mEnglishFlagLink = "https://cdn3.iconfinder.com/data/icons/142-mini-country-flags-16x16px/32/flag-united-kingdom2x.png"
private lateinit var mLanguageAdapter : LanguageAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.dialog_languages)
val mLanguageList : MutableList<Language> = mutableListOf(Language("Russian", mRussianFlagLink), Language("English", mEnglishFlagLink))
val mRecyclerView = findViewById<RecyclerView>(R.id.recycle_languages)
mRecyclerView.layoutManager = LinearLayoutManager(context)
mLanguageAdapter = LanguageAdapter(mLanguageList, context)
mLanguageAdapter.setClickListener(this)
mRecyclerView.adapter = mLanguageAdapter
}
override fun onItemClick(view: View, position: Int) {
Log.d("Test", "CLICK")
}
}
此处崩溃
mRecyclerView.layoutManager = LinearLayoutManager(context)
MainActivity呼叫
private fun callLanguageDialog() {
val mDialog = LanguageDialog(this)
mDialog.show()
错误日志
FATAL EXCEPTION: main
Process: com.example.a3802256.zzzz, PID: 9833
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a3802256.zzzz/com.example.a3802256.zzzz.main.MainActivity}: java.lang.IllegalStateException: mRecyclerView must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalStateException: mRecyclerView must not be null
at com.example.a3802256.zzzz.LanguageDialog.onCreate(LanguageDialog.kt:26)
at android.app.Dialog.dispatchOnCreate(Dialog.java:395)
at android.app.Dialog.show(Dialog.java:294)
at com.example.a3802256.zzzz.main.MainActivity.callLanguageDialog(MainActivity.kt:20)
at com.example.a3802256.zzzz.main.MainActivity.onCreate(MainActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
尝试在init中使用初始化上下文,并从中删除上下文
Dialog(context)
中的LanguageDialog
class LanguageDialog(context: Context) : Dialog, LanguageAdapter.ItemClickListener {
private var context: Context? = null
init
{
this.context = context
}
private var mRussianFlagLink = "https://cdn3.iconfinder.com/data/icons/142-mini-country-flags-16x16px/32/flag-russia2x.png"
private var mEnglishFlagLink = "https://cdn3.iconfinder.com/data/icons/142-mini-country-flags-16x16px/32/flag-united-kingdom2x.png"
private lateinit var mLanguageAdapter : LanguageAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.dialog_languages)
val mLanguageList : MutableList<Language> = mutableListOf(Language("Russian", mRussianFlagLink), Language("English", mEnglishFlagLink))
val mRecyclerView = findViewById<RecyclerView>(R.id.recycle_languages)
mRecyclerView.layoutManager = LinearLayoutManager(context)
mLanguageAdapter = LanguageAdapter(mLanguageList, context)
mLanguageAdapter.setClickListener(this)
mRecyclerView.adapter = mLanguageAdapter
}
override fun onItemClick(view: View, position: Int) {
Log.d("Test", "CLICK")
}
}