Java 我想从单选按钮中获取文本并将其存储在sqlite数据库中,但我遇到了一个错误

Java 我想从单选按钮中获取文本并将其存储在sqlite数据库中,但我遇到了一个错误,java,android,sqlite,Java,Android,Sqlite,我想从单选按钮获取文本,但出现错误 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.widget.RadioGroup.getCheckedRadioButtonId()” 请告诉我如何从单选按钮获取文本并删除NullPointerException 这是密码 public class BookActivity extends AppCompatActivity { EditText editText4;

我想从单选按钮获取文本,但出现错误

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.widget.RadioGroup.getCheckedRadioButtonId()”

请告诉我如何从单选按钮获取文本并删除
NullPointerException

这是密码

public class BookActivity extends AppCompatActivity  {

    EditText editText4;
    EditText editText6;
    RadioGroup time;
    Button book;
    RadioButton radioButton;

    //RadioButton radioButton, radioButton2, radioButton3, radioButton4, radioButton5, radioButton6, radioButton7, radioButton8, radioButton9;
    Context context = this;
    UserDbHelper userDbHelper;
    SQLiteDatabase sqLiteDatabase;

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

        editText4 = (EditText) findViewById(R.id.editText4);
        editText6 = (EditText) findViewById(R.id.editText6);
        time=(RadioGroup)findViewById(R.id.time);
        book=(Button)findViewById(R.id.book);

        addListenerOnButton();
    }


    public void addListenerOnButton() {
        time = (RadioGroup) findViewById(R.id.radio);
        book = (Button) findViewById(R.id.book);

        book.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // get selected radio button from radioGroup
                int selectedId = time.getCheckedRadioButtonId();

                // find the radiobutton by returned id
                radioButton = (RadioButton) findViewById(selectedId);
                if (radioButton != null) {
                    String pname = editText4.getText().toString();
                    String rtime = radioButton.getText().toString();

                    userDbHelper = new UserDbHelper(context);
                    sqLiteDatabase = userDbHelper.getWritableDatabase();
                    userDbHelper.addInformations(pname, rtime, sqLiteDatabase);
                    Toast.makeText(BookActivity.this, "Data Saved", Toast.LENGTH_LONG).show();
                    userDbHelper.close();
                }

                //Toast.makeText(BookActivity.this,radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
这是航海日志

10-23 01:04:35.607 16858-16858/com.example.hp.appointit D/AndroidRuntime: Shutting down VM
---- Beginning of crash
10-23 01:04:35.621 16858-16858/com.example.hp.appointit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hp.appointit, PID: 16858
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
    at com.example.hp.appointit.BookActivity$1.onClick(BookActivity.java:54)
    at android.view.View.performClick(View.java:4785)
    at android.view.View$PerformClick.run(View.java:19884)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
10-23 01:09:35.642 16858-16858/com.example.hp.appointit I/Process: Sending signal. PID: 16858 SIG: 9
10-23 01:09:36.449 22673-22673/com.example.hp.appointit W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-23 01:09:36.737 22673-22692/com.example.hp.appointit I/art: Background sticky concurrent mark sweep GC freed 8864(1707KB) AllocSpace objects, 1(16KB) LOS objects, 0% free, 16MB/16MB, paused 12.340ms total 74.187ms
10-23 01:09:36.944 22673-22673/com.example.hp.appointit E/DATABASE OPERATIONS: Database created / opened
10-23 01:09:36.985 22673-22714/com.example.hp.appointit D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-23 01:09:36.996 22673-22673/com.example.hp.appointit D/Atlas: Validating map...
10-23 01:09:37.034 22673-22714/com.example.hp.appointit I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016_msm8226_LA.BF.1.1.1_RB1__release_AU ()
    OpenGL ES Shader Compiler Version: E031.25.03.00
    Build Date: 02/11/15 Wed
    Local Branch: 
    Remote Branch: quic/LA.BF.1.1.1_rb1.10
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016 + 62ca4eb + acd831d + 9f8b442 + e027a02 + cba30ba + 53c303a + a649d79 + 23e16f8 + 5e97da7 + cbd2a44 + 33d072a + 7aacf06 + 72b33e7 + 28f6f60 + b4c13d8 +  NOTHING
10-23 01:09:37.036 22673-22714/com.example.hp.appointit I/OpenGLRenderer: Initialized EGL, version 1.4
10-23 01:09:37.060 22673-22714/com.example.hp.appointit D/OpenGLRenderer: Enabling debug mode 0
10-23 01:04:35.607 16858-16858/com.example.hp.appointit D/AndroidRuntime:关闭虚拟机
----坠机开始
10-23 01:04:35.621 16858-16858/com.example.hp.appointit E/AndroidRuntime:FATAL EXCEPTION:main
流程:com.example.hp.appointit,PID:16858
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.widget.RadioGroup.getCheckedRadioButtonId()”
在com.example.hp.appointit.BookActivity$1.onClick(BookActivity.java:54)上
在android.view.view.performClick上(view.java:4785)
在android.view.view$PerformClick.run(view.java:19884)
位于android.os.Handler.handleCallback(Handler.java:739)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5343)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:905)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
10-23 01:09:35.642 16858-16858/com.example.hp.appointit I/流程:发送信号。PID:16858信号:9
10-23 01:09:36.449 22673-22673/com.example.hp.appointit W/art:Android 4.1之前的方法Android.graphics.PorterDuffColorFilter Android.support.graphics.drawable.VectorDrawableCompat.UpdatentFilter(Android.graphics.PorterDuffColorFilter,Android.content.res.ColorStateList,Android.graphics.PorterDuff$)将错误地重写android.graphics.drawable.drawable中的包私有方法
10-23 01:09:36.737 22673-22692/com.example.hp.appointit I/art:背景粘性并发标记扫描GC释放8864(1707KB)AllocSpace对象,1(16KB)LOS对象,0%空闲,16MB/16MB,暂停12.340ms总计74.187ms
10-23 01:09:36.944 22673-22673/com.example.hp.appointit E/数据库操作:创建/打开数据库
10-23 01:09:36.985 22673-22714/com.example.hp.appointit D/OpenGLRenderer:使用EGL交换行为保存:true
10-23 01:09:36.996 22673-22673/com.example.hp.appointit D/Atlas:正在验证地图。。。
10-23 01:09:37.034 22673-22714/com.example.hp.appointit I/Adreno EGL::EGL 1.4高通公司构建:AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016_msm8226_LA.BF.1.1.1_RB1_release_AU()
OpenGL ES着色器编译器版本:E031.25.03.00
建造日期:2015年11月2日星期三
本地分行:
远程分支机构:quic/LA.BF.1.1_rb1.10
本地修补程序:无
重构分支:AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016+62ca4eb+acd831d+9f8b442+e027a02+cba30ba+53c303a+a649d79+23e16f8+5e97da7+CBD2A4+33d072a+7aacf06+72b33e7+28f6f60+b4c13d8+NOTHING
10-23 01:09:37.036 22673-22714/com.example.hp.appointit I/OpenGLRenderer:初始化EGL,版本1.4
10-23 01:09:37.060 22673-22714/com.example.hp.appointit D/OpenGLRenderer:启用调试模式0

问题可能在RadioGroup时间变量中
因为首先将id设置为R.id.time,然后在addListener函数中将id更改为R.id.radio.check在xml中正确的RadioGroup id。当然有帮助。谢谢

为什么对可变时间有两种不同的定义?你使用R.id.time和R.id.radio。