gpt4 book ai didi

android - 使用 LinearLayout.LayoutParams 时出现 ClassCastException

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:34 25 4
gpt4 key购买 nike

这是异常(exception)情况:

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;
}
}

我使用的 fragment 是 android.support.v4.app.Fragment。是什么原因造成的?

最佳答案

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

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

关于android - 使用 LinearLayout.LayoutParams 时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com