片段中的Kotlin RecyclerView-导航架构组件-空错误

时间:2018-06-23 15:40:00

标签: android-fragments android-recyclerview kotlin navigation android-architecture-components

我正在尝试在一个片段中创建一个RecyclerView,但是我在RecyclerView上遇到了空错误。我能够使用带有底部导航栏的新导航架构组件成功创建该片段。我正在运行Android Studio Canary 3.2 Beta 1。

请注意,我在哪里出错了,因为我在Android Studio中没有出现任何红线。

调试错误

  

E / Android运行时:致命异常:主要       流程:com.projects.arise.mytestapp,PID:28285       java.lang.RuntimeException:无法启动活动ComponentInfo {com.projects.arise.mytestapp / com.projects.arise.mytestapp.MainActivity}:   java.lang.IllegalStateException:rview_keys不能为null           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)           在android.os.Handler.dispatchMessage(Handler.java:105)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6541)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)        原因:java.lang.IllegalStateException:rview_keys不能为null           在com.projects.arise.mytestapp.MainActivity.onCreate(MainActivity.kt:55)           在android.app.Activity.performCreate(Activity.java:6975)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)           在android.os.Handler.dispatchMessage(Handler.java:105)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6541)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

MainActivity.kt

class MainActivity : AppCompatActivity() {

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

        val host: NavHostFragment = supportFragmentManager
                .findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return

        val navController = host.navController
        setupBottomNavMenu(navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        return Navigation.findNavController(this, R.id.nav_host_fragment).navigateUp()
    }

    private fun setupBottomNavMenu(navController: NavController) {
        findViewById<BottomNavigationView>(R.id.navigation)?.let { bottomNavView ->
            NavigationUI.setupWithNavController(bottomNavView, navController)
        }
    }
}

FragmentKeys.kt

class FragmentKeys : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        setHasOptionsMenu(true)
        return inflater.inflate(R.layout.fragment_keys_layout, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        //val rview_keys: RecyclerView = findViewById(R.id.rview_keys)
        rview_keys.layoutManager = LinearLayoutManager(context)
        rview_keys.adapter = AdapterKeys(KeysListObjects.keys)
    }
}

我注释掉了上一行,因为我在“ findViewById”上遇到“无法解析的引用”错误。这行似乎不是必需的,因为它不在其他人的代码中。例如:Kotlin Android Fragment recyclerView and context issue

AdapterKeys.kt

class AdapterKeys(val keys: List<KeysList>) : RecyclerView.Adapter<AdapterKeys.KeysViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KeysViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.list_item_keys, parent, false)
        return KeysViewHolder(view)
    }

    override fun getItemCount() : Int {
        return keys.size
    } //override fun getItemCount() = keys.size

    override fun onBindViewHolder(holder: KeysViewHolder, position: Int) {
        val keysList = keys[position]
        holder.setData(keysList, position)
    }

    inner class KeysViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun setData(keysList: KeysList?, position: Int) {
            itemView.tview_Keys.text = keysList!!.keysText
        }
    }
}

ModelKeys.kt

object KeysListObjects {
    val keys = listOf<KeysList>(
            KeysList("Testing1"),
            KeysList("Testing2"),
            KeysList("Testing3")
    )
}

2 个答案:

答案 0 :(得分:1)

您收到此错误是因为您对此进行了评论

  

// val rview_keys:RecyclerView = findViewById(R.id.rview_keys)

没有声明 rview_keys 作为变量。因此,它考虑将rview_keys作为视图,因为在您的活动中,您有一个ID为 rview_keys 的视图。

跟随错误的行进行确认。

  

java.lang.IllegalStateException:

处com.projects.arise.mytestapp.MainActivity.onCreate(MainActivity.kt:55)的rview_keys不得为null

在MainActivity的第55行检查代码。

这些人没有该行的原因是他们将其用作视图。 而且您不能在onActivityCreated()中将其用作视图。

答案 1 :(得分:1)

我必须做两件事才能使它起作用:

  1. 将两行rview_keys代码从onActivityCreated移动到onViewCreated(我认为这是片段生命周期中的更早时间)。感谢以上海报在这里的反馈!所以现在我的片段类看起来像这样:

FragmentKeys.kt

class FragmentKeys : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        setHasOptionsMenu(true)
        return inflater.inflate(R.layout.fragment_keys_layout, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        rview_keys.layoutManager = LinearLayoutManager(context)
        rview_keys.adapter = AdapterKeys(KeysListObjects.keys)
    }
}
  1. 我仍然遇到构建错误,因此尽管我收到了弃用警告,但我尝试将build.gradle文件(jdk7)中的Kotlin依赖项之一更改回了(jre7)之前的状态。

被要求使用新的依赖项-无效:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

我切换回的旧依赖关系-目前正在起作用:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"