在做Walker的健康栏目时,遇到了一些问题,像Java.lang.ClassCastException,从字面意思看,是类型转换错误。错误代码如下:
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class HealthActivity extends Activity {
private int currIndex;//当前页卡编号
private TextView tvCursor;
private TextView tvHealthNews;
private TextView tvIllnessDefense;
private ViewPager vpHealth;
private ImageView ivHealthBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health);
initViews();
initCursor();
initViewPager();
setListeners();
}
private void initViews() {
ivHealthBack=(ImageView)findViewById(R.id.icons_health_back);
tvCursor=(TextView)findViewById(R.id.cursor);
tvHealthNews=(TextView)findViewById(R.id.tv_healthnews);
tvIllnessDefense = (TextView)findViewById(R.id.tv_healthill);
vpHealth=(ViewPager)findViewById(R.id.viewpager);
}
@SuppressLint("NewApi")
private void initCursor() {
// TODO Auto-generated method stub
Display display=getWindow().getWindowManager().getDefaultDisplay();
DisplayMetrics metrics=new DisplayMetrics();
display.getMetrics(metrics);//取得手机屏幕宽度的一半
int tabLineLength=metrics.widthPixels/2;
//设置游标的宽度为屏幕宽度的一半
LayoutParams lp=(LayoutParams)tvCursor.getLayoutParams();
lp.width =tabLineLength;
tvCursor.setLayoutParams(lp);
}
此时系统并不报错,但当我们运行时,系统报错,Java.lang.ClassCastException,解决此问题的方法:一般是直接转换成该类型即可。如果不能确定类型可以通过下面的两种方式进行处理(假设对象为o):1、通过o.getClass().getName()得到具体的类型,可以通过输出语句输出这个类型,然后根据类型进行进行具体的处理。2、通过if(o
instanceof 类型)的语句来判断o的类型是什么。
正确代码如下:
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class HealthActivity extends Activity {
private int currIndex;//当前页卡编号
private TextView tvCursor;
private TextView tvHealthNews;
private TextView tvIllnessDefense;
private ViewPager vpHealth;
private ImageView ivHealthBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health);
initViews();
initCursor();
initViewPager();
setListeners();
}
private void initViews() {
ivHealthBack=(ImageView)findViewById(R.id.icons_health_back);
tvCursor=(TextView)findViewById(R.id.cursor);
tvHealthNews=(TextView)findViewById(R.id.tv_healthnews);
tvIllnessDefense = (TextView)findViewById(R.id.tv_healthill);
vpHealth=(ViewPager)findViewById(R.id.viewpager);
}
@SuppressLint("NewApi")
private void initCursor() {
// TODO Auto-generated method stub
Display display=getWindow().getWindowManager().getDefaultDisplay();
DisplayMetrics metrics=new DisplayMetrics();
display.getMetrics(metrics);//取得手机屏幕宽度的一半
int tabLineLength=metrics.widthPixels/2;
//设置游标的宽度为屏幕宽度的一半
LayoutParams lp=(LayoutParams)tvCursor.getLayoutParams();
lp.width =tabLineLength;
tvCursor.setLayoutParams(lp);
}
这里的错误是,我将LayoutParams的包导错了。LayoutParams是ViewGroup的一个内部类,而我将其强制类型转换成了Linearlayout了。强制类型转换的前提是父类引用指向的对象的类型是子类的时候才可以进行强制类型转换,如果父类引用指向的对象的类型不是子类的时候java.lang.ClassCastException异常。