zoukankan      html  css  js  c++  java
  • Android 应用按返回键异常退出的问题

    开发过程中遇到按返回键异常退出的问题,log显示为空指针异常,进一步产看是由于onActivityResult得到的Intent为空。

    按返回键复写代码例如以下:

            @Override
    	public void onBackPressed() {
    		super.onBackPressed();
    		Intent intent = new Intent();
    		intent.putExtra("id", id);
    		intent.putExtra("path", path);
    		setResult(RESULT_CANCELED, intent);
    		finish();
    		
    	}

    查看Activity源代码发现onBackPressed的默认实现例如以下:

        /** 
         * Called when the activity has detected the user's press of the back 
         * key.  The default implementation simply finishes the current activity, 
         * but you can override this to do whatever you want. 
         */  
        public void onBackPressed() {  
            finish();  
        }  
    因此假设调用了super.onBackPressed()。就调用了finish()函数,因此之后的setResult函数根本没起作用。去掉了super.onBackPressed()之后。异常消失。

  • 相关阅读:
    day_15补充加总结
    Day_15 内置模块er
    sort 函数 与 sorted 函数区别
    python 代码风格------------PEP8规则
    python 返回值
    函数记忆 : startswith() 与 endswith
    RE模块使用
    正则表达式
    collections
    函数datetime
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/6900634.html
Copyright © 2011-2022 走看看