0

I am currently working on an Android app with Android Studio. I followed a Tuto to add the facebook login functions with picture and username . Then I created a new Activity once the Login is successful. But when I start the app, the app crash within 5 seconds with the message Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

Main activity :

public class MainActivity extends AppCompatActivity {

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

    Bundle inBundle = getIntent().getExtras();

    String name = inBundle.get("name").toString();
    String surname = inBundle.get("surname").toString();
    String imageUrl = inBundle.get("imageUrl").toString();


    if (AccessToken.getCurrentAccessToken() == null) {
        TextView nameView = (TextView)findViewById(R.id.nameAndSurname);
        nameView.setText("" + name + " " + surname);
        new DownloadImage((ImageView)findViewById(R.id.profileImage)).execute(imageUrl);

        goLoginScreen();
    }
}

private void goLoginScreen() {
    Intent intent = new Intent(this, LoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

public void logout(View view) {
    LoginManager.getInstance().logOut();
    goLoginScreen();
}

Activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tech.alvarez.androidfacebooklogin.MainActivity">


<TextView
    android:id="@+id/nameAndSurname"
    android:layout_width="77dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/profileImage"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="-143dp"
    android:textSize="22dp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/profileImage"
    android:layout_width="87dp"
    android:layout_height="101dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="190dp" />
<Button
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="logout"
    android:text="@string/logout" />

Motie Khyaoui
  • 31
  • 1
  • 6
  • Can you share which line you are getting the error? – Sethuraman Srinivasan May 02 '18 at 23:16
  • Try to _find out_ what line of code is crashing by using the **logcat**, then post that only. Also, post your FULL Logcat surrounded in back ticks (`). Btw, your error is in java; no need to post XML. – CodingM May 02 '18 at 23:16
  • I get the error on the following line as it considers inBundle as null : String name = inBundle.get(name).toString(); – Motie Khyaoui May 03 '18 at 08:53

0 Answers0