0

我有 3 个片段,我很想以编程方式添加它们,

“mainQuiz.java”

package mobi.hkgh.tracnghiem_package;

import android.os.Bundle;
import android.app.Activity;
import android.widget.LinearLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
import android.view.View;


public class mainQuiz extends FragmentActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_quiz);

    if(getSupportFragmentManager().findFragmentByTag("head")==null) {
        head_status_fragment head = new head_status_fragment();
        main_content_fragment main_content = new main_content_fragment();
        choice_fragment choice = new choice_fragment();


        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        ft.add(R.id.main_quiz_layout, head, "head");
        ft.add(R.id.main_quiz_layout, main_content, "main_content");
        ft.add(R.id.main_quiz_layout, choice, "choice");


        ft.commit();
        fm.executePendingTransactions();
    }
    quiz();
}

public void quiz() {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    main_content_fragment kk = (main_content_fragment) fm.findFragmentByTag("main_content");
    View z = kk.getView();

    if(z==null) {
        ft.remove(fm.findFragmentByTag("choice"));
    }
    ft.commit();
    fm.executePendingTransactions();
}

}

“head_status_fragment.java”:

package mobi.hkgh.tracnghiem_package;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.TextView;

public class head_status_fragment extends Fragment  {
public void head_status_fragment(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    onCreateView(inflater, parent, savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.head_status_fragment, parent, false);
        return view;
}


}

“main_content_fragment.java”:

package mobi.hkgh.tracnghiem_package;

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

public class main_content_fragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_content_fragment, container, false);
        return view;
}

}

并且,“choice_fragment.java”:

package mobi.hkgh.tracnghiem_package;

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

public class choice_fragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.choice_fragment, container, false);
    return view;
}
}

如您所见,“z”变量为空,我不知道为什么?

4

1 回答 1

1

我不知道为什么?

onCreateView()尚未在片段上调用。

恕我直言,一个活动应该试图弄乱其片段的小部件。将该工作委托给片段,片段可以在小部件准备好时在适当的时间完成该工作。

于 2013-09-08T11:08:24.540 回答