Android studio 写出来的app第一次为空,后来就闪退报错。写的内容是添加两个xml文件,分别是textview和imageview,分别添加对应的Java文件,java文件中写了onCreatView方法,在activity_main中写两个framelayout,两个framelayout里面分别引用textview和imageview的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
</LinearLayout>
package com.example.fragmentdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class TextViewFragment extends Fragment {
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull @org.jetbrains.annotations.NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
inflater.inflate(R.layout.fragment_textview/*类型 int resource*/,container/* 类型 ViewGroup*/,false);
View view = null;
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--android:name="com.example.fragmentdemo.TextViewFragment" 当前的Fragment由哪一个类 对他的内容或者行为进行处理-->
<FrameLayout
android:name="com.example.fragmentdemo.ImageViewFragment"
android:id="@+id/image_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="@+id/text_fragment"
android:name="com.example.fragmentdemo.TextViewFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>