1. Download our Official Android App: Forums for Android!

Apps Keep having error! New to app development

Discussion in 'Android Development' started by DanFletcher, Sep 19, 2016.

  1. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    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 (Text):
    1.  
    2. 09-19 17:31:19.238 2625-2625/com.dan.fletcher.golearnthai E/AndroidRuntime: FATAL EXCEPTION: main
    3.                                                                             Process: com.dan.fletcher.golearnthai, PID: 2625
    4.                                                                             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
    5.                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
    6.                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
    7.                                                                                 at android.app.ActivityThread.access$1100(ActivityThread.java:222)
    8.                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
    9.                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
    10.                                                                                 at android.os.Looper.loop(Looper.java:158)
    11.                                                                                 at android.app.ActivityThread.main(ActivityThread.java:7229)
    12.                                                                                 at java.lang.reflect.Method.invoke(Native Method)
    13.                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    14.                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    15.                                                                              Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
    16.                                                                                 at com.dan.fletcher.golearnthai.MainActivity.onCreate(MainActivity.java:18)
    17.                                                                                 at android.app.Activity.performCreate(Activity.java:6876)
    18.                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
    19.                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
    20.                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
    21.                                                                                 at android.app.ActivityThread.access$1100(ActivityThread.java:222)
    22.                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
    23.                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
    24.                                                                                 at android.os.Looper.loop(Looper.java:158)
    25.                                                                                 at android.app.ActivityThread.main(ActivityThread.java:7229)
    26.                                                                                 at java.lang.reflect.Method.invoke(Native Method)
    27.                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    28.                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    29. [code]
     

    Attached Files:


    Advertisement

    #1 DanFletcher, Sep 19, 2016
    Last edited: Sep 19, 2016
  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,718
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,718
    9,450
    1,488
    Male
    Software developer
    South West of England
    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 (Text):
    1.  
    2. Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
    3.   at com.dan.fletcher.golearnthai.MainActivity.onCreate(MainActivity.java:18)
    4.  
    This tells you that line 18 in your MainActivity is causing the problem. How do you think this could be resolved?
     
  3. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    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 (Text):
    1.  
    2. button = (Button) findViewById(R.id.alpha);
    3. [code]
    4.  
    5. Is it the word button at the start?
     
  4. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    This is the whole MainActivity.java:

    Code (Text):
    1.  
    2. package com.dan.fletcher.golearnthai;
    3.  
    4. import android.content.Intent;
    5. import android.support.v7.app.AppCompatActivity;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.widget.Button;
    9.  
    10. public class MainActivity extends AppCompatActivity {
    11.  
    12.     Button button;
    13.     @Override
    14.     protected void onCreate(Bundle savedInstanceState) {
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.activity_main);
    17.  
    18.  
    19.         button = (Button) findViewById(R.id.alpha);
    20.  
    21.         button.setOnClickListener(new View.OnClickListener() {
    22.         @Override
    23.         public void onClick(View v) {
    24.  
    25.             Intent intent = new Intent(MainActivity.this,ActivityAlpha.Class);
    26.             startActivity(intent);
    27.  
    28.  
    29.         }
    30.     });
    31.  
    32.     }
    33. }
    34.  
    35. [code]
     
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,718
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,718
    9,450
    1,488
    Male
    Software developer
    South West of England
    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.
     
  6. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    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! :)
     
  7. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,718
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,718
    9,450
    1,488
    Male
    Software developer
    South West of England
    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 (Text):
    1.  
    2. button = (Button) findViewById(R.id.alpha);
    3.  
    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 (Text):
    1.  
    2. button = (ImageButton)findViewById(R.id.alpha);
    3.  
     
    DanFletcher and HeavensSentSword like this.
  8. HeavensSentSword

    HeavensSentSword Well-Known Member
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    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
    Code (Java):
    1. ImageButton button;
    2.  
    3. button = (ImageButton)....
    You will get the same error if you don't start with a image button
     
    DanFletcher likes this.
  9. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    Thank you so much :) all sorted now!
     
  10. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,718
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,718
    9,450
    1,488
    Male
    Software developer
    South West of England
    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
     
    HeavensSentSword likes this.
  11. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    lol yh I am definitely going back to reading up again! Must of spent half the day yesterday trying to work that out!

    Again, thanks for your help!
     
    LV426 likes this.
  12. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,718
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,718
    9,450
    1,488
    Male
    Software developer
    South West of England
    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.
     
    HeavensSentSword likes this.
  13. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    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 >_<
     
  14. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,718
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,718
    9,450
    1,488
    Male
    Software developer
    South West of England
    What's the error?
     
  15. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    Code (Text):
    1.  
    2. at com.dan.fletcher.golearnthai.MainActivity$1.onClick(MainActivity.java:25)
     
  16. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    Code (Text):
    1.  
    2.  
    3. package com.dan.fletcher.golearnthai;
    4.  
    5. import android.content.Intent;
    6. import android.support.v7.app.AppCompatActivity;
    7. import android.os.Bundle;
    8. import android.view.View;
    9. import android.widget.Button;
    10. import android.widget.ImageButton;
    11.  
    12. public class MainActivity extends AppCompatActivity {
    13.  
    14.     ImageButton button;
    15.     @Override
    16.     protected void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         setContentView(R.layout.activity_main);
    19.  
    20.  
    21.         button = (ImageButton) findViewById(R.id.alpha);
    22.  
    23.         button.setOnClickListener(new View.OnClickListener() {
    24.         @Override
    25.         public void onClick(View v) {
    26.  
    27.             Intent intent = new Intent(MainActivity.this,ActivityAlpha.Class);
    28.             startActivity(intent);
    29.  
    30.  
    31.         }
    32.     });
    33.  
    34.     }
    35. }
     
  17. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,718
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,718
    9,450
    1,488
    Male
    Software developer
    South West of England
    Need to see a bit more of the stack trace there
     
  18. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    Code (Text):
    1.  
    2.  
    3. 09-20 09:59:00.028 25828-25828/com.dan.fletcher.golearnthai E/AndroidRuntime: FATAL EXCEPTION: main
    4.                                                                               Process: com.dan.fletcher.golearnthai, PID: 25828
    5.                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Class.getName()' on a null object reference
    6.                                                                                   at android.content.ComponentName.<init>(ComponentName.java:129)
    7.                                                                                   at android.content.Intent.<init>(Intent.java:5295)
    8.                                                                                   at com.dan.fletcher.golearnthai.MainActivity$1.onClick(MainActivity.java:25)
    9.                                                                                   at android.view.View.performClick(View.java:5697)
    10.                                                                                   at android.view.View$PerformClick.run(View.java:22526)
    11.                                                                                   at android.os.Handler.handleCallback(Handler.java:739)
    12.                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
    13.                                                                                   at android.os.Looper.loop(Looper.java:158)
    14.                                                                                   at android.app.ActivityThread.main(ActivityThread.java:7229)
    15.                                                                                   at java.lang.reflect.Method.invoke(Native Method)
    16.                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    17.                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     
  19. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    The app loads fine but crashed when i click on the ImageButton called "alpha", which should load the second activity "ActivityAlpha
     
  20. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,718
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,718
    9,450
    1,488
    Male
    Software developer
    South West of England
    I'm surprised that even compiles. The line

    Code (Text):
    1.  
    2. Intent intent = new Intent(MainActivity.this,ActivityAlpha.Class)
    3.  
    Should be

    Code (Text):
    1.  
    2. Intent intent = new Intent(MainActivity.this,ActivityAlpha.class)
    3.  
     
    DanFletcher likes this.
  21. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    So its just the capital C thats the issue?? Really need to be careful
     
  22. DanFletcher

    DanFletcher Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Sep 19, 2016

    Sep 19, 2016
    13
    1
    16
    Male
    Thats it mate :) thanks again for taking the time to help out a noob
     

Share This Page

Loading...