向sqlite添加数据时出现java.lang.NullPointerException(因为RadioGroup)[重复]
Posted
技术标签:
【中文标题】向sqlite添加数据时出现java.lang.NullPointerException(因为RadioGroup)[重复]【英文标题】:java.lang.NullPointerException when adding data to sqlite (because of RadioGroup) [duplicate] 【发布时间】:2016-04-03 14:45:48 【问题描述】:向 SQLite 插入数据一直有效,直到我决定添加 RadioGroup。运行时我收到:
12-29 11:52:19.948: E/androidRuntime(21086): java.lang.NullPointerException
12-29 11:52:19.948: E/AndroidRuntime(21086): at com.example.appointapp.PatientFragment$1.onClick(PatientFragment.java:66)
12-29 11:52:19.948: E/AndroidRuntime(21086): at android.view.View.performClick(View.java:4438)
12-29 11:52:19.948: E/AndroidRuntime(21086): at android.view.View$PerformClick.run(View.java:18422)
12-29 11:52:19.948: E/AndroidRuntime(21086): at android.os.Handler.handleCallback(Handler.java:733)
12-29 11:52:19.948: E/AndroidRuntime(21086): at android.os.Handler.dispatchMessage(Handler.java:95)
12-29 11:52:19.948: E/AndroidRuntime(21086): at android.os.Looper.loop(Looper.java:136)
12-29 11:52:19.948: E/AndroidRuntime(21086): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-29 11:52:19.948: E/AndroidRuntime(21086): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 11:52:19.948: E/AndroidRuntime(21086): at java.lang.reflect.Method.invoke(Method.java:515)
12-29 11:52:19.948: E/AndroidRuntime(21086): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-29 11:52:19.948: E/AndroidRuntime(21086): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-29 11:52:19.948: E/AndroidRuntime(21086): at dalvik.system.NativeStart.main(Native Method)
utearfreagement.java代码:
package com.example.appointapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class PatientFragment extends Fragment
DatabaseHelper myDB;
EditText editName,editFamilyname,editDob,editAddress,editPhonenb,editEmail;
Button btnSave, selectedRadioButton;
RadioButton radioB;
RadioGroup radioG;
private Patient mPatient;
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
mPatient= new Patient();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_patient, parent, false);
myDB = new DatabaseHelper(getActivity());
editName = (EditText)v.findViewById(R.id.name_text);
editFamilyname = (EditText)v.findViewById(R.id.familyname_text);
editDob = (EditText)v.findViewById(R.id.date_text);
editAddress = (EditText)v.findViewById(R.id.address_text);
editPhonenb = (EditText)v.findViewById(R.id.phone_text);
editEmail = (EditText)v.findViewById(R.id.email_text);
btnSave = (Button)v.findViewById(R.id.save_button);
radioG = (RadioGroup)v.findViewById(R.id.myRadioGroup);
int selectedId = radioG.getCheckedRadioButtonId();
if(selectedId != -1)
selectedRadioButton = (RadioButton)v.findViewById(selectedId);
AddData();
return v;
public void AddData()
btnSave.setOnClickListener(
new View.OnClickListener()
@Override
public void onClick(View v)
boolean isInserted = myDB.insertData(editName.getText().toString() ,
editFamilyname.getText().toString() ,
editDob.getText().toString() ,
editAddress.getText().toString() ,
editPhonenb.getText().toString() ,
editEmail.getText().toString(),
selectedRadioButton.getText().toString()) ;
if(isInserted == true)
Toast.makeText(getActivity(), "Patient inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(getActivity(), "Patient not inserted", Toast.LENGTH_LONG).show();
);
和fragment_patient.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal" >
<EditText
android:id="@+id/name_text"
android:layout_
android:layout_
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:hint="@string/name_text_hint" />
<EditText
android:id="@+id/familyname_text"
android:layout_
android:layout_
android:layout_marginLeft="16dp"
android:hint="@string/familyname_text_hint" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal" >
<EditText
android:id="@+id/date_text"
android:layout_
android:layout_
android:ems="10"
android:hint="@string/date_text_hint" >
</EditText>
</LinearLayout>
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_
android:layout_
android:layout_below="@+id/text"
android:checkedButton="@+id/male_text" >
<RadioButton
android:id="@+id/female_text"
android:layout_
android:layout_
android:text="@string/female_text" />
<RadioButton
android:id="@+id/male_male_text"
android:layout_
android:layout_
android:text="@string/male_text" />
</RadioGroup>
<EditText
android:id="@+id/address_text"
android:layout_
android:layout_
android:ems="10"
android:hint="@string/address_text_hint" />
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal" >
<EditText
android:id="@+id/phone_text"
android:layout_
android:layout_
android:hint="@string/phone_text_hint" />
<EditText
android:id="@+id/email_text"
android:layout_
android:layout_
android:hint="@string/email_text_hint" />
</LinearLayout>
<Button
android:id="@+id/save_button"
android:layout_
android:layout_
android:layout_gravity="right"
android:text="@string/save_button" />
</LinearLayout>
and databasehelper.java code:
package com.example.appointapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper
public static final String DATABASE_NAME="appo.db";
public static final String TABLE_NAME = "patients_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "FAMILYNAME";
public static final String COL_4 = "DOB";
public static final String COL_5 = "ADDRESS";
public static final String COL_6 = "PHONENUMBER";
public static final String COL_7 = "EMAIL";
public static final String COL_8 = "GENDER";
public DatabaseHelper(Context context)
super(context, DATABASE_NAME, null, 1);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL( "create table "+ TABLE_NAME + "( ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, FAMILYNAME TEXT, DOB TEXT, ADDRESS TEXT, PHONENUMBER TEXT, EMAIL TEXT, GENDER TEXT )" );
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
public boolean insertData(String name, String familyname, String dob, String address, String phonenumber, String email, String gender)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,familyname);
contentValues.put(COL_4,dob);
contentValues.put(COL_5,address);
contentValues.put(COL_6,phonenumber);
contentValues.put(COL_7,email);
contentValues.put(COL_8,gender);
long result = db.insert(TABLE_NAME, null , contentValues);
if(result == -1)
return false;
else
return true;
请帮助我是一个新的程序员。请注意,在尝试将 RadioGroup 的结果添加到数据库之前,我的应用程序正在运行。 提前谢谢你。
【问题讨论】:
selectedRadioButton
是 null
以使其工作在任何 RadioButton xml 中添加 android:checked="true"
以在 RadioGroup
中设置默认选择的 RadioButton
非常感谢。所以我现在不能投票,因为我是新的 span>
【参考方案1】:
您需要在将数据保存到数据库时获取当前选中的 RadioButton。 否则,如果用户做出与默认不同的选择,您将永远无法捕捉到用户的选择。
启动时不需要对当前选中的 RadioButton 做任何事情:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_patient, parent, false);
myDB = new DatabaseHelper(getActivity());
editName = (EditText)v.findViewById(R.id.name_text);
editFamilyname = (EditText)v.findViewById(R.id.familyname_text);
editDob = (EditText)v.findViewById(R.id.date_text);
editAddress = (EditText)v.findViewById(R.id.address_text);
editPhonenb = (EditText)v.findViewById(R.id.phone_text);
editEmail = (EditText)v.findViewById(R.id.email_text);
btnSave = (Button)v.findViewById(R.id.save_button);
radioG = (RadioGroup)v.findViewById(R.id.myRadioGroup);
//remove from here:
/*
int selectedId = radioG.getCheckedRadioButtonId();
if(selectedId != -1)
selectedRadioButton = (RadioButton)v.findViewById(selectedId);
*/
AddData();
return v;
获取已删除的代码,并将其移至 AddData() 方法中的 OnClickListener:
public void AddData()
btnSave.setOnClickListener(
new View.OnClickListener()
@Override
public void onClick(View v)
int selectedId = radioG.getCheckedRadioButtonId();
if(selectedId != -1)
selectedRadioButton = (RadioButton)v.findViewById(selectedId);
boolean isInserted = myDB.insertData(editName.getText().toString() ,
editFamilyname.getText().toString() ,
editDob.getText().toString() ,
editAddress.getText().toString() ,
editPhonenb.getText().toString() ,
editEmail.getText().toString(),
selectedRadioButton.getText().toString()) ;
if(isInserted == true)
Toast.makeText(getActivity(), "Patient inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(getActivity(), "Patient not inserted", Toast.LENGTH_LONG).show();
);
【讨论】:
感谢您的帮助和解释 @JavaProgrammer 当然,没问题!您的问题现在解决了吗? 很抱歉给您带来不便。首先,在我的 xml 中,male_male_text 应该是 male_text。第二,谢谢你的解释,你是对的,但是在移动代码时,我又得到了 java.lang.NullPointerException。实际上在链接中:***.com/questions/15702255/…。有一个编辑,但我无法访问教程的链接。如果可以,请提供帮助。以上是关于向sqlite添加数据时出现java.lang.NullPointerException(因为RadioGroup)[重复]的主要内容,如果未能解决你的问题,请参考以下文章
Netbeans 连接到 SQLite 时出现 SQLite 错误或缺少数据库(没有这样的表)
尝试在 SQLITE DATABASE 中插入 API DATA 时出现问题