完成活动的例外情况

时间:2013-01-13 18:11:48

标签: android android-intent

我列出了一些项目&每当我点击其中任何一个时,我都会相应地转到新的意图。但是当我尝试再次返回列表时,一些项目正在给出正确的结果,而其他项目则给出错误。

我已经尝试在onDestroy&中调用finish()在给出错误的意图中的onStop方法。我也尝试过评论这两种方法,因为默认情况下应该没有堆栈。但LogCat显示

01-12 23:04:55.050: E/AndroidRuntime(14445): java.lang.RuntimeException: Unable to stop    activity {com.balance.start/com.balance.start.GFX}: java.lang.NullPointerException
01-12 23:04:55.050: E/AndroidRuntime(14445):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3613)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at     android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3679)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at android.app.ActivityThread.access$2900(ActivityThread.java:126)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at android.os.Looper.loop(Looper.java:123)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at android.app.ActivityThread.main(ActivityThread.java:4633)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at java.lang.reflect.Method.invokeNative(Native Method)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at java.lang.reflect.Method.invoke(Method.java:521)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at dalvik.system.NativeStart.main(Native Method)
01-12 23:04:55.050: E/AndroidRuntime(14445): Caused by: java.lang.NullPointerException
01-12 23:04:55.050: E/AndroidRuntime(14445):    at android.app.Activity.performStop(Activity.java:3869)
01-12 23:04:55.050: E/AndroidRuntime(14445):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3608)

这是我的代码..

 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.report_ledger);
        ...
    }

class MyAdapter extends CursorAdapter {
    public MyAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View view, Context ctxt, Cursor c) {
        // TODO Auto-generated method stub

        TextView tv1 = (TextView) view.findViewById(R.id.tvAB1);
        TextView tv2 = (TextView) view.findViewById(R.id.tvAB2);
        TextView tv3 = (TextView) view.findViewById(R.id.tvAB3);
        TextView tv4 = (TextView) view.findViewById(R.id.tvAB4);
        TextView tv5 = (TextView) view.findViewById(R.id.tvAB5);
        tv1.setText(c.getString(2));
        tv2.setText("" + c.getDouble(3));
        tv3.setText("" + c.getDouble(4));
        tv4.setText("" + (c.getDouble(4) - c.getDouble(3)));
        tv5.setText((c.getDouble(3) > c.getDouble(4) ? "CR." : "DR."));
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = getLayoutInflater();
        return inflater.inflate(
                R.layout.text_view_for_list_view_account_balance, parent,
                false);
    }
}

我没有为后退按钮实现onClickListener,因为我不认为这是必需的。我认为系统应该自己完成活动。 :/

感谢。

1 个答案:

答案 0 :(得分:0)

我恐怕你在finish()打电话给onDestroy()。实际上,当您想要finish()您的活动时需要调用finish(),其中onDestroy()方法会自动调用。如果需要,您可以在onDestroy()中使您的意图或其他对象为空。