向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)
PatientFragement.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_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="54dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/name_text"
android:layout_width="143dp"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:hint="@string/familyname_text_hint" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/date_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:hint="@string/date_text_hint" >
</EditText>
</LinearLayout>
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:checkedButton="@+id/male_text" >
<RadioButton
android:id="@+id/female_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female_text" />
<RadioButton
android:id="@+id/male_male_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male_text" />
</RadioGroup>
<EditText
android:id="@+id/address_text"
android:layout_width="348dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/address_text_hint" />
<LinearLayout
android:layout_width="340dp"
android:layout_height="57dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/phone_text"
android:layout_width="143dp"
android:layout_height="wrap_content"
android:hint="@string/phone_text_hint" />
<EditText
android:id="@+id/email_text"
android:layout_width="213dp"
android:layout_height="wrap_content"
android:hint="@string/email_text_hint" />
</LinearLayout>
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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的结果添加到数据库之前,我的应用程序正在运行。 提前谢谢。
答案 0 :(得分:0)
您需要在将数据保存到数据库时获取当前选定的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();
}
}
}
);
}