致命异常:主要错误

时间:2016-03-07 17:35:46

标签: java android android-layout

这是日志消息

public void testCollectionAdd() {
    if (!isAddSupported()) return;

    Object[] elements = getFullElements();
    for (int i = 0; i < elements.length; i++) {
        resetEmpty();
        boolean r = collection.add(elements[i]);
        confirmed.add(elements[i]);
        verify();
        assertTrue("Empty collection changed after add", r);
        assertTrue("Collection size is 1 after first add",
                   collection.size() == 1);
    }

    resetEmpty();
    int size = 0;
    for (int i = 0; i < elements.length; i++) {
        boolean r = collection.add(elements[i]);
        confirmed.add(elements[i]);
        verify();
        if (r) size++;
        assertEquals("Collection size should grow after add",
                     size, collection.size());
        assertTrue("Collection should contain added element",
                   collection.contains(elements[i]));
    }
}

这是我的代码

03-07 22:54:51.555: E/AndroidRuntime(18687): FATAL EXCEPTION: main
03-07 22:54:51.555: E/AndroidRuntime(18687): Process: com.example.simplegamer003.registerapp, PID: 18687
03-07 22:54:51.555: E/AndroidRuntime(18687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplegamer003.registerapp/com.example.simplegamer003.registerapp.SpinnerActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Spinner
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.-wrap11(ActivityThread.java)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.os.Looper.loop(Looper.java:148)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.main(ActivityThread.java:5466)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at java.lang.reflect.Method.invoke(Native Method)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-07 22:54:51.555: E/AndroidRuntime(18687): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Spinner
03-07 22:54:51.555: E/AndroidRuntime(18687):    at com.example.simplegamer003.registerapp.SpinnerActivity.onCreate(SpinnerActivity.java:22)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.Activity.performCreate(Activity.java:6251)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
03-07 22:54:51.555: E/AndroidRuntime(18687):    ... 9 more

我不知道为什么我会收到此错误,请有人告诉我。

3 个答案:

答案 0 :(得分:1)

嗯,显然

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Spinner

表示您在Spinner

中有一个名为"@+id/spinner的LinearLayout而不是activity_spinner

答案 1 :(得分:0)

如果您确定它是微调器并且您最近对布局文件进行了更改,那么只需清理项目并再次运行即可解决您的问题。 有时修改布局文件不会立即反映到R文件并提供ClassCastException。

答案 2 :(得分:0)

在你的SpinnerActivity中,LinearLayout无法强制转换Spinner.Which意味着你在 activity_spinner.xml 中使用了LinearLayout,而是使用spinner = (Spinner)findViewById(R.id.spinner);代替。