片段错误logcat,如果立即崩溃,当我尝试启动应用程序时,home片段不喜欢它。
2019-09-23 16:45:10.033 6139-6139/? E/ex.connexsocia: Unknown bits set in runtime_flags: 0x8000 2019-09-23 16:45:11.249 6139-6139/com.connex.connexsocial E/FragmentManager: No view found for id 0x7f080055 (com.connex.connexsocial:id/container) for fragment HomeFragment{3cd0405 (50d70a86-2cb8-4ba3-844c-f016bd5545cb) id=0x7f080055 } 2019-09-23 16:45:11.249 6139-6139/com.connex.connexsocial E/FragmentManager: Activity state: 2019-09-23 16:45:11.269 6139-6139/com.connex.connexsocial E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.connex.connexsocial, PID: 6139
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textSize="30sp"
android:gravity="center"
android:text="Home" />
</FrameLayout>
答案 0 :(得分:0)
我看到您的id
没有设置TextView
。像下面这样给它id
,希望它能起作用。
<TextView
android:id="@+id/tv_name_your_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textSize="30sp"
android:gravity="center"
android:text="Home" />
然后从您的片段中调用它,例如:(我在Kotlin中这样做)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val containerView = inflater.inflate(R.layout.fragment_restaurant_food_cart_preview, container, false)
containerView.tv_name_your_textview.text = "Some value"
return containerView
}
答案 1 :(得分:0)
要向活动添加片段,我们可以在Framelayout中使用容器ID, 但是您忘了在framelayout中使用id。 因此,请在您的框架布局中添加ID。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment"
android:id="@+id/container">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textSize="30sp"
android:gravity="center"
android:text="Home" />
</FrameLayout>
并在您的活动中添加这样的片段
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container, new HomeFragment()).commit();
答案 2 :(得分:-1)
public class HomeFragment extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment,container,false);
return view;
}
像这样初始化片段布局