错误:java.lang.ClassCastException:android.widget.Button无法强制转换为android.view.ViewGroup

时间:2014-01-22 22:10:48

标签: java android xml

任何人都可以帮我弄清楚这个android.widget.Button的原因是不是可以强制转换为android.view.ViewGroup错误?我花了几个小时在网上寻找线索,但其他用户突出显示的问题都没有为我找到解决方案。

public class LandingPageActivity extends Activity {@
    Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final String packageName = this.getClass().getPackage().getName();
        final Context context = this;

        // ScrollView
        ViewGroup contentView = (ViewGroup) LayoutInflater.from(context)
            .inflate(R.layout.activity_landing_page, null);

        // Layout containing Buttons
        ViewGroup g = (ViewGroup) contentView.getChildAt(0);
        int count = g.getChildCount();

        for (int i = 0; i < count; i++) {
            Button btn = (Button) g.getChildAt(i);
            final String text = btn.getText().toString();
            btn.setOnClickListener(new OnClickListener() {@
                Override
                public void onClick(View v) {
                    try {
                        Class c = Class.forName(packageName + "." + text);
                        startActivity(new Intent(context, c));
                    } catch (ClassNotFoundException e) {
                        Toast.makeText(context, String.valueOf(e), 5000).show();
                    }
                }
            });
        }
        setContentView(contentView);
    }
}

activity_landing_page.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Click Button" />

</ScrollView>

错误记录

01-22 22:05:45.068: E/AndroidRuntime(29628): FATAL EXCEPTION: main
01-22 22:05:45.068: E/AndroidRuntime(29628): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.clarkben.android.footballinsight/com.clarkben.android.footballinsight.LandingPageActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.ViewGroup
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.os.Looper.loop(Looper.java:137)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.app.ActivityThread.main(ActivityThread.java:5103)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at java.lang.reflect.Method.invokeNative(Native Method)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at java.lang.reflect.Method.invoke(Method.java:525)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at dalvik.system.NativeStart.main(Native Method)
01-22 22:05:45.068: E/AndroidRuntime(29628): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.ViewGroup
01-22 22:05:45.068: E/AndroidRuntime(29628):    at com.clarkben.android.footballinsight.LandingPageActivity.onCreate(LandingPageActivity.java:27)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.app.Activity.performCreate(Activity.java:5133)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-22 22:05:45.068: E/AndroidRuntime(29628):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-22 22:05:45.068: E/AndroidRuntime(29628):    ... 11 more

3 个答案:

答案 0 :(得分:2)

而不是使用getChildAt(),您应该像这样直接寻址您的组件:

findViewById(R.id.button1)

答案 1 :(得分:0)

尝试将代码更改为:

// ScrollView
ViewGroup contentView = (ViewGroup) LayoutInflater.from(context)
    .inflate(R.layout.activity_landing_page, null);

// Layout containing Buttons
int count = contentView.getChildCount();

for (int i = 0; i < count; i++) {
    Button btn = (Button) contentView.getChildAt(i);
    final String text = btn.getText().toString();
    btn.setOnClickListener(new OnClickListener() {@
        Override
        public void onClick(View v) {
            try {
                Class c = Class.forName(packageName + "." + text);
                startActivity(new Intent(context, c));
            } catch (ClassNotFoundException e) {
                Toast.makeText(context, String.valueOf(e), 5000).show();
            }
        }
    });
}
setContentView(contentView);

答案 2 :(得分:0)

这是你如何夸大你的观点,我不知道为什么你会使用Viewgroup而不是实际的类。它还阐明了您正在访问的布局中的哪个对象

ScrollView  contentView = (ScrollView) LayoutInflater.from(context).inflate(R.layout.activity_landing_page, null);

bseides,你知道你实际上可以将xml设置为contentView吗?见帖子的结尾

内联计数,使代码更短,因为它看起来不像你再次使用那个int。

for (int i = 0; i < contentView.getChildCount(); i++) {
    Button btn = (Button) contentView.getChildAt(i);
    final String text = btn.getText().toString();
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Class c = Class.forName(packageName + "." + text);
                startActivity(new Intent(context, c));
            } catch (ClassNotFoundException e) {
                Toast.makeText(context, String.valueOf(e), 5000).show();
            }
        }
    });
}
setContentView(contentView);

除此之外,您可以像这样访问Button:

contentView.findViewById(R.id.button1);

但是我假设你要添加更多的按钮,即使你不认为只需要大量的按钮就会看起来太好了。

另一种方法

这与您的代码完全相同,但明显更短。

setContentView(R.layout.activity_landing_page):

Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
    Override
    public void onClick(View v) {
        try {
            Class c = Class.forName(packageName + "." + text);
            startActivity(new Intent(context, c));
        } catch (ClassNotFoundException e) {
            Toast.makeText(context, String.valueOf(e), 5000).show();
        }
    }
});