0

我正在研究一個最低版本爲2.2的Android應用程序。我也在使用android support v7 gridlayout。我試圖用java代碼創建它並將其插入線性佈局。但是,當我這樣做時它會崩潰。插入子視圖時Android支持v7 gridlayout崩潰

GridLayout gl = new GridLayout(this); 
    LinearLayout.LayoutParams gridMargin = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    gl.setLayoutParams(gridMargin); 
    gl.setColumnCount(1); 
    gl.setRowCount(1); 

      TextView number = new TextView(this); 
      number.setPadding(10, 10, 10, 10); 
      number.setText(Integer.toString(1, 10)); 
      number.setTextAppearance(this, R.style.studentGrid); 
      number.setBackgroundResource(R.drawable.grid_cell); 
      LinearLayout.LayoutParams numberMargin = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      number.setLayoutParams(numberMargin); 
      gl.addView(number); 

    studentContainer.addView(gl); 

如果我不添加子元素,那麼它不會崩潰。有誰知道這裏有什麼錯嗎?

謝謝。

10-22 20:33:06.160: E/AndroidRuntime(25131): FATAL EXCEPTION: main 
10-22 20:33:06.160: E/AndroidRuntime(25131): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.GridLayout$LayoutParams 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.support.v7.widget.GridLayout.computeLayoutParamsHashCode(GridLayout.java:872) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.support.v7.widget.GridLayout.checkForLayoutParamsModification(GridLayout.java:879) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.support.v7.widget.GridLayout.onMeasure(GridLayout.java:927) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.HorizontalScrollView.measureChildWithMargins(HorizontalScrollView.java:1475) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.HorizontalScrollView.onMeasure(HorizontalScrollView.java:361) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1466) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.ScrollView.onMeasure(ScrollView.java:388) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.View.measure(View.java:15460) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1987) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1401) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1121) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4598) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.Choreographer.doFrame(Choreographer.java:525) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.os.Handler.handleCallback(Handler.java:615) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.os.Looper.loop(Looper.java:137) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at android.app.ActivityThread.main(ActivityThread.java:4950) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at java.lang.reflect.Method.invokeNative(Native Method) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at java.lang.reflect.Method.invoke(Method.java:511) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
10-22 20:33:06.160: E/AndroidRuntime(25131): at dalvik.system.NativeStart.main(Native Method) 
+0

後堆棧跟蹤? –

+0

好的,我發佈了它。 – omega

回答

0

當您將佈局參數設置爲子部件時,佈局參數實現類必須與容器部件匹配。換句話說,如果您將孩子添加到GridLayout,則必須將集合GridLayout.LayoutParams用於孩子。

例如,

GridLayout.LayoutParams numberMargin = new GridLayout.LayoutParams(...); 
number.setLayoutParams(numberMargin); 
+0

這是未定義的「新的GridLayout.LayoutParams」,但是eclipse提供了改變它,所以它增加了更多的參數。,6個整數和2個規格類型.... – omega

+0

是的,請參閱方便的花花公子參考頁。 –

+0

我試着點擊那個建議,但後來說它不是易用的。 – omega