-3

I have a java.lang.NullPointerException error with this code:

A= (ImageButton) findViewById( R.id.button);
A.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        setContentView(R.layout.activity_prof);

    }
});

The app works in my phone, Samsung Galaxy Ace Plus S7500, but it crashes in the emulator with Nexus 5 and Samsung Galaxy S4. Could anyone help me?

logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{testteam.test/testteam.test.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at testteam.test.MainActivity.onCreate(MainActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
ClearCode
  • 47
  • 4
  • 11
  • could you please post ur logcat? – Milad Faridnia Feb 21 '15 at 19:04
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand the stack trace, edit your question and post the stack trace, and indicate what lines in this code correspond to lines in the stack trace. – CommonsWare Feb 21 '15 at 19:04
  • 2
    if you are using setContentView(R.layout.activity_prof); in onClick listerner then how do you get referance of that A imageview, plz post complete code – KOTIOS Feb 21 '15 at 19:09
  • 1
    @Milad I put LogCat. A is a variable : private ImageButton. There isn't any other code – ClearCode Feb 21 '15 at 19:15
  • as you see your error occurs at line 24 of your code in onCreate() method. please post your oncreate() method too. And of course dont forget to post your fields. – Milad Faridnia Feb 21 '15 at 19:19
  • @Milad I know the problem, it's View.OnClickListner() ! I don't know why this error occur in a newer version of Android. In my phone this code works great! – ClearCode Feb 21 '15 at 19:25
  • clean your project and then test it on emulator. also check your xml you must have a `ImageButton` in your xml code in layout folder, maybe it helps – Milad Faridnia Feb 21 '15 at 19:33
  • this error occurs when you did not initialise your field, so it throws NPE. just make sure you initialize your view corrctly. – Milad Faridnia Feb 21 '15 at 19:43
  • @Milad I think is a problem with Android version, I didn't do any mistake and also my code works in my phone. If it was an error of intialization it couldn't work in my phone. – ClearCode Feb 21 '15 at 20:01
  • 1
    @ClearCode How your App works while you are using this code `A= (ImageButton) findViewById( R.id.button);` before creating your layout with `setContentView(R.layout.activity_prof);`? This should get you error! – Morteza Feb 21 '15 at 20:09
  • @Morteza I just posted the code that give me problem! It's obvious that this will give me error... Where is the class? and the layout? I only posted the code with error! – ClearCode Feb 21 '15 at 20:17
  • @ClearCode So edit your question and add the whole class. – Morteza Feb 21 '15 at 20:24
  • @Morteza Stop asking me for the entire code! It's useless! I just have 1 variable and this code, everything in OnCreate method, there isn't any other code, just 2 layout with a button! – ClearCode Feb 21 '15 at 20:46
  • @ClearCode Well you said that you have posted only error causing codes and asked me where is the class! and now you are saying this is the whole code, So if this is the code as I told you should get error this is normal. – Morteza Feb 21 '15 at 20:54
  • @Morteza Sorry for my bad english, I wanted to say that setContentView() and standard code to start app are in my file, my app run!It works! I can use my app! the error is in these lines, I can't post the entire code because it is useless, declaration of layout is in my file ok? So stop asking me to post code, It's useless to post entire code because there is anything to say about the entire code because I created the project and just added these lines! – ClearCode Feb 21 '15 at 21:25
  • @ClearCode What exactly you want to do with these codes? – Morteza Feb 21 '15 at 21:33
  • @Morteza This code cause to my app a crash for NullPointerException, the strange thing is that the app crash just in some Android, for example in Nexus 5 the app crash, in samsung galaxy ace the app works without crash! – ClearCode Feb 21 '15 at 22:23
  • @ClearCode Check if on your mobile it works fine maybe the `button` doesnt work there too if it does then this would be really strange! – Morteza Feb 21 '15 at 22:37
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ali Bdeir Dec 30 '16 at 21:25

1 Answers1

1

These kinds of errors usually come from finding wrong views. Please double check your xml layout and make sure you have an ImageButton named button in xml.

It seems this ImageButton is not in your current view. It might be because you haven't call setContentView method before setting onClickListener in onCreatMethod

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78