回家后活动崩溃了

时间:2013-08-16 12:37:05

标签: java android database

我有一个带有单个edittext视图的编辑任务活动。当我单击操作栏上的后退按钮时,数据库会更新,日志甚至会注册它,但活动会在返回时崩溃。主要活动确实显示在任务活动中添加的文本。 这是主页按钮的代码:

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)

3 个答案:

答案 0 :(得分:2)

尝试检查适配器是否为空

if(adapt != null) {
    adapt.notifyDataSetChanged();
} else {
   // Initialize and do other stuff with it
}

在声明中,声明像这样适应

public MyAdapter adapt;

我可以看到你使用自定义适配器类。我认为问题也可能出现在notifyDataSetChanged()方法中。你可以发布吗?

答案 1 :(得分:0)

if(adapt != null) {
    adapt.notifyDataSetChanged();
} else {
    adapt = new Adapter();
    listview.setAdapter(adapt);
}

答案 2 :(得分:0)

检查诺亚的答案。他重写了notify()以访问BaseAdapter

https://stackoverflow.com/a/5327197/2074990