简体   繁体   中英

Android Nullpointer exception button

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

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 
.... 
}

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()); 

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM