我试图将记录添加到我的 sqlite 数据库。如果单击“添加”,则会打开一个 DialogFragment。然后,当您将一些信息写入文本字段并在 Spinner 中选择一个选项时,它应该被添加到 sqlite 数据库中。如果我从主要活动中执行此操作,则此方法有效。但是我只有在尝试从 DialogFragment 中获取相同内容时才获得 FC。
对话框片段代码:
public class CustomDialogFragment extends DialogFragment {
Spinner spin;
EditText etNumber, etForwardNumber;
Boolean initialDisplay = true;
Database mDB = new Database(getActivity());
Cursor c;
public void DialogFragment(){
// Empty constructor required for DialogFragment
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.activity_dialog, null);
etNumber = (EditText)view.findViewById(R.id.etNumber);
etForwardNumber = (EditText)view.findViewById(R.id.etForwardNumber);
etNumber.setHint("Phone Number");
etForwardNumber.setHint("Forward Number");
etForwardNumber.setVisibility(View.GONE);
spin = (Spinner)view.findViewById(R.id.spOptions);
List<String> list = new ArrayList<String>();
list.add("Block Call");
list.add("Forward Call");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(dataAdapter);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.d("DIALOGFRAGMENT", "Option selected: " + position);
if (position == 0) {
Log.d("LOG_TAG", "Block");
etForwardNumber.setVisibility(View.GONE);
//Log.d("LOG_TAG", initialDisplay.toString());
//initialDisplay = false;
}else {
Log.d("LOG_TAG", "Forward");
etForwardNumber.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
builder.setMessage("")
.setTitle("Add a new number")
.setView(view);
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Hide the dialog
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Add records to sqlite database
mDB.open();
String cname = "test";
String caction = "Forward";
//String cnumber = "Numbertoforward";
ContentValues cv = new ContentValues();
cv.put("cName", cname);
cv.put("cAction", caction);
//cv.put("cNumber", cnumber);
mDB.empInsert(cv);
c.requery();
}
});
return builder.create();
}
/*@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the XML view for the help dialog fragment
//View view = inflater.inflate(R.layout.activity_dialog, container);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
/*builder.setMessage("Test msg")
.setTitle("Title");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});*/
/*return view;
} */
}
日志猫:
10-23 15:06:00.125: E/AndroidRuntime(14935): FATAL EXCEPTION: main
10-23 15:06:00.125: E/AndroidRuntime(14935): java.lang.NullPointerException
10-23 15:06:00.125: E/AndroidRuntime(14935): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
10-23 15:06:00.125: E/AndroidRuntime(14935): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
10-23 15:06:00.125: E/AndroidRuntime(14935): at com.spxc.forwardcalls.Database.open(Database.java:23)
10-23 15:06:00.125: E/AndroidRuntime(14935): at com.spxc.forwardcalls.CustomDialogFragment$3.onClick(CustomDialogFragment.java:90)
10-23 15:06:00.125: E/AndroidRuntime(14935): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-23 15:06:00.125: E/AndroidRuntime(14935): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 15:06:00.125: E/AndroidRuntime(14935): at android.os.Looper.loop(Looper.java:137)
10-23 15:06:00.125: E/AndroidRuntime(14935): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-23 15:06:00.125: E/AndroidRuntime(14935): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 15:06:00.125: E/AndroidRuntime(14935): at java.lang.reflect.Method.invoke(Method.java:525)
10-23 15:06:00.125: E/AndroidRuntime(14935): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-23 15:06:00.125: E/AndroidRuntime(14935): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-23 15:06:00.125: E/AndroidRuntime(14935): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:112)
10-23 15:06:00.125: E/AndroidRuntime(14935): at dalvik.system.NativeStart.main(Native Method)
任何帮助深表感谢!