简体   繁体   中英

My app is crashing when I try to go to another activity

I'm trying to make a clickable image button redirect to the mainactivity But since I've putted this code, the app crash when I click on the button to go to the 2nd Activity.

This id the first time I'm making an application.

Activity_good.java

public class Activity_good extends AppCompatActivity {

    private Button backGood;

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

        final TextView txtBien = (TextView) findViewById(R.id.txtBien);
        Button genBien = (Button) findViewById(R.id.genBien);
        final String[] pBien={"« Pour réussir, votre désir de réussite doit être plus grand que votre peur de l’échec.  » Bill Cosby", "trql", "oui", "non"};
        genBien.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int rando = (int) (Math.random()*4);
                txtBien.setText(pBien[rando]);
            }
        });

        backGood = (Button) findViewById(R.id.backGood);
        backGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMainActivity();
            }
        });
    }
    public void openMainActivity(){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
private Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final MediaPlayer pianoman = MediaPlayer.create(this, R.raw.piano);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pianoman.start();
                openActivity_good();
            }
        });
    }
    public void openActivity_good() {
        Intent intent = new Intent(this, Activity_good.class);
        startActivity(intent);
    }
}

Crash log ?

E/MediaPlayer: error (1, -19)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: fr.gab.artapp, PID: 9016
    java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.gab.artapp/fr.gab.artapp.Activity_good}: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
        at fr.gab.artapp.Activity_good.onCreate(Activity_good.java:30)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Application terminated.


I exprected to to get back on mainactivity when I click on the button backGood.+

Change this line:

backGood = (Button) findViewById(R.id.backGood);

To this:

backGood = (AppCompatImageButton) findViewById(R.id.backGood);

You are casting a AppCompatImageButton as a Button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM