嗨,我正在制作一个允许用户添加提醒的应用程序,但是当我单击“添加新”按钮时出现错误 - 不幸的是,提醒器已停止。
AddTaskActivity.java 代码:
public class AddTaskActivity extends Activity {
Task mTask;
Handler mHandler = new AddHandler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task);
Button add = (Button) this.findViewById(R.id.submit);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
GrammarParser parser = new GrammarParser();
parser.setAndroidContext(AddTaskActivity.this);
EditText text = (EditText) findViewById(R.id.add_task);
String input = text.getText().toString();
mTask = parser.parse(input);
String output = mTask.toString();
TextView log = (TextView) findViewById(R.id.log_cat);
log.setText(output);
}
});
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mTask != null)
DatabaseInterface.addTask(AddTaskActivity.this, mHandler,
mTask);
}
});
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
}
add_task.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/add_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/add_new_task" />
</LinearLayout>
<TextView
android:id="@+id/log_cat"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:text="@string/no_task"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/save" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/cancel" />
</LinearLayout>
</LinearLayout>
字符串.xml:
<resources>
<string name="hello">Hello World, RemindererActivity!</string>
<string name="app_name">Reminderer</string>
<string name="hello_world">Hello world</string>
<string name="add_new_task">Add new</string>
<string name="submit">Submit</string>
<string name="no_task">Sample tasks:\n
buy eggs\n
buy eggs 8pm\n
buy eggs Monday 9am\n
buy eggs July 1\n
buy eggs next Monday\n
buy eggs 8pm repeats daily</string>
<string name="due_now">These task(s) are due now:</string>
<string name="dismiss">Dismiss</string>
<string name="snooze">Snooze</string>
<string name="save">Save</string>
<string name="cancel">Cancel</string>
Logcat 说:
01-11 16:05:01.252: E/AndroidRuntime(943): FATAL EXCEPTION: main
01-11 16:05:01.252: E/AndroidRuntime(943): java.lang.NullPointerException
01-11 16:05:01.252: E/AndroidRuntime(943): at com.frankandrobot.reminderer.AddTaskActivity$1.onClick(AddTaskActivity.java:39)
01-11 16:05:01.252: E/AndroidRuntime(943): at android.view.View.performClick(View.java:4202)
01-11 16:05:01.252: E/AndroidRuntime(943): at android.view.View$PerformClick.run(View.java:17340)
01-11 16:05:01.252: E/AndroidRuntime(943): at android.os.Handler.handleCallback(Handler.java:725)
01-11 16:05:01.252: E/AndroidRuntime(943): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 16:05:01.252: E/AndroidRuntime(943): at android.os.Looper.loop(Looper.java:137)
01-11 16:05:01.252: E/AndroidRuntime(943): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-11 16:05:01.252: E/AndroidRuntime(943): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 16:05:01.252: E/AndroidRuntime(943): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 16:05:01.252: E/AndroidRuntime(943): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-11 16:05:01.252: E/AndroidRuntime(943): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-11 16:05:01.252: E/AndroidRuntime(943): at dalvik.system.NativeStart.main(Native Method)