728x90
반응형
안드로이드 개발중에 java.lang.ClassCastException 라는 에러를 만난다면 아래처럼 간단히 해결해보세요.
1. 에러 코드 (오류)
-. 아래코드는 해당 익셉션이 발생하는 코드입니다.
plusNumber 함수안에서 Object를 이용해서 Integer변수 타입을 선언하였습니다.
이후 출력할때 String 타입으로 캐스팅을 하게됩니다. 이때 에러가 발생합니다.
2. 에러 로그
-. 아래처럼 java.lang.ClassCastException 이 발생하는것을 확인 가능합니다.
3. 해결 방법
-. 아래 파란색으로 마킹된 부분처럼 선언한 변수의 타입인 integer에 맞게 출력하면 해결이 됩니다.
아주 간단한 에러이지만, 실제 코딩중에는 자주 발생하는 에러입니다.
*. 에러 로그 원문
FATAL EXCEPTION: main Process: com.picker.javaexception, PID: 26927 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.picker.javaexception/com.picker.javaexception.MainActivity}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at co m.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at co m.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at com.picker.javaexception.MainActivity.plusNumber(MainActivity.java:26) at com.picker.javaexception.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:8051) at android.app.Activity.performCreate(Activity.java:8031) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at co m.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at co m.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) |
*. 익셉션 백서 원문
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:
Object x = new Integer(0); System.out.println((String)x);
Summary
Public constructors
ClassCastException()Constructs a ClassCastException with no detail message. |
ClassCastException(String s)Constructs a ClassCastException with the specified detail message. |
Inherited methods
From class java.lang.Throwable | |
From class java.lang.Object |
Public constructors
ClassCastException
Added in API level 1
public ClassCastException ()
Constructs a ClassCastException with no detail message.
ClassCastException
Added in API level 1
public ClassCastException (String s)
Constructs a ClassCastException with the specified detail message.
Parameters
s | String: the detail message. |
*. 코드 원문
package com.picker.javaexception;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "javaexception";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plusNumber();
}
private void plusNumber() {
Object x = new Integer(0);
System.out.println((Integer)x);
}
}
반응형
'안드로이드 개발' 카테고리의 다른 글
android.view.WindowManager$BadTokenException 해결 (1) | 2023.05.05 |
---|---|
ArithmeticException 해결 방법 (0) | 2023.05.04 |
java.lang.IllegalThreadStateException 해결 법 (0) | 2023.05.04 |
java.lang.UnsupportedOperationException 해결 법 (0) | 2023.05.04 |
java.lang.SecurityException: Permission Denial: starting Intent 해결 (0) | 2023.05.04 |