ClassCastException при использовании LinearLayout.LayoutParams

Это исключение:

07-20 19:52:14.193: E/AndroidRuntime(3908): FATAL EXCEPTION: main 07-20 19:52:14.193: E/AndroidRuntime(3908): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.LinearLayout.measureVertical(LinearLayout.java:634) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1249) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 07-20 19:52:14.193: E/AndroidRuntime(3908): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2265) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.os.Handler.dispatchMessage(Handler.java:99) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.os.Looper.loop(Looper.java:137) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.app.ActivityThread.main(ActivityThread.java:4507) 07-20 19:52:14.193: E/AndroidRuntime(3908): at java.lang.reflect.Method.invokeNative(Native Method) 07-20 19:52:14.193: E/AndroidRuntime(3908): at java.lang.reflect.Method.invoke(Method.java:511) 07-20 19:52:14.193: E/AndroidRuntime(3908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 07-20 19:52:14.193: E/AndroidRuntime(3908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 07-20 19:52:14.193: E/AndroidRuntime(3908): at dalvik.system.NativeStart.main(Native Method) 

Я не вижу линии, в которой исключение выбрасывается, поэтому я не могу понять это довольно хорошо. Я знаю, что в этих строках:

 public class TodayFragment extends Fragment { ListView listView; String[] urls; ActionBar mActionBar; Spinner spinner1; public TodayFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LinearLayout linear = new LinearLayout(getActivity()); linear.setOrientation(LinearLayout.VERTICAL); spinner1 = new Spinner(getActivity()); spinner1.setLayoutParams(new LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); linear.addView(spinner1); listView = new ListView(getActivity()); spinner1.setLayoutParams(new LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); linear.addView(listView); new ParseHTML().execute(); return linear; } } 

Фрагмент, который я использую, – это android.support.v4.app.Fragment. Что вызывает это?

Solutions Collecting From Web of "ClassCastException при использовании LinearLayout.LayoutParams"

Оператор import для LayoutParams, вероятно, импортирует ViewGroup.LayoutParams. Попробуйте явно использовать LinearLayout.LayoutParams (…):

  spinner1.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

Попробуйте установить LayoutParams LinearLayout 'перед тем, как привлечь детей.

 LinearLayout linear = new LinearLayout(getActivity()); linear.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); linear.setOrientation(LinearLayout.VERTICAL); 

Если вам интересно, какие LayoutParams вы должны использовать для каждого представления, посмотрите на этот документ :

Каждая точка зрения должна иметь LayoutParam в соответствии с его родителем.

Например, для создания этого представления hierarhy,

  • RelativeLayout relativeLayout
    • LinearLayout linearLayout, ориентация: вертикальная
    • TextView textView
    • Кнопка button1
    • Кнопка button2

Замените содержание вашего метода onCreate в своем основном действии с помощью:

  super.onCreate(savedInstanceState); RelativeLayout relativeLayout = new RelativeLayout(this); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); relativeLayout.addView(linearLayout); TextView textView = new TextView(this); textView.setText("a text"); textView.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); linearLayout.addView(textView); Button button1 = new Button(this); button1.setText("button 1"); button1.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); linearLayout.addView(button1); setContentView(relativeLayout);