【问题标题】:Retrieving the wrong selected radiobutton in a multi-fragmented viewpager在多片段查看器中检索错误的选定单选按钮
【发布时间】:2016-05-08 09:45:26
【问题描述】:

我长期潜伏在尝试学习 Android 编码(使用 Android Studio)。到目前为止,我只是通过谷歌搜索解决了我所有的问题。但是现在我真的很绝望,所以我希望有人能帮助我。

我正在尝试编写测验代码: 我使用 ViewPager 创建了许多片段(这里是 5 个)。第一个片段 (StartView.java) 只是一些带有开始测验的按钮的文本显示。以下片段都有相同的布局,只是不同的问题。所以我只使用一种布局(QuestionView.java 和 fragment_view1.xml)。每个片段有 5 个单选按钮,分组在一个单选组中。此外,还有一个按钮用于前进到下一个片段。单击此按钮时,将检索按下的单选按钮的文本并将其传递回主活动。

简而言之我的问题: 代码运行没有错误(在我的三星 Galaxy Nexus 上),但代码没有检索到正确选择的 RadioButton。第一个 QuestionView 片段确实给了我所选单选按钮的值。但是对于下一个 QuestionView 片段,我总是从上一个片段中获取选定的单选按钮。

对观察到的行为的更详细描述(A) 我运行程序并按下开始测验按钮(在 Fragment #0 上,即 StartView)。在 QuestionView 片段 (F #1) 的以下实例中,我选择了一个 RadioButton,例如数字 1。当我按下“下一个”按钮时,该数字随后会打印在 logcat 中(“问题视图 - 文本 1”,正确行为)。 (B) 所以我前进到下一个片段(F #2),我再次选择一个 RadioButton,比如数字 3。但是当我按下按钮时,前一个片段的 RadioButton 会打印在logcat(即:logcat 读取“问题视图 - TEXT 1”,而不是数字 3。) (C) 在下一个片段 (F #3) 中,选择一个单选按钮(例如数字 5 ) 并按下“下一步”按钮,上一个片段 (#2) 中的数字将打印在 logcat 中(“问题视图 - 文本 3”,我预计数字 5)。所以检索到的数字总是前一个片段的那个。

我希望我充分描述了我的问题。当我解释得很糟糕时,请不要犹豫告诉我——英语不是我的母语,而且解释得更少:)。

非常感谢,感谢你的努力。

代码如下:

MainActivity.java

public class MainActivity extends FragmentActivity
    implements QuestionView.OnDataPass{

private ViewPager viewPager;
private MyAdapter pageAdapter;
private static final int ITEMS = 5;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (findViewById(R.id.pager) != null) {
        if (savedInstanceState != null) {
            return;
        }
        viewPager = (ViewPager)findViewById(R.id.pager);
        pageAdapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pageAdapter);
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

public static class MyAdapter extends FragmentPagerAdapter {
    private Fragment[] tabList = new Fragment[ITEMS]; 
    public MyAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {
        return ITEMS;
    }


    @Override
    public Fragment getItem(int i) {
        if (tabList[i] != null) {
            return tabList[i];
        } else {
            switch (i) {
                case 0:
                    tabList[0] = StartView.newInstance("Introduction screen");
                    return  tabList[0];
                case 1:
                    tabList[1] = QuestionView.newInstance("Question 1");
                    return tabList[1];
                case 2:
                    tabList[2] = QuestionView.newInstance("Question 2");
                    return tabList[2];
                case 3:
                    tabList[3] = QuestionView.newInstance("Question 3");
                    return tabList[3];
                case 4:
                    tabList[4] = QuestionView.newInstance("Question 4");
                    return tabList[4];
            }
        }
        return null ;
    }
}

@Override
public void WorkData(String data) {
    Log.d("LOG", " Activity - TEXT   " + data);
}}

QuestionView.java

public class QuestionView extends Fragment {

    OnDataPass dataPasser;   

    private TextView firstText;
    private Button btn;
    RadioGroup rgOpinion;
    ViewPager viewPager;

    public interface OnDataPass  {
        public void WorkData(String data);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view1, container, false);
        firstText = (TextView) view.findViewById(R.id.viewOneText);
        firstText.setText(getArguments().getString("msg"));

        btn = (Button) view.findViewById(R.id.viewOneBtn);
        viewPager = (ViewPager) getActivity().findViewById(R.id.pager);

        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rgOpinion = (RadioGroup) getActivity().findViewById(R.id.radioGroup);
                RadioButton selected = (RadioButton) getActivity().findViewById(rgOpinion.getCheckedRadioButtonId());
                String text = selected.getText().toString();
                Log.d("LOG", " Question view - TEXT  " +text);
                dataPasser.WorkData(text);
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
            }

        });
        return view;
    }

    public static QuestionView newInstance(String text) {
        QuestionView f = new QuestionView();
        Bundle b = new Bundle();
        b.putString("msg", text);
        f.setArguments(b);
        return f;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        dataPasser = (OnDataPass) context;
    }
}

fragment_view1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width = "fill_parent"
        android:layout_height= "fill_parent"
        android:src="@drawable/hintergrundbild"
        android:scaleType="fitXY"
        android:adjustViewBounds="true">
    </ImageView>


    <Button
        android:background="@drawable/red_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="weiter..."
        android:id="@+id/viewOneBtn"
        style="@style/button_text"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:id="@+id/viewOneText"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentTop="true"
        android:background="#3f51b5"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:text="MY PAGE TITLE"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff" />

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:layout_alignTop="@id/viewOneText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="53dp">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:id="@+id/radBut1"
            android:layout_above="@+id/radioButtonAnimationMovies"
            android:layout_alignLeft="@+id/radioButtonAnimationMovies"
            android:layout_alignStart="@+id/radioButtonAnimationMovies"
            android:checked="false" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:id="@+id/radBut2"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:checked="false" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:id="@+id/radBut3"
            android:layout_below="@+id/radioButtonAnimationMovies"
            android:layout_alignLeft="@+id/radioButtonAnimationMovies"
            android:layout_alignStart="@+id/radioButtonAnimationMovies"
            android:checked="false" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:id="@+id/radBut4"
            android:layout_below="@+id/radioButtonHorrorMovies"
            android:layout_alignLeft="@+id/radioButtonHorrorMovies"
            android:layout_alignStart="@+id/radioButtonHorrorMovies"
            android:checked="false" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:id="@+id/radBut5"
            android:layout_below="@+id/radioButtonComedyMovies"
            android:layout_alignLeft="@+id/radioButtonComedyMovies"
            android:layout_alignStart="@+id/radioButtonComedyMovies"
            android:checked="false" />
    </RadioGroup>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/radioGroup"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="449dp"
        android:layout_marginStart="449dp"></FrameLayout>


</RelativeLayout>

【问题讨论】:

    标签: java android android-fragments android-viewpager


    【解决方案1】:

    而不是使用 getActivity(),它将查找整个活动的 Id(不仅仅是当前片段)

    rgOpinion = (RadioGroup) getActivity().findViewById(R.id.radioGroup);
                RadioButton selected = (RadioButton) getActivity().findViewById(rgOpinion.getCheckedRadioButtonId());
    

    尝试像这样使用您之前在 onCreateview() 上定义的视图

    rgOpinion = (RadioGroup) view.findViewById(R.id.radioGroup);
                RadioButton selected = (RadioButton) view.findViewById(rgOpinion.getCheckedRadioButtonId());
    

    【讨论】:

    • 嗨 Cilvet,感谢您的回复。你的评论解释了很多。所以我更改了我的代码并尝试了它,但我在RadioButton selected = (RadioButton) getActivity().findViewById(rgOpinion.getCheckedRadioButtonId()); 行中得到了一个 NullPointerException。但我能感觉到我错在哪里。
    • 你必须使用我发布的第二个代码!不是第一个 xD 抱歉,我之前没有回答。如果没有选中的操作按钮,它可能会因为你要一个而崩溃,所以你必须先检查
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2016-03-22
    • 2015-08-07
    • 2014-03-26
    • 2017-11-19
    • 2017-01-27
    • 1970-01-01
    • 2019-05-29
    相关资源
    最近更新 更多