android - 在添加之前或之后设置 fragment 内容?

标签 android android-fragments

我基本上是在尝试创建这个测验系统,其中我有一个问题模板,并且我会动态更改问题的内容: 例如:

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

我将用我从数据库中选择的一些电影替换 X。 问题后面会有4个 optional (也是根据问题生成的),用户只能点击其中一个。问题和答案都在 fragment .xml 文件中。我试图在显示之前更改 fragment 的内容。看过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
    }
}

fragment 类

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 为空。但是,如果我执行挂起的交易,为什么会出现这种情况?

最佳答案

在 fragment 中生成 UI 的代码需要时间来执行。我相信即使您正在提交交易,当您尝试访问它时,用户界面仍然没有准备好。不要忘记您的 fragment 生命周期与您的 Activity 生命周期相关联,但您希望在 Activity 的 OnCreate 方法中访问 fragment UI。

考虑改为使用回调模式在您的 Activity 和 fragment 之间进行通信。

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

使用此模式,您可以在确定 UI 已完成且可用时调用您的 Activity 代码。

关于android - 在添加之前或之后设置 fragment 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288768/

相关文章:

android - 绑定(bind)服务泄漏内存

java - fragment 在两 Pane 平板电脑模式下未正确显示

java - CursorLoader 不刷新数据

android - 在android中将可见性设置为GONE后如何恢复布局

Android Studio 呈现交互式内容的错误

javascript - 警告 : Memory Router ignores the history props

android - fragment 中的静态字段

android - 导航架构组件-如何刷新 fragment ?

java - ARCore - 从 Modelrenderable 列表中选择不同的 3D 对象

android - 应用程序 :srcCompat does not work for ImageView