ClassCastException错误?

时间:2018-06-10 12:58:33

标签: java android

刚开始学习如何使用Android Studio进行开发,并在执行我的应用程序时收到此错误:

06-10 22:53:56.819 11735-11735/com.example.jakey.new_application E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.jakey.new_application, PID: 11735
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jakey.new_application/com.example.jakey.new_application.MainActivity}: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.LinearLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2545)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605)
        at android.app.ActivityThread.access$1100(ActivityThread.java:165)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:179)
        at android.app.ActivityThread.main(ActivityThread.java:5730)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
     Caused by: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.LinearLayout
        at com.example.jakey.new_application.MainActivity.onCreate(MainActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:6439)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2498)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:165) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:179) 
        at android.app.ActivityThread.main(ActivityThread.java:5730) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) 

1 个答案:

答案 0 :(得分:0)

错误发生在您提供的onCreate函数中:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    background = (LinearLayout) findViewById(R.id.layout); //here is the error
    myButton = (Button) findViewById(R.id.myButton);
    myButton.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) {
            background.setBackgroundColor(Color.parseColor("#00aa00")); 
        } 
    }); 
}

当你调用findViewById(R.id.layout)时,它会返回一个ConstraintLayout,然后你将其转换为LinearLayout,这会导致错误。 要修复它,只需替换此行:

background = (LinearLayout) findViewById(R.id.layout);

这一个:

background = (ConstraintLayout) findViewById(R.id.layout);