我是Java / Android编程的新手,我正在使用AlertDialog,我希望存储两个字符串并增加一个计数器,然后将其存储在SharedPreferences中。
AlertDialog
public boolean createEvent(MenuItem item){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.create_new, null))
.setTitle("Add A Custom Event")
// Add action buttons
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText dateBoxBox = (EditText)findViewById(R.id.dateBox); //error thrown here
EditText nameBoxBox = (EditText)findViewById(R.id.nameBox);
String nameString = nameBoxBox.getText().toString();
String dateString = dateBoxBox.getText().toString();
eventAdd(nameString, dateString);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do nothing
}
})
.show();
return true;
}
create_new.xml
<EditText
android:id="@+id/nameBox"
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Name" />
<EditText
android:id="@+id/dateBox"
android:inputType="date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:hint="Date"/>
这是我在尝试将这些东西提交给SharedPreferences时调用的函数
eventAdd()
public void eventAdd(String sName, String sDate) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
int iEventCounter = settings.getInt("eventCounter", 0);
editor.putInt("eventCounter", iEventCounter+1);
editor.putString("event1Name", sName);
editor.putString("event1Date", sDate);
// Commit the edits!
editor.commit();
TextView testTextView = (TextView)findViewById(R.id.section_label);
testTextView.setText(sName);
}
我可以看到logcat中出现错误的位置,但无法弄清楚它为什么会这样做。
logcat的
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.cistoran.electriccountdown.MainActivity$2.onClick(MainActivity.java:124)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5270)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:974)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:790)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
final View view = inflater.inflate(R.layout.create_new, null)
builder.setView(view);
//and add other settings you've done builder.set...
更改
EditText dateBoxBox = (EditText)findViewById(R.id.dateBox);
EditText nameBoxBox = (EditText)findViewById(R.id.nameBox);
要
EditText dateBoxBox = (EditText)view.findViewById(R.id.dateBox);
EditText nameBoxBox = (EditText)view.findViewById(R.id.nameBox);