getFragmentByTag()返回Fragment,但getFragmentByTag()。getView()返回null

时间:2013-09-08 09:05:46

标签: android android-fragments nullpointerexception

我有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,我无法找出原因?

1 个答案:

答案 0 :(得分:1)

  

我无法弄明白为什么?

尚未在片段上调用

onCreateView()

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