其他分享
首页 > 其他分享> > android-如何在进入viewpager中的下一页之前检查条件

android-如何在进入viewpager中的下一页之前检查条件

作者:互联网

在我的应用程序中,我试图使用单选按钮来实现小测验,并且试图在viewpager中实现,现在的问题是我已经在viewpager中显示了问题,并且试图在移至下一页之前检查条件并需要显示基于条件的吐司消息.但是我遇到错误,应用崩溃.
我已经在下面发布了我的代码部分

t_vPager.setOnPageChangeListener(new OnPageChangeListener()
                {

                    @Override
                    public void onPageSelected(int position)
                    {
                        // TODO Auto-generated method stub
                        System.out.println("checking");

                            switch(position)
                    {
                        case 0:

                            System.out.println("question1 ");

                            id=radioanswergroup.getCheckedRadioButtonId();

                            radioanswerButton = (RadioButton)findViewById(id);

                            System.out.println("answer--"+radioanswerButton.getText());

                            break;

                        case 1:

                            System.out.println("question2 ");
                            break;

                        case 2:

                            System.out.println("question3 ");
                            break;

                        case 3:

                            System.out.println("question4 ");
                            break;
                    }

                                }
                        else 
                        {
                            System.out.println("verify the answer");
                        }

                    }

                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) 
                    {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onPageScrollStateChanged(int arg0)
                    {
                        // TODO Auto-generated method stub

                    }
                });

logcat错误

    03-31 13:34:18.743: E/InputEventReceiver(11605): Exception dispatching input event.
03-31 13:34:18.743: E/MessageQueue-JNI(11605): Exception in MessageQueue callback: handleReceiveCallback
03-31 13:34:18.758: E/MessageQueue-JNI(11605): java.lang.NullPointerException
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at com.example.instantenglish.Todaytest$Dailytask$1.onPageSelected(Todaytest.java:234)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:567)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2015)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.View.dispatchTouchEvent(View.java:7350)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2412)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2147)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2139)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1476)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2087)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.View.dispatchPointerEvent(View.java:7535)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3415)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3347)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4456)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4434)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4538)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.os.MessageQueue.nativePollOnce(Native Method)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.os.MessageQueue.next(MessageQueue.java:125)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.os.Looper.loop(Looper.java:124)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at android.app.ActivityThread.main(ActivityThread.java:4921)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at java.lang.reflect.Method.invokeNative(Native Method)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at java.lang.reflect.Method.invoke(Method.java:511)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
03-31 13:34:18.758: E/MessageQueue-JNI(11605):  at dalvik.system.NativeStart.main(Native Method)
03-31 13:34:18.763: D/AndroidRuntime(11605): Shutting down VM
03-31 13:34:18.763: W/dalvikvm(11605): threadid=1: thread exiting with uncaught exception (group=0x41e0b2a0)
03-31 13:34:18.773: E/AndroidRuntime(11605): FATAL EXCEPTION: main
03-31 13:34:18.773: E/AndroidRuntime(11605): java.lang.NullPointerException
03-31 13:34:18.773: E/AndroidRuntime(11605):    at com.example.instantenglish.Todaytest$Dailytask$1.onPageSelected(Todaytest.java:234)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:567)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2015)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.View.dispatchTouchEvent(View.java:7350)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2412)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2147)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2139)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1476)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2087)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.View.dispatchPointerEvent(View.java:7535)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3415)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3347)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4456)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4434)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4538)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.os.MessageQueue.nativePollOnce(Native Method)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.os.MessageQueue.next(MessageQueue.java:125)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.os.Looper.loop(Looper.java:124)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at android.app.ActivityThread.main(ActivityThread.java:4921)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at java.lang.reflect.Method.invokeNative(Native Method)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at java.lang.reflect.Method.invoke(Method.java:511)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
03-31 13:34:18.773: E/AndroidRuntime(11605):    at dalvik.system.NativeStart.main(Native Method)

这是我的完整代码

import java.util.ArrayList;
import java.util.Collections;

import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class Todaytest extends Activity implements OnClickListener
{

    //urls

            String url="link";

            //json array

            JSONArray jarray;

            //Array list

            ArrayList<String> tst_alquestion = new ArrayList<String>();

            ArrayList<String> tst_alcorrectanswer = new ArrayList<String>();

            ArrayList<String> tst_alwronganswer1 = new ArrayList<String>();

            ArrayList<String> tst_alwronganswer2 = new ArrayList<String>();

            ArrayList<String> tst_alwronganswer3 = new ArrayList<String>();

            ArrayList<String> tst_alwronganswer4 = new ArrayList<String>();

            ArrayList<String> shufanswer=new ArrayList<String>();

            //Radio button

            RadioGroup radioanswergroup;

            RadioButton radioanswerButton,btn1,btn2,btn3,btn4;

            int id,qid=0;

            //text view

            TextView question;

            ViewPager t_vPager;

            PagerAdapter t_padapter;


        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            setContentView(R.layout.test);

            t_vPager = (ViewPager) findViewById(R.id.ttpager);

            question=(TextView)findViewById(R.id.qst);

            radioanswergroup=(RadioGroup)findViewById(R.id.radioanswer);

            btn1=(RadioButton)findViewById(R.id.btn1);

            btn2=(RadioButton)findViewById(R.id.btn2);

            btn3=(RadioButton)findViewById(R.id.btn3);

            btn4=(RadioButton)findViewById(R.id.btn4);

            new Dailytask().execute();

            //questionview();


        }



        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub



        }



        private class Dailytask extends AsyncTask<String, String, JSONObject> 
        {
            private ProgressDialog pDialog;

            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();



                pDialog = new ProgressDialog(Todaytest.this);

                pDialog.setMessage("Loading Assessment ...");

                pDialog.setIndeterminate(false);

                pDialog.setCancelable(true);

                pDialog.show();
            }

            @Override
            protected JSONObject doInBackground(String... args) 
            {
                Json j = new Json();
                // Getting JSON from URL
                JSONObject json = j.getJSONFromUrl(url);
                return json;
            }

            @Override
            protected void onPostExecute(JSONObject json)
            {

                System.out.println("---------------return today test json------------"+json);

                pDialog.dismiss();

                try
                {
                        // Getting JSON Array from URL
                    JSONObject jobj = json.getJSONObject("response");

                    jarray = jobj.getJSONArray("Test");

                    System.out.println("json array length"+jarray.length());

                    for (int i = 0; i < jarray.length(); i++) 
                    {
                        JSONObject data = jarray.getJSONObject(i);
                        // Storing JSON item in a Variable
                        tst_alquestion.add(data.getString("question"));

                        tst_alcorrectanswer.add(data.getString("correctanswer"));

                        tst_alwronganswer1.add(data.getString("wronganswer1"));

                        tst_alwronganswer2.add(data.getString("wronganswer2"));

                        tst_alwronganswer3.add(data.getString("wronganswer3"));

                        tst_alwronganswer4.add(data.getString("wronganswer4"));

                        System.out.println("correct answer------------>"+tst_alcorrectanswer);
                        System.out.println("wroang ans1------------>"+tst_alwronganswer1);

                        System.out.println("wroang ans2------------>"+tst_alwronganswer2);
                        System.out.println("wroang ans3------------>"+tst_alwronganswer3);
                        System.out.println("wroang ans4------------>"+tst_alwronganswer4);



                    }


                }
                catch(Exception e)
                {

                }

    t_padapter = new testadapter(Todaytest.this, tst_alquestion,tst_alcorrectanswer ,tst_alwronganswer1,tst_alwronganswer2,tst_alwronganswer3,tst_alwronganswer4);


                t_vPager.setAdapter(t_padapter);

                t_vPager.setOnPageChangeListener(new OnPageChangeListener()
                {

                    @Override
                    public void onPageSelected(int position)
                    {
                        // TODO Auto-generated method stub
                        System.out.println("checking"+position);
                        //Toast.makeText(getApplicationContext(), "please answer for above questions",Toast.LENGTH_SHORT ).show();

                        switch(position)
                        {
                            case 0:

                                System.out.println("question1 ");

                                id=radioanswergroup.getCheckedRadioButtonId();

                                radioanswerButton = (RadioButton)findViewById(id);

                                System.out.println("answer--"+radioanswerButton.getText());

                                break;

                            case 1:

                                System.out.println("question2 ");
                                break;

                            case 2:

                                System.out.println("question3 ");
                                break;

                            case 3:

                                System.out.println("question4 ");
                                break;
                        }




                    }

                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) 
                    {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onPageScrollStateChanged(int arg0)
                    {
                        // TODO Auto-generated method stub

                    }
                });


            }
        }



}

test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:background="#F5DA81" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="15dp"
        android:background="#FFFFFF"
        android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/ttpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

// testadapter

package com.example.instantenglish;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.apache.http.util.ByteArrayBuffer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcelable;
import android.os.StrictMode;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class testadapter extends PagerAdapter
{
    // Declare Variables
    Context context;
    ArrayList<String> question;
    ArrayList<String> answer;
    ArrayList<String> ans1;
    ArrayList<String> ans2;
    ArrayList<String> ans3;
    ArrayList<String> ans4;

    int[] flag;
    LayoutInflater inflater;


    public testadapter(Todaytest tt, ArrayList<String> tst_alquestion,
            ArrayList<String> tst_alcorrectanswer,
            ArrayList<String> tst_alwronganswer1,
            ArrayList<String> tst_alwronganswer2,
            ArrayList<String> tst_alwronganswer3,
            ArrayList<String> tst_alwronganswer4) 
    {
        // TODO Auto-generated constructor stub
        this.context=tt;
        this.question=tst_alquestion;
        this.answer=tst_alcorrectanswer;
        this.ans1=tst_alwronganswer1;
        this.ans2=tst_alwronganswer2;
        this.ans3=tst_alwronganswer3;
        this.ans4=tst_alwronganswer4;
    }

        @Override
    public int getCount() {
        return question.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {

        // Declare Variables
        TextView txtquestion;
        RadioButton radioanswerButton,btn1,btn2,btn3,btn4,btn5;        //TextView txtpopulation;
        RadioGroup radioanswergroup;   

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.todaytest, container,
                false);

        // Locate the TextViews in viewpager_item.xml
        txtquestion = (TextView) itemView.findViewById(R.id.qst);
        radioanswergroup=(RadioGroup)itemView.findViewById(R.id.radioanswer);

        btn1=(RadioButton)itemView.findViewById(R.id.btn1);

        btn2=(RadioButton)itemView.findViewById(R.id.btn2);

        btn3=(RadioButton)itemView.findViewById(R.id.btn3);

        btn4=(RadioButton)itemView.findViewById(R.id.btn4);

        btn5=(RadioButton)itemView.findViewById(R.id.btn5);


        // Capture position and set to the TextViews
        txtquestion.setText(question.get(position));
        btn1.setText(answer.get(position));
        btn2.setText(ans1.get(position));
        btn3.setText(ans2.get(position));
        btn4.setText(ans3.get(position));
        btn5.setText(ans4.get(position));
        //txtpopulation.setText(population[position]);

        // Locate the ImageView in viewpager_item.xml

        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((LinearLayout) object);

    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
    }

    @Override
    public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

    }   

  }

testadapter xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
     android:background="#F5DA81">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#FFFFFF" >

        <TextView
            android:id="@+id/qst"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:background="@drawable/txtview"
            android:gravity="center"
            android:text="Questions"
            android:textColor="#000000"
            android:textStyle="bold" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="25dp" >

                <RadioGroup
                    android:id="@+id/radioanswer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >


                <RadioButton
                    android:id="@+id/btn1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/rbtn1" 
                    android:textColor="#000000"/>

                <RadioButton
                    android:id="@+id/btn2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:text="@string/rbtn2"
                    android:textColor="#000000" />

                <RadioButton
                    android:id="@+id/btn3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:text="@string/rbtn3"
                    android:textColor="#000000" />

                  <RadioButton
                      android:id="@+id/btn4"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="15dp"
                      android:text="@string/rbtn4"
                      android:textColor="#000000" />

                  <RadioButton
                      android:id="@+id/btn5"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="15dp"
                      android:text="@string/rbtn5"
                      android:textColor="#000000" />

                 </RadioGroup>



        </TableRow>

    </LinearLayout>

</LinearLayout>

解决方法:

如您的详细信息所示,您正在使用getCheckedRadioButtonId(),但是在您的xml中未选中RadioButton.所以只需检查如下的RedioButton

<RadioButton
       android:id="@+id/btn3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="15dp"
       android:text="@string/rbtn3"
       android:checked="true"
       android:textColor="#000000" />

并在获取带有选中ID的按钮之前使用条件.使用如下

int id=radioanswergroup.getCheckedRadioButtonId();
if(id>0)  
radioanswerButton = (RadioButton)findViewById(id); 

标签:android-viewpager,radio-group,android,radio-button
来源: https://codeday.me/bug/20191121/2055432.html