android.view.InflateException:二进制XML文件第26行:尝试在空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)' 我正在构建一个音乐播放器应用程序,在对mainactivity.kt进行少量修改并添加了NavigationDraweradapter.kt的代码之前,该应用程序运行良好,现在当我运行该应用程序时,会显示启动画面,但当它进入主要活动时,该应用程序将崩溃并显示以上错误。 我已附上以下代码,请帮助。 MAINACTIVITY.KT
package com.asr.suno.activities
import android.os.Bundle
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import com.asr.suno.fragments.MainScreenFragment
import com.asr.suno.R
import com.asr.suno.adapter.NavigationDrawerAdapter
class MainActivity : AppCompatActivity() {
var navigationDrawerIconList: ArrayList<String> = arrayListOf()
var images_for_navdrawer=
intArrayOf(R.drawable.navigation_allsongs,R.drawable.navigation_favorites,
R.drawable.navigation_settings,R.drawable.navigation_aboutus)
object Statified {
var drawerLayout: DrawerLayout? = null
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar=findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
MainActivity.Statified.drawerLayout=findViewById(R.id.drawer_layout)
navigationDrawerIconList.add ("All Songs")
navigationDrawerIconList.add("Favorite")
navigationDrawerIconList.add("Settings")
navigationDrawerIconList.add("About Us")
val toggle=ActionBarDrawerToggle(this@MainActivity,
MainActivity.Statified.drawerLayout,
toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close)
// MainActivity.Statified.drawerLayout?.setDrawerListener(toggle)
MainActivity.Statified.drawerLayout?.addDrawerListener(toggle)
toggle.syncState()
val mainScreenFragment = MainScreenFragment()
this.supportFragmentManager
.beginTransaction()
.add(R.id.details_fragment,mainScreenFragment,"MainScreenFragment")
.commit()
var _navigationAdapter =
NavigationDrawerAdapter(navigationDrawerIconList,images_for_navdrawer,this)
_navigationAdapter.notifyDataSetChanged()
var navigation_recycler_view =findViewById<RecyclerView>
(R.id.navigation_recycler_view)
navigation_recycler_view.layoutManager= LinearLayoutManager(this)
navigation_recycler_view.itemAnimator= DefaultItemAnimator()
navigation_recycler_view.adapter = _navigationAdapter
navigation_recycler_view.setHasFixedSize(true)
}
override fun onStart() {
super.onStart()
}
}
NAVIGATIONDRAWERADAPTER.KT
package com.asr.suno.adapter
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.asr.suno.R
import com.asr.suno.activities.MainActivity
import com.asr.suno.fragments.AboutUsFragment
import com.asr.suno.fragments.FavoriteFragment
import com.asr.suno.fragments.MainScreenFragment
import com.asr.suno.fragments.SettingsFragment
class
NavigationDrawerAdapter(_contentList:ArrayList<String>,
_getImages:IntArray,_context:Context)
:RecyclerView.Adapter<NavigationDrawerAdapter.NavViewHolder>(){
var contentList: ArrayList<String>?=null
var getImages: IntArray?=null
var mContext: Context?=null
init {
this.contentList=_contentList
this.getImages=_getImages
this.mContext=_context
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int):
NavViewHolder {
var itemView =
LayoutInflater.from(parent?.context)
.inflate(R.layout.row_custom_navigationdrawer,parent,false)
val returnThis=NavViewHolder(itemView)
return returnThis
}
override fun onBindViewHolder(holder: NavViewHolder?, position: Int) {
holder?.icon_GET?.setBackgroundResource(getImages?.get(position)as Int)
holder?.text_GET?.setText(contentList?.get(position))
holder?.contentHolder?.setOnClickListener({
if(position==0){
val mainScreenFragment = MainScreenFragment()
(mContext as MainActivity).supportFragmentManager
.beginTransaction()
.replace(R.id.details_fragment,mainScreenFragment)
.commit()
}else if(position==1){
val favorateFragment = FavoriteFragment()
(mContext as MainActivity).supportFragmentManager
.beginTransaction()
.replace(R.id.details_fragment,favorateFragment)
.commit()
}else if(position==2) {
val settingsFragment = SettingsFragment()
(mContext as MainActivity).supportFragmentManager
.beginTransaction()
.replace(R.id.details_fragment,settingsFragment)
.commit()
}else {
val aboutUsFragment = AboutUsFragment()
(mContext as MainActivity).supportFragmentManager
.beginTransaction()
.replace(R.id.details_fragment,aboutUsFragment)
.commit()
}
MainActivity.Statified.drawerLayout?.closeDrawers()
})
}
override fun getItemCount(): Int {
return (contentList as ArrayList).size
}
class NavViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
var icon_GET: ImageView? = null
var text_GET: TextView? = null
var contentHolder: RelativeLayout? = null
init {
icon_GET = itemView?.findViewById(R.id.icon_navdrawer)
text_GET = itemView?.findViewById(R.id.text_navdrawer)
contentHolder =
itemView?.findViewById(R.id.navdrawer_item_content_holder)
}
}
}