【发布时间】:2013-11-29 14:08:09
【问题描述】:
我是 Java/Android 编程新手,我正在使用 AlertDialog,我想存储两个字符串并增加一个计数器,然后将其存储在 SharedPreferences 中。
警报对话框
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)
【问题讨论】:
-
@xBroak 哎呀!抱歉,我知道我忘记了什么。
标签: java android error-handling nullpointerexception android-alertdialog