将片段放入框架布局:片段的列表视图为空
Putting a fragment into framelayout : fragment's listview is null
我尝试用片段填充 activity,但当我尝试加载片段时,片段的列表视图为空。我试图将 setContentView(R.layout.fragment) 添加到 activity,但出现错误,找不到视图。
这是我的activity主要代码的一部分
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
setContentView(R.layout.activity_main)
val fragmentCourant: Fragment = Fragment()
val fm: FragmentManager = supportFragmentManager//supportFragmentManager//getSupportFragmentManager()//supportFragmentManager
val transaction: FragmentTransaction = fm.beginTransaction()
transaction.replace(R.id.FrameLayout,fragmentCourant)
transaction.commit()
这是我的片段的一部分
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view=inflater?.inflate(R.layout.fragment,container,false)
val button=view.findViewById<Button>(R.id.button)
var listView = view.findViewById<ListView>(R.id.listView)
listView.adapter=CustomAdapter(DataClass(),this)
谢谢。
解决方案:
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
val fragmentCourant = Fragment()
fragmentCourant.arguments = intent.extras
supportFragmentManager.beginTransaction()
.add(R.id.FrameLayout, fragmentCourant)
.commit()
}
我尝试用片段填充 activity,但当我尝试加载片段时,片段的列表视图为空。我试图将 setContentView(R.layout.fragment) 添加到 activity,但出现错误,找不到视图。
这是我的activity主要代码的一部分
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
setContentView(R.layout.activity_main)
val fragmentCourant: Fragment = Fragment()
val fm: FragmentManager = supportFragmentManager//supportFragmentManager//getSupportFragmentManager()//supportFragmentManager
val transaction: FragmentTransaction = fm.beginTransaction()
transaction.replace(R.id.FrameLayout,fragmentCourant)
transaction.commit()
这是我的片段的一部分
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view=inflater?.inflate(R.layout.fragment,container,false)
val button=view.findViewById<Button>(R.id.button)
var listView = view.findViewById<ListView>(R.id.listView)
listView.adapter=CustomAdapter(DataClass(),this)
谢谢。
解决方案:
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
val fragmentCourant = Fragment()
fragmentCourant.arguments = intent.extras
supportFragmentManager.beginTransaction()
.add(R.id.FrameLayout, fragmentCourant)
.commit()
}