首页 > 解决方案 > Android静态片段 - 应用程序崩溃问题

问题描述

我是一名 android 初学者,刚刚学习 android 静态片段概念,但在运行我的代码后出现应用程序崩溃问题。以下是详细信息:

MainActivity.java

package com.example.lalendrakumar.fragmentdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:weightSum="2">

<fragment
    android:id="@+id/fragment1"

    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />
<fragment
    android:id="@+id/fragment2"


    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />

所以任何人都可以解决我的问题。

标签: javaandroidandroid-fragments

解决方案


我假设您希望这两个片段填充页面。“fill_parent”现在已被“match_parent”取代。但是,如果您使用它,理论上,这两个片段将彼此重叠。

要在屏幕上拆分它们,每个都必须调整到零高度。这是每个片段的尺寸应该如何读取

android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"

推荐阅读