gpt4 book ai didi

android - com.example.myapplication.ui.home.HomeFragment上的kotlin.KotlinNullPointerException

转载 作者:行者123 更新时间:2023-12-02 13:04:43 30 4
gpt4 key购买 nike

我正在尝试操作片段xml中的元素,但是我所做的所有尝试都导致了一些错误。当我使用root !!。qr_code.visibility = View.INVISIBLE时,我得到了:
我的元素引用有什么问题?

FATAL EXCEPTION: mainProcess: com.example.test, PID: 26445java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.myapplication.MainActivity}: kotlin.KotlinNullPointerExceptionat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:201)at android.app.ActivityThread.main(ActivityThread.java:6810)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)Caused by: kotlin.KotlinNullPointerExceptionat com.example.myapplication.ui.home.HomeFragment.setElementVisibility(HomeFragment.kt:49)at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:72)


我的HomeFragment :(从MainActivity中的onCreate调用)
package com.example.myapplication.ui.home

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.example.myapplication.R
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_home.view.*

class HomeFragment : Fragment() {

private lateinit var homeViewModel: HomeViewModel
private var root: View? = null

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreate(savedInstanceState);
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
root = inflater.inflate(R.layout.fragment_home, container, false)

//The findViewById appears as unresolved reference.
qrcode = findViewById(R.id.qr_code) as LinearLayout

return root

}

fun setElementVisibility(permissionData: Boolean, serialData: Boolean){
//also I tried:
//qr_code.visibility = View.INVISIBLE
//which gives me the same error from above
if(!serialData){
root!!.qr_code.visibility = View.VISIBLE
root!!.failed_layout.visibility = View.VISIBLE
root!!.success_layout.visibility = View.INVISIBLE
} else if(!permissionData){
root!!.qr_code.visibility = View.INVISIBLE
root!!.failed_layout.visibility = View.VISIBLE
root!!.success_layout.visibility = View.INVISIBLE
} else if(permissionData && serialData){
root!!.qr_code.visibility = View.INVISIBLE
root!!.failed_layout.visibility = View.INVISIBLE
root!!.success_layout.visibility = View.VISIBLE
}
}
}
对HomeFragment的MainActivity调用:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)



val homeFragment: HomeFragment = HomeFragment()
homeFragment.setElementVisibility(true, true)//this parameters won't be statics

}
我的xml片段:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".ui.home.HomeFragment">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/success_layout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginVertical="50dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:id="@+id/success_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="0dp"
android:ems="8"
android:gravity="center"
android:text="@string/success_msg"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />

</LinearLayout>

<LinearLayout
android:id="@+id/failed_layout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginVertical="50dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:id="@+id/failed_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="0dp"
android:ems="8"
android:gravity="center"
android:text="@string/failed_msg"
android:textAlignment="center"
android:textColor="@color/design_default_color_error"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:id="@+id/qr_code"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginVertical="50dp"
android:gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/openScanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/take_scan_qr_code" />
</LinearLayout>
</LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

findViewById()View对象的方法。
试试这个 :

qrcode = root.findViewById(R.id.qr_code) as LinearLayout

关于android - com.example.myapplication.ui.home.HomeFragment上的kotlin.KotlinNullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63134010/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com