在添加之前或之后设置片段内容?

时间:2014-03-09 22:02:18

标签: android android-fragments

我基本上试图创建这个测验系统,我有一个问题模板,我动态地改变了问题的内容: 例如:

Who directed movie X?
   - Incorrect answer director
   - Correct answer director
   - Incorrect answer director
   - Incorrect answer director

我将用我从数据库中选择的一些电影替换X. 以下问题将有4个可选选项(也基于问题生成),用户只能单击其中一个。问题和答案都在片段.xml文件中。我正在尝试在显示之前更改片段的内容。我已经阅读了android文档,但是这部分似乎没有写得足够详细。

public class QuizTemplate extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz_template);
            if (savedInstanceState == null) {
                PlaceholderFragment firstFragment = new PlaceholderFragment();

                // Trying to set question before I commit transaction
                // firstFragment.setQuestion("IT WORKS!");

                FragmentManager manager = getSupportFragmentManager();
                manager.beginTransaction()
                .add(R.id.container2, firstFragment).commit();

    //          Tried to execute pending transactions, then find view
                manager.executePendingTransactions();

                PlaceholderFragment firstFragment = (PlaceholderFragment) manager.findFragmentById(R.id.container2);
                RadioButton answer1 = (RadioButton) firstFragment.getView().findViewById(R.id.answer1);
                RadioButton answer2 = (RadioButton) firstFragment.getView().findViewById(R.id.answer2);
                RadioButton answer3 = (RadioButton) firstFragment.getView().findViewById(R.id.answer3);
                RadioButton answer4 = (RadioButton) firstFragment.getView().findViewById(R.id.answer4);
                TextView question  = (TextView) firstFragment.getView().findViewById(R.id.question);

                question.setText("test");
                answer1.setText("test");
    //          ... and so on
    }
}

片段类

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quiz_template,
                container, false);
        return rootView;
    }

    public void setQuestion(String text){
        TextView textView = (TextView) getView().findViewById(R.id.question);
        textView.setText(text);
    }
}

(布局)fragment_quiz_template.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="com.example.moviequiz.QuizTemplate$PlaceholderFragment" >

    <TextView
        android:id="@+id/question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:text="@string/hello_world" />

    <RadioButton
        android:id="@+id/answer2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/answer3"
        android:layout_below="@+id/answer3"
        android:layout_marginTop="28dp"
        android:onClick="changeQuestion"
        android:text="@string/hello_world" />

    <RadioButton
        android:id="@+id/answer3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/answer4"
        android:layout_below="@+id/answer4"
        android:layout_marginTop="17dp"
        android:onClick="changeQuestion"
        android:text="@string/hello_world" />

    <RadioButton
        android:id="@+id/answer4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/answer1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:onClick="changeQuestion"
        android:text="@string/hello_world" />

    <RadioButton
        android:id="@+id/answer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/answer4"
        android:layout_below="@+id/question"
        android:layout_marginTop="40dp"
        android:onClick="changeQuestion"
        android:text="@string/hello_world" />

</RelativeLayout>

我的方法都没有用,因为我的应用程序在到达测验时崩溃了(日志中没有任何东西?)。什么可能出错的想法?​​

编辑:修正了我的日志,它现在告诉我:

Unable to start activity ComponentInfo: java.lang.NullPointerException

所以我的猜测就是当我打电话给

RadioButton answer1 = (RadioButton) firstFragment.getView().findViewById(R.id.answer1);

answer1为空。但是,如果我执行挂起的交易,为什么会这样呢?

1 个答案:

答案 0 :(得分:1)

在片段中生成UI的代码需要一些时间来执行。我相信即使你提交了交易,当你试图访问它时,UI仍然没有准备好。不要忘记,您的片段生命周期与您的活动生命周期相关联,但您希望在活动的OnCreate方法中访问片段UI。

请考虑使用回调模式在您的活动和片段之间进行通信。

http://developer.android.com/training/basics/fragments/communicating.html

使用此模式,您可以在确定用户界面完整且可用时调用活动代码。