概述
今天遇到一bug,报错如下
09-15 11:07:29.075 7669 7669 E AndroidRuntime: FATAL EXCEPTION: main
09-15 11:07:29.075 7669 7669 E AndroidRuntime: Process: com.android.mms, PID: 7669
09-15 11:07:29.075 7669 7669 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mms/com.android.mms.ui.ComposeMessageActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to com.android.mms.ui.MyMarqueeTextView
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2743)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4657)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.ActivityThread.-wrap19(ActivityThread.java)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1550)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.os.Looper.loop(Looper.java:203)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6293)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1086)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to com.android.mms.ui.MyMarqueeTextView
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at com.android.mms.ui.ComposeMessageActivity.updateTitle(ComposeMessageActivity.java:2727)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at com.android.mms.ui.ComposeMessageActivity.initialize(ComposeMessageActivity.java:3400)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at com.android.mms.ui.ComposeMessageActivity.onCreate(ComposeMessageActivity.java:3086)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6681)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2696)
09-15 11:07:29.075 7669 7669 E AndroidRuntime: ... 10 more
可能情况1 java代码中向下转型错误
比如如下栗子
xml:
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="这是超长文本这是超长文本这是超长文本这是超长文本文本这是超长文本" />
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="这是超长文本这是超长文本这是超长文本这是超长文本文本这是超长文本" />
主界面
public class MainActivity extends Activity {
TextView tv = null;
TextView auto = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
tv.setTextColor(Color.BLUE);
auto = (AutoMarqueeTextView) findViewById(R.id.auto);
auto.setTextColor(Color.RED);
auto = (AutoMarqueeTextView)tv;//向下转型报错
}
}
PS:AutoMarqueeTextView是继承TextView的子类
虽然说父类引用可以指向子类对象,即所谓的向上转型(子类转父类)
即:
TextView auto = null;
auto = (AutoMarqueeTextView) findViewById(R.id.auto);
但是向下转型是有问题的,tv是TextView(父类),不能转换为子类AutoMarqueeTextView,这也是为什么
auto = (AutoMarqueeTextView)tv;//编译无错误,运行时将出错
会报错。核心报错在于(AutoMarqueeTextView)tv;,而报错类型正是ClassCastException
换句话说,父类可以指向子类对象,但是却不能将父类强制转化为子类
但是有一个情况例外
比如B extends A
那么如下代码是没有问题的:
A a = new B();
B b = (B)a;
以上代码缩写的话就是
B b=(B)new B();
原因是a本来就是B对象的实例
以上这种没有错误的写法用在android的栗子中,就是
TextView tv = null;
AutoMarqueeTextView auto = null;
tv = (AutoMarqueeTextView) findViewById(R.id.auto);
auto = (AutoMarqueeTextView)tv;
为避免向下转型问题发生可以加入如下判断
TextView tv = null;
AutoMarqueeTextView auto = null;
tv = (AutoMarqueeTextView) findViewById(R.id.auto);
if(tv instanceof AutoMarqueeTextView){
Log.v("chj","tv is AutoMarqueeTextView");
auto = (AutoMarqueeTextView)tv;
}else{
Log.v("chj","tv is not AutoMarqueeTextView");
}
最终应该会打印
03-08 13:12:39.317 5807 5807 V chj : tv is AutoMarqueeTextView
可能情况2 xml中的定义与java代码中的不匹配(xml引起的类型转化错误)
比如xml定义为:
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="这是超长文本这是超长文本这是超长文本这是超长文本文本这是超长文本" />
而java代码却是这样:
AutoMarqueeTextView auto = null;
auto = (AutoMarqueeTextView) findViewById(R.id.auto);
就会报错。其实本质原因一样。findViewById(R.id.auto);取到的是TextView(父类),强制转换成子类或者说向下转型时报错。
今天遇到的问题是android 7.1加入的分屏功能引起的。 分屏和全屏时使用的xml不一样,之前只修改了一个xml和java文件,导致在分屏时java与另一个xml不匹配,发生了类型转化错误
最后
以上就是强健汉堡为你收集整理的android类型强制转化,Android笔记 - android 类型转化错误的全部内容,希望文章能够帮你解决android类型强制转化,Android笔记 - android 类型转化错误所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复