0
votes

I'm just learning some basics of Java and have little problem with my app

This is part of my MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            EditText edit = (EditText) findViewById(R.id.editText);
            if (edit.getText().toString().equals("admin")) {
                Intent gz = new Intent(getApplicationContext(), gz.class);
                startActivity(gz);
            }

        }
    });
}

I would like that app changed activity from mainActivity to gz activity after click button and check EditText edit.

At this stage app just crash.

--------- beginning of crash 08-16 12:50:38.284 9345-9345/com.example.fiery.app1 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fiery.app1, PID: 9345 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fiery.app1/com.example.fiery.app1.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.support.design.widget.FloatingActionButton at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) at android.app.ActivityThread.access$900(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5551) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.support.design.widget.FloatingActionButton at com.example.fiery.app1.MainActivity.onCreate(MainActivity.java:25) at android.app.Activity.performCreate(Activity.java:6272) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)  at android.app.ActivityThread.access$900(ActivityThread.java:157)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5551)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

1
Do you have the stacktrace of the crash? If yes, posting it would be very helpful to diagnose the problem. - Danail Alexiev
@DanailAlexiev i have no idea how to find that. - Panfery
please share your log - Shailesh
Why not copy and paste it in the question. The important bit is missing from the image. But you are trying to cast some widget.AppComponent... into some container that is either not widget.AppComponent... or does not extend it. - Abbas

1 Answers

0
votes

you are intialize your button in wrong way,You can try to cast yout widget button to FloatingActionButtonyou and it's not possible please just change Like this

Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            EditText edit = (EditText) findViewById(R.id.editText);
            if (edit.getText().toString().equals("admin")) {
                Intent gz = new Intent(getApplicationContext(), gz.class);
                startActivity(gz);
            }

        }
    });