奇怪的java.lang.ClassCastException

时间:2016-09-05 05:50:50

标签: java android classcastexception

我正在编写扑克游戏应用程序,并尝试使用以下代码调整屏幕上的卡片位置:

的xml:

<RelativeLayout...>
......
    <LinearLayout...>......</LinearLayout>
    <LinearLayout...>......</LinearLayout>
    <TableLayout...>......</TableLayout>
......
<ImageView
    android:layout_width="75dp"
    android:layout_height="100dp"
    android:id="@+id/assignedCardCenter"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />
<ImageView
    android:layout_width="75dp"
    android:layout_height="100dp"
    android:scaleType="centerInside"
    android:id="@+id/assignedCardEast"
    android:layout_toRightOf="@id/assignedCardCenter"
    android:layout_toEndOf="@id/assignedCardCenter"
    android:layout_alignBottom="@id/assignedCardCenter" />
<ImageView
    android:layout_width="75dp"
    android:layout_height="100dp"
    android:scaleType="centerInside"
    android:id="@+id/assignedCardWest"
    android:layout_toLeftOf="@id/assignedCardCenter"
    android:layout_toStartOf="@id/assignedCardCenter"
    android:layout_alignBottom="@id/assignedCardCenter" />
<ImageView
    android:layout_width="75dp"
    android:layout_height="100dp"
    android:scaleType="centerInside"
    android:id="@+id/assignedCardSouth"
    android:layout_below="@id/assignedCardCenter"
    android:layout_alignLeft="@id/assignedCardCenter"
    android:layout_alignStart="@id/assignedCardCenter" />
<ImageView
    android:layout_width="75dp"
    android:layout_height="100dp"
    android:scaleType="centerInside"
    android:id="@+id/assignedCardNorth"
    android:layout_above="@id/assignedCardCenter"
    android:layout_alignLeft="@id/assignedCardCenter"
    android:layout_alignStart="@id/assignedCardCenter"
    android:src="@drawable/c50"/>
......
</RelativeLayout>

的java:

private ImageView[] assignedCardsOnTable=new ImageView[4];
private int[] assignedCardsOnTableID={R.id.assignedCardEast,R.id.assignedCardSouth,R.id.assignedCardWest,R.id.assignedCardNorth};
//......
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        
    for(int i=0;i<4;i++)
    {
        assignedCardsOnTable[i]=(ImageView)findViewById(cardsOnTableID[i]);
        assignedCardsOnTable[i].setVisibility(View.GONE);
    }
    //......
    SetAssignedCard_DummyHandLayout();
}
//......
private void SetAssignedCard_DummyHandLayout()
{
    for(int i=0;i<4;i++)
    {
        assignedCardsOnTable[i].setVisibility(View.VISIBLE);
    }
    RelativeLayout.LayoutParams[] cardPosition=new RelativeLayout.LayoutParams[4];
    for(int i=0;i<4;i++)
    {
        //==========THIS IS WHERE THE EXCEPTION OCCURED===========
        cardPosition[i]=(RelativeLayout.LayoutParams)assignedCardsOnTable[i].getLayoutParams();
        //========================================================
        cardPosition[i].addRule(RelativeLayout.RIGHT_OF,0);
        cardPosition[i].addRule(RelativeLayout.LEFT_OF,0);
        cardPosition[i].addRule(RelativeLayout.ABOVE,0);
        cardPosition[i].addRule(RelativeLayout.BELOW,0);
    }
    if(Contract.dealer=='E')
    {

    }
    else if(Contract.dealer=='W')
    {

    }
    else
    {
        cardPosition[0].addRule(RelativeLayout.RIGHT_OF,R.id.assignedCardCenter);
        cardPosition[0].addRule(RelativeLayout.ALIGN_BOTTOM,R.id.assignedCardCenter);
        cardPosition[0].setMargins(5,0,0,0);
        cardPosition[1].addRule(RelativeLayout.BELOW,R.id.assignedCardCenter);
        cardPosition[1].addRule(RelativeLayout.ALIGN_LEFT,R.id.assignedCardCenter);
        cardPosition[1].setMargins(0,5,0,0);
        cardPosition[2].addRule(RelativeLayout.LEFT_OF,R.id.assignedCardCenter);
        cardPosition[2].addRule(RelativeLayout.ALIGN_BOTTOM,R.id.assignedCardCenter);
        cardPosition[2].setMargins(0,0,5,0);
        cardPosition[3].addRule(RelativeLayout.ABOVE,R.id.assignedCardCenter);
        cardPosition[3].addRule(RelativeLayout.ALIGN_LEFT,R.id.assignedCardCenter);
        cardPosition[3].setMargins(0,0,0,5);
    }
    for(int i=0;i<4;i++)
    {
        assignedCardsOnTable[i].setLayoutParams(cardPosition[i]);
        assignedCardsOnTable[i].setImageResource(cardImageID[50]);
    }
}

我的函数java.lang.ClassCastException中发生SetAssignedCard_DummyHandLayout(),这是堆栈跟踪:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams                                                                             
at com.einsambr.bridge_0_0_2.MainActivity.SetAssignedCard_DummyHandLayout(MainActivity.java:568)                                                                             
at com.einsambr.bridge_0_0_2.MainActivity.SetPlayerRevisedAndPlay(MainActivity.java:544)                                                                                 
at com.einsambr.bridge_0_0_2.MainActivity.access$1000(MainActivity.java:21)                                                                                
at com.einsambr.bridge_0_0_2.MainActivity$1.onClick(MainActivity.java:273)                                                                             
at android.view.View.performClick(View.java:5198)                                                                             
at android.view.View$PerformClick.run(View.java:21147)                                                                             
at android.os.Handler.handleCallback(Handler.java:739)                                                                             
at android.os.Handler.dispatchMessage(Handler.java:95)                                                                             
at android.os.Looper.loop(Looper.java:148)                                                                             
at android.app.ActivityThread.main(ActivityThread.java:5417)                                                                             
at java.lang.reflect.Method.invoke(Native Method)                                                                             
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

问题:根据xml代码,assignedCardsOnTable [i]确实安排在RelativeLayout中,因此,

assignedCardsOnTable[i].getLayoutParams();

应该提出RelativeLayout.LayoutParams。 为什么会发生ClassCastException?我哪里做错了?谢谢你的帮助:)

0 个答案:

没有答案