마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

화면 전환하면 앱이 강제 종료가 됩니다

0 추천

코드 문법 상으로는 문제가 없습니다.

manifest.xml에도 activity 이름 다 넣었습니다.

그런데 이상하게 안 되네요.

 

2021-09-18 20:21:09.358 17908-17908/com.example.quotes_ver1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.quotes_ver1, PID: 17908
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quotes_ver1/com.example.quotes_ver1.MainActivity2}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        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:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button
        at com.example.quotes_ver1.MainActivity2.onCreate(MainActivity2.java:20)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        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:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

 

이건 로그켓입니다

그리고 밑에 코드는 버튼 관련 코드입니다. 이미지 버튼인데 이미지 버튼이랑 버튼 구분을 안해서인걸까요?

 

public class MainActivity2 extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        button = findViewById(R.id.imageButton2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity2.this, Quotes1.class);
                startActivity(intent);
            }
        });

        button = findViewById(R.id.imageButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity2.this, Quotes2.class);
                startActivity(intent);
            }
        });
    }
}
경제적자유를위해 (120 포인트) 님이 2021년 9월 18일 질문

1개의 답변

0 추천

네. 그렇게 보이네요.  ImageButto은 ImageView를 상속받은 것이고 Button은 TextView를 상속받은 거예요. 완전히 달라요.

그리고 View를 초기화 하는 부분이 좀 수상하네요.

public class MainActivity2 extends AppCompatActivity {
    private ImageButton imageButton;
    private ImageButton imageButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...

        imageButton2 = findViewById(R.id.imageButton2);  //==> 멤버변수 butto을 공유하면 안됨.
        ...

        imageButton = findViewById(R.id.imageButton);   //==> 멤버변수 butto을 공유하면 안됨
        ... 
    }
}

 

변수를 하나 가지고 Activity레벨에서 View를 두개를 참조하는 건 잘못된 코드입니다. 멤버변수를 다른 곳에서 사용하지 않는다면 멤버변수를 만들지 마시고 그냥 로컬변수만 사용하세요.

protected void onCreate(Bundle saveInstanceState) {
   ...
   View imageButton2 = findViewById(R.id.imageButton2);
   View imageButton = findViewById(R.id.imageButton);
   ...

}

 

spark (224,800 포인트) 님이 2021년 9월 18일 답변
정말 감사합니다 ㅠㅠㅠㅠㅠ 얼마 전에 자바 시작한 초보인데 이런 사이트가 있어서 정말 다행인 것 같네요. 진짜 감사합니다
...