sqlite データベースにレコードを追加しようとしています。「追加」をクリックすると、DialogFragment が開きます。次に、テキストフィールドに情報を書き込み、スピナーでオプションを 1 つ選択すると、それが sqlite データベースに追加されます。これは、メインアクティビティから実行すると機能します。しかし、私はDialogFragmentから同じことをしようとしたときにのみFCを取得します。
DialogFragment コード:
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)
どんな助けでも大歓迎です!