Android使用Intent返回上一个Activity时StackOverflowError

问题是这样的,有AB两个Activity,A中用startActivityForResult启动Activity B,B中执行完毕之后用setResult(resultCode,intent)将结果返回给A,这样没有问题。但是运行时点击B的完成按钮就报错,log信息如下

<span style="font-size:18px;"><span style="color:#ff0000;">10-14 16:33:30.362: E/AndroidRuntime(18115): FATAL EXCEPTION: main
10-14 16:33:30.362: E/AndroidRuntime(18115): java.lang.IllegalStateException: Could not execute method of the activity
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.view.View$1.onClick(View.java:3599)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.view.View.performClick(View.java:4204)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.view.View$PerformClick.run(View.java:17355)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.os.Handler.handleCallback(Handler.java:725)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.os.Looper.loop(Looper.java:137)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.app.ActivityThread.main(ActivityThread.java:5102)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at java.lang.reflect.Method.invoke(Method.java:511)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at dalvik.system.NativeStart.main(Native Method)
10-14 16:33:30.362: E/AndroidRuntime(18115): Caused by: java.lang.reflect.InvocationTargetException
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at java.lang.reflect.Method.invoke(Method.java:511)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.view.View$1.onClick(View.java:3594)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	... 11 more
10-14 16:33:30.362: E/AndroidRuntime(18115): Caused by: java.lang.StackOverflowError
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at java.util.HashMap.doubleCapacity(HashMap.java:575)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at java.util.HashMap.put(HashMap.java:405)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.os.Bundle.putString(Bundle.java:492)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at android.content.Intent.putExtra(Intent.java:5340)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at com.example.test.ActivityB.finish(ActivityB.java:45)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at com.example.test.ActivityB.finish(ActivityB.java:47)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at com.example.test.ActivityB.finish(ActivityB.java:47)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at com.example.test.ActivityB.finish(ActivityB.java:47)
10-14 16:33:30.362: E/AndroidRuntime(18115): 	at com.example.test.ActivityB.finish(ActivityB.java:47)</span>
</span>
错误指出caused by java.lang.StackOverflowError.使用单步调试的时候发现点击B的完成按钮时总是在调用的函数中循环,搜索StackOverflowError相关信息很多都是和布局嵌套层数太多造成的,但是这里并不存在这种情况。最后发现时函数名造成的,其中ActivityB完成按钮响应代码如下:

<span style="font-size:18px;">    public void onClick(View v){
    	switch(v.getId()){
    	case R.id.Start_C:
    		startC();
    		break;
    	case R.id.Back_A:
    		finish();
    		break;
    	}
    }

    public void startC(){
		Intent intent = new Intent(this,ActivityC.class);
		this.startActivityForResult(intent, requestCodeOfB);    	
    }
    
    public void finish(){
		Intent intent = new Intent(this,ActivityA.class);
		intent.putExtra("msgOfB", "msgOfB:"+et_info.getText().toString()+"\nmsgOfC:"+tv_info.getText().toString());
		this.setResult(resultCodeOfB, intent);
		this.finish();    	
    }</span>

问题就出在这个响应函数的函数名finish()上,点击完成按钮后调用finish()函数,该函数中将ActivityB的处理结果返回给A,同时用this.finish()关掉ActivityB,这是我们常规的处理方法,但是按钮响应函数名写成finish()时和Activity的finish()函数是重名的,相当于重写了这个方法,所以调用这个方法的时候引起递归,单步的时候就会看见finish()这个函数循环执行,解决办法就是将按钮响应函数写成别的名字即可。


你可能感兴趣的:(android,intent)