任何人都可以帮我找到这段代码中的错误,因为它说,它不能在主要活动中膨胀片段,顺便说一句,我是android的新手。我试图将膨胀的代码行更改为onCreate函数中的最后一行,但它什么也没做。
以下是活动代码:
package com.voicenoteinc.tictactou
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.FragmentActivity
import android.support.v4.app.Fragment
import android.view.View
import android.widget.Button
import kotlinx.android.synthetic.main.fragment_test.*
class MainActivity : AppCompatActivity() {
var tstFragment:TestFragment? = null
val manager = supportFragmentManager
val FRAGMENT_TAG = "fragment_tag"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (manager.findFragmentByTag(FRAGMENT_TAG) != null) {
tstFragment = manager.findFragmentByTag(FRAGMENT_TAG) as TestFragment
}
if (tstFragment == null) {
tstFragment = TestFragment()
manager.beginTransaction().add(tstFragment, FRAGMENT_TAG).commit()
}
}
}
XML活动:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
tools:context="com.voicenoteinc.tictactou.MainActivity">
<fragment
android:id="@+id/fragment"
android:name="com.voicenoteinc.tictactou.TestFragment"
android:tag="fragment_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_test" />
</LinearLayout>
片段类:
package com.voicenoteinc.tictactou
import android.content.Context
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_test.*
class TestFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
bu.setOnClickListener { bu.setBackgroundColor(Color.CYAN) }
return inflater!!.inflate(R.layout.fragment_test, container, false)
}
}// Required empty public constructor
答案 0 :(得分:0)
有两种方法可以将Fragment
添加到Activity
。
像在此处一样静态添加:
<fragment
android:id="@+id/fragment"
android:name="com.voicenoteinc.tictactou.TestFragment"
android:tag="fragment_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_test" />
将其动态添加到ViewGroup
,如下所示:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
看来你试图在这里做两件事。假设你想静态添加它,你可以安全地删除所有这些代码:
// Remove all this code
if (manager.findFragmentByTag(FRAGMENT_TAG) != null) {
tstFragment = manager.findFragmentByTag(FRAGMENT_TAG) as TestFragment
}
if (tstFragment == null) {
tstFragment = TestFragment()
manager.beginTransaction().add(tstFragment, FRAGMENT_TAG).commit()
}
在Adding a fragment to an activity