我有一个带有单个编辑文本视图的编辑任务活动。当我单击操作栏上的后退按钮时,数据库会更新并且日志甚至会对其进行注册,但是在返回时活动会崩溃。主活动确实显示了在任务活动中添加的文本。这是主页按钮的代码:
public class NewTask extends Activity {
protected TaskerDbHelper db;
MyAdapter adapt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_task);
db = new TaskerDbHelper(this);
setupActionBar();
}
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_task, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
EditText t = (EditText) findViewById(R.id.editText1);
String s = t.getText().toString();
if (s.equalsIgnoreCase("")) {
Toast.makeText(this, "enter the task description first!!",
Toast.LENGTH_LONG);
} else {
Task task = new Task(s, 0);
db.addTask(task);
Log.d("tasker", "data added");
//t.setText("");
//adapt.add(task);
adapt.notifyDataSetChanged();
}
setResult(RESULT_OK);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
怎么了?
这是日志:
08-16 17:56:05.117: E/AndroidRuntime(6162): FATAL EXCEPTION: main
08-16 17:56:05.117: E/AndroidRuntime(6162): java.lang.NullPointerException
08-16 17:56:05.117: E/AndroidRuntime(6162): at com.example.tasker.NewTask.onOptionsItemSelected(NewTask.java:62)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.app.Activity.onMenuItemSelected(Activity.java:2552)
08-16 17:56:05.117: E/AndroidRuntime(6162): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.view.View.performClick(View.java:4204)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.view.View$PerformClick.run(View.java:17355)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.os.Handler.handleCallback(Handler.java:725)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.os.Handler.dispatchMessage(Handler.java:92)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.os.Looper.loop(Looper.java:137)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.app.ActivityThread.main(ActivityThread.java:5234)
08-16 17:56:05.117: E/AndroidRuntime(6162): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:56:05.117: E/AndroidRuntime(6162): at java.lang.reflect.Method.invoke(Method.java:525)
08-16 17:56:05.117: E/AndroidRuntime(6162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
08-16 17:56:05.117: E/AndroidRuntime(6162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
08-16 17:56:05.117: E/AndroidRuntime(6162): at dalvik.system.NativeStart.main(Native Method)