setlayoutParams()抛出异常

时间:2013-05-19 11:17:38

标签: android relativelayout android-custom-view

我正在研究一个从RelativeLayout扩展的子类,并且有一个OnScaleGestureListener。我希望你在视图上缩放时,这个变化的高度(就像scalaing,但调整对象的大小)。但它让我有一个例外,我不知道如何解释它。

我的代码是:

public class simpleOnScaleGestureListener extends SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        tmpScale = detector.getCurrentSpan() / detector.getPreviousSpan();
        Log.d("DEBUG", ".getCurrentSpan() is "+detector.getCurrentSpan());
        Log.d("DEBUG", ".getPreviousSpan() is "+detector.getPreviousSpan());
        Log.d("DEBUG", ".getScaleFactor() is "+detector.getScaleFactor());
        scaling = true;
        RelativeLayout.LayoutParams params = new LayoutParams(kTicketWidth, (int) (getMeasuredHeight()*mScaleFactor));
        setLayoutParams(params);
        //invalidate();
        requestLayout();
    return true;
    }
    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
        mScaleFactor = detector.getCurrentSpan() / detector.getPreviousSpan();
        adjustTickets();
        //Expand Animation
        /*if (mScaleFactor >= kMinScale){
            ScaleAnimation anim = new ScaleAnimation(1, 1, mScaleFactor, 1);
            startAnimation(anim);
            tmpScale=1;
            adjustTickets();
        } else if (mScaleFactor < kMinScale){
            ScaleAnimation anim = new ScaleAnimation(1, 1, mScaleFactor, 1);
            startAnimation(anim);
            tmpScale = kMinScale;
            adjustTickets();
        }
        scaling = false;*/
        Log.d("SCALE", "Scale have ended");
    }
}   

对我而言,例外情况是:

05-19 13:10:28.943: E/AndroidRuntime(15701): FATAL EXCEPTION: main
05-19 13:10:28.943: E/AndroidRuntime(15701): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
05-19 13:10:28.943: E/AndroidRuntime(15701):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:655)
05-19 13:10:28.943: E/AndroidRuntime(15701):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-19 13:10:28.943: E/AndroidRuntime(15701):    at android.view.View.measure(View.java:15190)

另一件我不知道为什么会发生的事情是mScaleFactor一直变为1.0,当它变为0.81464或1.28774然后再次变为1.0。有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:2)

异常显示问题是什么

 android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

您正在尝试将LinearLayout强制转换为RelativeLayout,请检查您的XML文件