0

嗨,我正在制作一个允许用户添加提醒的应用程序,但是当我单击“添加新”按钮时出现错误 - 不幸的是,提醒器已停止。

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)
4

1 回答 1

2

很难判断哪一行onClick()导致NPE,因为您没有提供行号。您需要在代码中执行更多错误检查,并assert()更频繁地使用:

@Override
public void onClick(View arg0) {
    GrammarParser parser = new GrammarParser();
    parser.setAndroidContext(AddTaskActivity.this);

    EditText text = (EditText) findViewById(R.id.add_task);
    assert(text != null);
    String input = text.getText().toString();
    mTask = parser.parse(input);
    if (mTask != null) {
        String output = mTask.toString();
        TextView log = (TextView) findViewById(R.id.log_cat);
        assert(log != null);
        log.setText(output);
    }
    else {
        Log.e(TAG, "Failed to parse text");
    }
}
于 2013-01-11T08:05:36.540 回答