1

アクティビティのコンテンツビューが設定された後、アクティビティにフラグメントを追加しようとしています。互換性パッケージを使用しておらず、ターゲットAPIを16に設定しています。メインアクティビティのsetContentViewメソッドの後にアクティビティがフラグメントレイアウトに設定されることを期待していますが、実行時にフラグメントが追加されることはなく、画面が表示されます。空白で。フラグメント内のライフサイクルイベントのコールバックが呼び出されることはないため、System.outsは出力されません。Logcatは、フラグメントに関連するエラーも出力しません。フラグメント要素がactivity_main.xmlで作成されている場合、フラグメントは正常に機能しますが、クラスのRelativeLayoutに追加しても機能しません。誰か助けてくれませんか?ありがとう!編集:fragment_containerとしてFrameLayoutを使用してみましたが、違いはありません。

これが私の主な活動です。

package com.example.derp;  

import android.os.Bundle;  
import android.app.Activity;  
import android.app.Fragment;  
import android.app.FragmentManager;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Fragment frag = new Fragment();
    FragmentManager fManager = getFragmentManager();
    fManager.beginTransaction().add(R.id.fragment_container, frag).commit();
}
}

これは私の断片です:

 package com.example.derp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Frag extends Fragment{

public Frag(){

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    System.out.println("onAttach");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("onCreate");
}

@Override
public void onStart() {
    super.onStart();
    System.out.println("onStart");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    System.out.println("onCreateView");
    return inflater.inflate(R.layout.fragment_frag, container, false);//
}
}

これはfragment_frag.xmlです:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="64dp"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

</RelativeLayout>

activity_main.xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >    

<RelativeLayout 
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</RelativeLayout>

</RelativeLayout>
4

1 に答える 1

1

フラグメントクラスのインスタンスを作成していません。むしろ、基本フラグメントクラスのオブジェクトを作成しています。

変化する...

Fragment frag = new Fragment();

に...

Fragment frag = new Frag();
于 2013-02-20T23:53:54.400 に答える