6

这是一个例外:

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。这是什么原因造成的?

4

3 回答 3

16

您的 LayoutParams 导入语句可能正在导入 ViewGroup.LayoutParams。尝试显式使用 LinearLayout.LayoutParams(...):

   spinner1.setLayoutParams(new LinearLayout.LayoutParams(
           LinearLayout.LayoutParams.WRAP_CONTENT,
           LinearLayout.LayoutParams.WRAP_CONTENT));
于 2012-07-20T18:39:33.853 回答
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);
于 2012-07-20T18:39:42.770 回答
1

如果您想知道每个视图必须使用哪些 LayoutParams,请查看此文档

每个视图都必须有一个与其父级对应的 LayoutParam。

例如,为了创建这个视图层次结构,

  • 相对布局相对布局
    • LinearLayout 线性布局,方向:垂直
    • 文本视图文本视图
    • 按钮按钮1
    • 按钮按钮 2

将主要活动中的 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);
于 2015-11-08T22:22:26.153 回答