請參考以下鏈接我的開機畫面: http://postimg.org/image/yvivajxov/拋出ClassCastException在訪問兒童的LayoutParams佈局
我一直在試圖建立在我的活動,重新調整大小(高度)的觸摸手勢以滾動的 根據手勢進行佈局。如果是MotionEvent.ACTION_DOWN
,則應增加第一佈局的高度並減小第二佈局的高度。
最初佈局在屏幕上均勻分佈。以下xml已用於創建佈局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScrollAct" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/llout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/red_bar"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="@color/red" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/green_bar"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="@color/green" >
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
以下是全局:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
linearLayout=(LinearLayout)findViewById(R.id.llout);
redPanel = (RelativeLayout)findViewById(R.id.red_bar);
greenPanel = (RelativeLayout)findViewById(R.id.green_bar);
而且我對事件的代碼如下:
linearLayout.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) redPanel.getLayoutParams();
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) greenPanel.getLayoutParams();
_yDelta =Y - lParams.bottomMargin;
layoutParams.topMargin -=_yDelta;
lParams.bottomMargin +=_yDelta;
redPanel.setLayoutParams(lParams);
greenPanel.setLayoutParams(layoutParams);
Log.i("Relative LayoutParams",""+redPanel.getLayoutParams().height);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_MOVE:
}
return false;
}});
isPanelShown=false;
}
,並拋出以下異常: -
12-17 08:48:44.633: E/AndroidRuntime(29031): FATAL EXCEPTION: main
12-17 08:48:44.633: E/AndroidRuntime(29031): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
12-17 08:48:44.633: E/AndroidRuntime(29031): at com.example.multifrag.ScrollAct$1.onTouch(ScrollAct.java:54)
12-17 08:48:44.633: E/AndroidRuntime(29031): at android.view.View.dispatchTouchEvent(View.java:5624)
12-17 08:48:44.633: E/AndroidRuntime(29031): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1964)
12-17 08:48:44.633: E/AndroidRuntime(29031): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1725)
12-17 08:48:44.633: E/AndroidRuntime(29031): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
我經歷瞭解決方案省以下問題: ClassCastException LinearLayout LayoutParams
並創建了一個tablelayout來訪問作爲父佈局的reqd RelativeLayouts,但是它引發了相同的異常。
嘗試清理並生成一次 – Raghunandan