• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Keep having error! New to app development

Hi was just wondering if anyone here is able to help me. I am making an app and have just created a new activity and am trying to load that activity when I click on a ImageButton. I thought I had done it correctly but I am now getting a crash every time i go to run the app.

Does this error message make sense to anyone? Would really appreciate some help! Thank you :)

Code:
09-19 17:31:19.238 2625-2625/com.dan.fletcher.golearnthai E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.dan.fletcher.golearnthai, PID: 2625
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dan.fletcher.golearnthai/com.dan.fletcher.golearnthai.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                                                                                at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:158)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                             Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
                                                                                at com.dan.fletcher.golearnthai.MainActivity.onCreate(MainActivity.java:18)
                                                                                at android.app.Activity.performCreate(Activity.java:6876)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                                                                                at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:158)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
[code]
 

Attachments

  • Screen Shot 2016-09-19 at 17.44.54.png
    Screen Shot 2016-09-19 at 17.44.54.png
    130.5 KB · Views: 253
  • Screen Shot 2016-09-19 at 17.44.49.png
    Screen Shot 2016-09-19 at 17.44.49.png
    75.2 KB · Views: 234
  • Screen Shot 2016-09-19 at 17.44.41.png
    Screen Shot 2016-09-19 at 17.44.41.png
    182.3 KB · Views: 251
  • Screen Shot 2016-09-19 at 17.44.29.png
    Screen Shot 2016-09-19 at 17.44.29.png
    179 KB · Views: 303
  • Screen Shot 2016-09-19 at 17.44.15.png
    Screen Shot 2016-09-19 at 17.44.15.png
    502.8 KB · Views: 267
  • Screen Shot 2016-09-19 at 17.43.56.png
    Screen Shot 2016-09-19 at 17.43.56.png
    898.9 KB · Views: 305
Last edited:
First thing is, you should become familiar with how to read a stack trace, because it tells you exactly where the problem is in your code. Basically you are looking for any lines which relate to your code, not framework library code.
So the relevant part of this stack trace is

Code:
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
  at com.dan.fletcher.golearnthai.MainActivity.onCreate(MainActivity.java:18)

This tells you that line 18 in your MainActivity is causing the problem. How do you think this could be resolved?
 
Thank you very much for replying. Yes I definitely will look into how to read the errors.

I supose the button link is not working? This is line 18:
Code:
button = (Button) findViewById(R.id.alpha);
[code]

Is it the word button at the start?
 
This is the whole MainActivity.java:

Code:
package com.dan.fletcher.golearnthai;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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


        button = (Button) findViewById(R.id.alpha);

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,ActivityAlpha.Class);
            startActivity(intent);


        }
    });

    }
}

[code]
 
Read the exception error. You have a ClassCastException.

Do you know what it means to cast an object? If you don't know this, you need to step back a bit and do some reading about the Java language. Continuing further will cause you many problems. Please find a good book, or online tutorial and understand the basics before you continue.
 
I have been reading and watching many online sources and this is where I've got to. I thought maybe I have a stray bit of text wrong somewhere that I have missed, but I guess what I have done is a complete mess? lol

Thanks for taking your time to give me a kick up the arse! :)
 
It's not a complete mess, you've just declared button to be of type Button, where it should be ImageButton. The exception you're getting is because an object of type ImageButton can't be assigned to an object of type Button.

By using the line

Code:
button = (Button) findViewById(R.id.alpha);

You're telling the compiler that what method findViewById() returns is actually an object of type Button. This catches you out at runtime, because what findViewById is actually returning, is an object of type ImageButton.

All you need to do is change the type of the button variable to ImageButton, and change the above to

Code:
button = (ImageButton)findViewById(R.id.alpha);
 
Adding to help clarify it for a new user and new programmer, what LV426 said is right but you also need to declare it right to like this
Java:
ImageButton button;

button = (ImageButton)....

You will get the same error if you don't start with a image button
 
Adding to help clarify it for a new user and new programmer, what LV426 said is right but you also need to declare it right to like this
Java:
ImageButton button;

button = (ImageButton)....

You will get the same error if you don't start with a image button

yeah I did say that, but not explicitly in code. People need to work some things out themselves in order to learn ;) :D:D:D:D
 
Adding to help clarify it for a new user and new programmer, what LV426 said is right but you also need to declare it right to like this
Java:
ImageButton button;

button = (ImageButton)....

You will get the same error if you don't start with a image button

Sorry but the pedant in me must respond :eek: :D
You wouldn't get the same error, because the compiler can work out that you're trying to cast an object to an ImageButton, when you have in fact declared the receiving variable as a Button. So this would be a compile time error, as opposed to a runtime error.
 
My next error is this:
Intent intent = new Intent(MainActivity.this,ActivityAlpha.Class);

Is it still down to me originally not setting it up as a ImageButton?

I dont quite see what is wrong with this line to be honest >_<
 
Code:
package com.dan.fletcher.golearnthai;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

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


        button = (ImageButton) findViewById(R.id.alpha);

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,ActivityAlpha.Class);
            startActivity(intent);


        }
    });

    }
}
 
Code:
09-20 09:59:00.028 25828-25828/com.dan.fletcher.golearnthai E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.dan.fletcher.golearnthai, PID: 25828
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Class.getName()' on a null object reference
                                                                                  at android.content.ComponentName.<init>(ComponentName.java:129)
                                                                                  at android.content.Intent.<init>(Intent.java:5295)
                                                                                  at com.dan.fletcher.golearnthai.MainActivity$1.onClick(MainActivity.java:25)
                                                                                  at android.view.View.performClick(View.java:5697)
                                                                                  at android.view.View$PerformClick.run(View.java:22526)
                                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:158)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 
I'm surprised that even compiles. The line

Code:
Intent intent = new Intent(MainActivity.this,ActivityAlpha.Class)

Should be

Code:
Intent intent = new Intent(MainActivity.this,ActivityAlpha.class)
 
Back
Top Bottom