为什么Android会自行崩溃?我做错了什么?

时间:2015-08-27 12:59:18

标签: java android android-xml callstack

我正在为小孩子们编写一个Android应用程序来学习数学,我正在研究用户在正确回答所有问题时可以获得奖品的部分。我正在动态地在屏幕右侧添加一些视图,告诉用户他/她已获得奖品。 (每个可能的问题选项都有相应的奖品!)左侧用于显示分数。然而,该应用程序崩溃了。我试着看一下调用堆栈,看看我哪里做错了。但我的方法都没有在调用堆栈上!

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
        at android.widget.ScrollView.onMeasure(ScrollView.java:321)
        at android.view.View.measure(View.java:16030)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1034)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2196)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1290)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5508)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:5225)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)

请参阅?我的包名称叫com.smartkidslovemaths,但它不是调用堆栈!我怎么知道我做错了什么?

无论如何,我认为我的一些代码是相关的。这是屏幕右侧的布局:

<ScrollView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/right_side"
        android:orientation="vertical">

    </LinearLayout>
</ScrollView>

以下是我添加视图的方法,请注意此方法是在onCreate方法中调用的。

private void displayPrize () {
    ((LinearLayout)findViewById (R.id.right_side)).setLayoutParams (new LinearLayout.LayoutParams (FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

    TextView text = new TextView (this);
    final LinearLayout.LayoutParams textParams =
            new LinearLayout.LayoutParams (
                    ViewGroup.LayoutParams.WRAP_CONTENT, 0, 1F
            );

    final LinearLayout.LayoutParams imageParams =
            new LinearLayout.LayoutParams (
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 8F
            );
    text.setLayoutParams (textParams);
    Resources res = getResources ();
    QuestionOptions options = QuestionsActivity.Options.getOptions ();

    switch (options.getDigitCount ()) {
        case 1:
        default:
            text.setText (res.getText (R.string.earn_star_text));
            break;
        case 2:
            text.setText (res.getText (R.string.earn_badge_text));
            break;
        case 3:
            text.setText (res.getText (R.string.earn_trophy_text));
            break;
    }

    text.setTextSize (res.getDimension (R.dimen.answer_text_size) / 4);

    ((LinearLayout)findViewById (R.id.right_side)).addView (text);

    ImageView image = new ImageView (this);
    image.setLayoutParams (imageParams);
    int resID = QuestionOptionMaps.getOptionsDrawableMap ().get (options);
    image.setImageResource (resID);
    ((LinearLayout)findViewById (R.id.right_side)).addView (image);
}

我真的不知道我做错了什么。或者也许我是正确的,这只是一个Android错误?

2 个答案:

答案 0 :(得分:0)

 (new LinearLayout.LayoutParams (FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

FrameLayout更改为LinearLayout

答案 1 :(得分:0)

ScrollView延伸FrameLayout

设置视图的layoutparams时,你要为它们所在的布局设置参数。因此在这一部分:

((LinearLayout)findViewById (R.id.right_side)).setLayoutParams(
  new LinearLayout.LayoutParams(
    FrameLayout.LayoutParams.MATCH_PARENT,
     FrameLayout.LayoutParams.MATCH_PARENT));

尝试用

替换new LinearLayout.LayoutParams
new FrameLayout.LayoutParams(...)