片段给了我白色的屏幕

时间:2019-02-22 09:42:45

标签: java android android-fragments fragment

我已经构建了一个应用程序,其中包含一个包含两个片段的活动。上面的片段应该获得两条信息(名称和年龄),并将它们插入到下面的片段的TextView中。但是,当我运行该应用程序时,出现白屏。 Logcat没有显示任何错误。 我添加了xml文件,也许是问题所在。

public class userActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListener {
@Override
public void createName(String name, int age) {
    BottomFragment bottomFragment= (BottomFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
    bottomFragment.setName(name,age);
}


public class TopSectionFragment extends Fragment {
private static EditText name,age;

TopSectionListener activityCommander;
public interface TopSectionListener {
    public void createName(String name,int age);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        activityCommander=(TopSectionListener)context;
    }catch (ClassCastException e){
        throw new ClassCastException(context.toString());
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.top_section_fragment,container,false);
    name= (EditText)view.findViewById(R.id.name);
    age = (EditText)view.findViewById(R.id.age);
    final Button save = (Button) view.findViewById(R.id.save);
    save.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    saveClicked(v);
                }
            }

            );
    return view;
}
public void saveClicked(View view) {
    activityCommander.createName(name.getText().toString(),Integer.getInteger(age.getText().toString()));
}
}


public class BottomFragment extends Fragment {
private static TextView list;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.bottom_fragment,container,false);
    list = (TextView)view.findViewById(R.id.list);
    return view;
}
public void setName(String name,int age){
    list.setText(name + " " + age);
}

}

activity-

    <?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:orientation="vertical"
    android:background="#FF4F87BC"
    tools:context="com.example.student.familycommitment.userActivity">


    <fragment
        android:id="@+id/fragment"
        android:name="com.example.student.familycommitment.TopSectionFragment"
        android:layout_width="match_parent"
        android:layout_height="336dp"
        tools:layout="@layout/top_section_fragment" />

    <fragment
        android:id="@+id/fragment2"
      android:name="com.example.student.familycommitment.userList$BottomFragment"
        android:layout_width="match_parent"
        android:layout_height="392dp"
        tools:layout="@layout/bottom_fragment" />
</LinearLayout>

bottom-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    android:background="#FF4F87BC"
    tools:context="com.example.student.familycommitment.userActivity">

    <TextView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="228dp"
        android:layout_marginTop="450dp"
        android:fontFamily="serif"
        android:gravity="start"
        android:textColor="#FFFF"
        android:textSize="20sp"></TextView>
</RelativeLayout>

top-

<?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:orientation="vertical"
    android:background="#FF4F87BC"
    tools:context="com.example.student.familycommitment.userActivity">

    <TextView
        android:id="@+id/user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center_horizontal"
        android:text="Please enter your children's names and ages"
        android:textColor="#FFFF"
        android:fontFamily="sans-serif-black"
        android:textSize="30sp"></TextView>

    <EditText
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/name"
        android:hint="Name"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textSize="25sp"
        android:background="@android:drawable/editbox_background_normal"
        />
    <EditText
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/age"
        android:hint="age"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="25sp"
        android:background="@android:drawable/editbox_background_normal"
        />

    <Button
        android:id="@+id/save"
        android:layout_width="134dp"
        android:layout_height="34dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@android:drawable/editbox_dropdown_dark_frame"
        android:fontFamily="monospace"
        android:gravity="center_horizontal"
        android:text="save"
        android:textColor="#FFFF"
        android:textSize="20sp" />

</LinearLayout>

0 个答案:

没有答案