如何在Android中使用kotlin获取片段中的资源ID?
我尝试了下面提到的这个代码,但在运行时崩溃了。 发生的错误是Android运行时间:
致命例外:主进程:com.root.specialbridge,PID:17706 kotlin.KotlinNullPointerException在com.root.specialbridge.fragments.profile_fragment.WallFragments.initializeView(WallFragments.kt:49)
class WallFragments : Fragment(){ private var wallAdapter: WallAdapter? = null private var wall_recycler: RecyclerView? = null private val wallArrayList: ArrayList? = null private var mainlayout: LinearLayout? = null private var no_result_found_layout: RelativeLayout? = null private var userProfileWallInterface: UserProfileWallInterface? = null internal var wallActivityBeanse: MutableList = ArrayList() override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater!!.inflate(R.layout.wall_fragments, container, false) userProfileWallInterface = UserProfileWallPresentation(activity, this) initializeView() wallAdapter = WallAdapter(activity, wallActivityBeanse) wall_recycler!!.adapter = wallAdapter return view } fun initializeView() { wall_recycler = view!!.findViewById(R.id.wall_recycler_id) as RecyclerView mainlayout = view!!.findViewById(R.id.mainlayout) as LinearLayout no_result_found_layout = view!!.findViewById(R.id.no_result_found_layout) as RelativeLayout wall_recycler!!.layoutManager = LinearLayoutManager(activity) wall_recycler!!.setHasFixedSize(true) if (AuthPreference(activity).isGetMemberProfile) { userProfileWallInterface!!.getMemberProfileWall(view!!) } else { userProfileWallInterface!!.getUserProfileWall(AuthPreference(activity).token, AuthPreference(activity).user.id, view!!) } } companion object { val instance: WallFragments get() = WallFragments() }}
介绍Kotlin Android扩展 。
你不必再使用findViewById
了。 使用这个插件,你可以直接使用UI组件作为一个全局字段。 活动,片段和视图支持。
例如,要从下面的布局引用文本视图,
在活动中,你可以简单地写,
// Using R.layout.activity_main from the main source set import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Instead of findViewById(R.id.hello) as TextView hello.setText("Hello, world!") } }
而且,你不应该使用!!
到处都是,除非你明确地需要NullPointerException
。
而是使用以下任何人:
- 做无效检查与安全电话 –
?.
例如,nullableVariable?.method()
- 使用
?.let{ }
使用非空对象,例如。nullableVariable?.let { nonNullVariable.method() }
- 使用elvis运算符为可为空的variables提供备份值 –
?:
例如。nullableVariable ?:
。
在Kotlin阅读更多关于Null安全的信息 。
加
apply plugin: 'kotlin-android-extensions'
在应用程序级别的gradle文件中
import
import kotlinx.android.synthetic.main.fragment_your_fragment_name.view.*
在你的片段的onCreateView
(例如,如果你的textview的id是textView)
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { // Inflate the layout for this fragment val view = inflater!!.inflate(R.layout.fragment_splashfragment, container, false) view.textView.text = "hello" //add your view before id else getting nullpointer exception return view }
在片段中初始化视图:
wall_recycler=view.findViewById(R.id.wall_recycler_id) mainlayout = view.findViewById(R.id.mainlayout)
问题是您正在访问得太快。 getView()
和view
在onCreateView
返回null。我已经findonViewCreated()
所有视图。 尝试在onViewCreated
方法中做到这onViewCreated
:
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { wall_recycler=getView().findViewById(R.id.wall_recycler_id) mainlayout = getView().findViewById(R.id.mainlayout) mainlayout.setOnClickListener { Log.d(TAG, "onViewCreated(): hello world");} }