任何人都可以幫助我找出這個android.widget.Button的原因不能轉換爲android.view.ViewGroup錯誤?我花了幾個小時在線尋找線索,但沒有其他用戶強調的問題爲我帶來了一個解決方案。錯誤:java.lang.ClassCastException: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
您是否嘗試過清潔項目並重建它? – panini
是的,我試過 – theclarkofben
'ViewGroup g =(ViewGroup)contentView.getChildAt(0);'佈局的第一個孩子(實際上是唯一的孩子)是'Button'。是什麼讓你期望它是一個'ViewGroup'? – Simon