2013-01-11 102 views
0

嗨,我想提出一個應用程序中,我允許用戶添加一個提醒,可是當我做點擊添加新按鈕收到錯誤 - 不幸的是Reminderer已經停止。不幸的是Reminderer已停止

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> 

的strings.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) 
+0

您對AddTaskActivity.java的線39 NullPointerException異常。你能告訴你的示例代碼中哪一行是? – Veger

+0

你可以發佈你的佈局文件'add_task'嗎? – GrIsHu

+0

@Grishu我發佈了add_task.xml – Liza

回答

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"); 
    } 
} 
+1

@ Liza你需要發佈'Parser.Task.toString()',但看起來你正在變成一個幫助吸血鬼。如果我的答案有助於解決您的原始問題,那麼使用類似的方法可能會解決這個新問題。無論哪種方式,我們都不會花更多的時間更新這個問題和我對套件的回答。 – trojanfoe