我目前正在使用Android Studio开发Android应用。我跟着Tuto添加了图片和用户名的facebook登录功能。然后,一旦登录成功,我就创建了一个新的Activity。但是当我启动应用程序时,应用程序在5秒内崩溃并显示消息尝试在空对象引用上调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'
主要活动:
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" />