0

my app sometime crashes, i know where but i don't know why. Please watch this code:

private Button btFacebook     
@Override   
public final void onCreate(Bundle savedInstanceState) 
{
    ....
    btFacebook     = (Button) findViewById(R.id.bt_fb_zaloguj);
    btFacebook.setText(getResources().getString(R.string.act_logowanie_bt_facebook).toUpperCase()); //line crash 
    .... 
}

Why btFacebook sometime is null?

EDIT:

> java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.gosell.ghmaster/pl.gosell.ghmaster.activity.ActSplash}: java.lang.NullPointerException
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
android.app.ActivityThread.access$900(ActivityThread.java:161)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:157)
android.app.ActivityThread.main(ActivityThread.java:5356)8at java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
pl.gosell.ghmaster.activity.ActSplash.onActivityData(ActSplash.java:184)
com.android.lifter.activity.ActivityBase.onCreate(ActivityBase.java:99)
android.app.Activity.performCreate(Activity.java:5426)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
19... 11 more
2
  • 1
    Where is your logcat? post it. Commented Oct 3, 2014 at 7:17
  • If your application is crashing on the loading, it might me because of the above, if it's crashing after loading, it means whatever functionality that button is doing is causing the crash. Only way to answer this is if you post the logcat of the error, otherwise people are giving answers blindly. Commented Oct 3, 2014 at 9:15

3 Answers 3

2

You missed

  setContentView(R.layout.your_layout);

and then you initialized button like:

protected  void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
btFacebook     = (Button) findViewById(R.id.bt_fb_zaloguj);
btFacebook.setText(getResources().getString(R.string.act_logowanie_bt_facebook).toUpperCase());      //line crash 
.... 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for reply, but i don't missed setContentView(R.layout.your_layout);
@michael It's totally fake that you said my app sometime crashes
1

Looking at your code if u missed this.

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

then add it

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

    btFacebook = (Button) findViewById(R.id.bt_fb_zaloguj);
    btFacebook.setText(getResources().getString(R.string.act_logowanie_bt_facebook).toUpperCase()); 

1 Comment

Thank you for reply, but i don't missed super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
-1

If you are already in Activity you can simplify you call like this

this.getString(R.string.act_logowanie_bt_facebook)

Also if string is there it should not give exception. I think the exception you are getting is because of Button. Somehow button object is null. Can you have a look at your view and check if the id is same ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.