Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a ClassCastException about anonymous class (java)

now i get some logs like below (log-1)

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to ClassA$1
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1010)
...


line 1010

final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) obj.method();//line 1010


I think ,the log should be like this (log-2)

java.lang.ClassCastException: ClassA$1 cannot be cast to android.widget.LinearLayout$LayoutParams

I wannna know that why do i get log-1,is it right?
If log-1 is right,then how can i write code to create an exception like that?

ps:I know that ClassA$1 is an anonymous class.
ps:I get this log in a monkey test,and it's vrey hard to reappear

someone tell me :

MyInterface my = ClassA$1;//$1 is an anonymous class which implements MyInterface
my.getClass().cast(lp);

then the log will be like log-1
but why the log did not point out the "cast" method line?
and i check the folder frameworks/base/core,no one call the method "? java.lang.Class.cast(Object obj)"
any ideas?

like image 645
android_su Avatar asked Nov 03 '22 15:11

android_su


1 Answers

I cannot think of a logical explanation for that behaviour. If the facts are as you report them, then I agree that error message text is the wrong way around.

So what are the alternatives?

  • You could be misreading the log files.
  • You could be looking at the wrong version of the source code.
  • You could have found a bug ... though that seems unlikely for something as fundamental as this. (In theory, the bug could be an incorrect error message coming from the VM, an incorrect line number in the stacktrace, or even a compiler bug that is cause it to do the wrong type cast.)
  • It is even possible that the exception stacktrace is a fake ... though that explanation borders on paranoia.

My suggestions are:

  • Try running the code under a debugger, and single stepping through that section of the code to confirm your belief about the cause and source of the exception.
  • Create an SSCE for this behaviour and see if other people can reproduce it.
like image 117
Stephen C Avatar answered Nov 11 '22 06:11

Stephen C