这是一个例外:
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。是什么原因造成的?
答案 0 :(得分:16)
您对LayoutParams的import语句可能正在导入ViewGroup.LayoutParams。尝试明确使用LinearLayout.LayoutParams(...):
spinner1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
答案 1 :(得分:2)
在添加任何子项之前尝试设置LinearLayout LayoutParams。
LinearLayout linear = new LinearLayout(getActivity());
linear.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
linear.setOrientation(LinearLayout.VERTICAL);
答案 2 :(得分:1)
如果您想知道每个视图必须使用哪些LayoutParams,请查看此doc:
每个视图必须根据其父级设置一个LayoutParam。
例如,为了创建此视图hierarhy,
使用以下命令替换主活动中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);