1

我正在测试基本的 Fragment 东西,所以我创建了 4 个类,1 个 MainActivity,1 个 FragmentActivity 和 2 个 Fragment,我们的想法是我们通过 MainActivity 中的一个按钮调用 FragmentActivity,它应该显示 Fragment1 和 Fragment2,因为 Fragment1 有 30%带有橙色背景的屏幕和 Fragment 2 的 70% 带有蓝色背景。没有错误,只是当我单击 MainActivity 中的按钮时,它会转到 FragmentActivity,但只显示带有 textview 的白屏,但​​是当我去的时候进入 FragmentActivity 上的 .xml 文件,它会显示橙蓝屏幕?

这是 MainActivity 部分

    buttonFrag2.setOnClickListener(new Button.OnClickListener() {


    public void onClick(View v) {


        Intent intent = new Intent(MainActivity.this,StaticFragment.class);
         startActivity(intent); 
                 }

           }); 

然后是 StaticFragment 类

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

public class StaticFragment extends FragmentActivity {
@Override
protected void onCreate(Bundle bundle) {


    super.onCreate(bundle);
        }

}

和 static_fragment.xml ,如前所述,在图形布局中正确显示

<LinearLayout 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"
android:orientation="horizontal" 
android:baselineAligned="false"
>


<fragment

    android:name="com.example.fragment.ListFrag"
    android:id="@+id/listFrag"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight=".3"
    tools:layout="@layout/list_frag" />

<fragment

    android:name="com.example.fragment.DetailFrag"
    android:id="@+id/detailFrag"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight=".7"
    tools:layout="@layout/detail_frag" />

</LinearLayout> 

也作为片段类之一的示例(它们是相同的)

package com.example.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListFrag extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.list_frag, container, false);
}

}
4

1 回答 1

2

您没有设置新活动的内容视图。您可能正在推送到 StaticFragment Activity,但您没有加载视图的 xml。在super.onCreate()您的 StaticFragmentActivity 之后,添加setContentView(R.layout.static_fragment.xml);

应该解决问题。

于 2013-08-13T16:37:38.290 回答