2014-01-22 62 views
-3

任何人都可以幫助我找出這個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 
+0

您是否嘗試過清潔項目並重建它? – panini

+0

是的,我試過 – theclarkofben

+5

'ViewGroup g =(ViewGroup)contentView.getChildAt(0);'佈局的第一個孩子(實際上是唯一的孩子)是'Button'。是什麼讓你期望它是一個'ViewGroup'? – Simon

回答

2

而不是使用getChildAt()你應該直接解決您的組件是這樣的:

findViewById(R.id.button1) 
+0

這也是一個好主意,謝謝你的提示。 – theclarkofben

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); 
0

這是你如何膨脹你的看法,我不知道你爲什麼會使用Viewgroup而不是實際的類。同時還將闡明您正在訪問

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

bseides,你知道,你其實可以只設置XML作爲內容查看其對象從您的佈局?見帖子結束

內聯計數,使代碼更短,因爲它看起來不像你再次使用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); 

除此之外,你可以像這樣訪問按鈕:

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(); 
     } 
    } 
}); 
+0

感謝您的好提示。你說得對,我有更多的按鈕可以添加。實際上,我爲了簡化這個問題做了一個非常簡單的佈局xml,並且沒有讓每個人都閱讀代碼。實際的xml頁面在相對佈局中有很多按鈕。 – theclarkofben

相關問題