ViewFlipper是一个按钮?

Dan*_*bbs 0 android casting button classcastexception viewflipper

我最近决定在我的应用程序中添加一个ViewFlipper,这是我所做的:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/steve"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    // Child elements here.

</ViewFlipper>
Run Code Online (Sandbox Code Playgroud)

MyApp.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.steve);
Run Code Online (Sandbox Code Playgroud)

但是,当我运行我的应用程序时,我得到了一个java.lang.ClassCastException: android.widget.Button用于转换为ViewFlipper的行.

经过调试,结果是findViewById(R.id.steve)返回一个Button,而不是一个ViewFlipper.

我怎样才能解决这个问题?

Gal*_*lal 5

清理并重建您的项目.这应该解决它.而且,不,ViewFlipper不是Button或Button的子类.