我有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'变量为null,我无法找出原因?
答案 0 :(得分:1)
尚未在片段上调用我无法弄明白为什么?
onCreateView()
。