【问题标题】:Unable to start activity ComponentInfo - Android无法启动活动 ComponentInfo - Android
【发布时间】:2020-01-18 12:30:38
【问题描述】:

我正在为 Android 创建一个移动应用程序,但在通过 Google 下载连接后出现问题

应用程序崩溃。谁能给我一个理由以及如何附上它?

主要活动触手可及。

public class MainActivity extends AppCompatActivity {
GoogleSignInClient mGoogleSignInClient;
private int RC_SIGN_IN = 3;
SignInButton signInButton;

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

    signInButton = findViewById(R.id.sign_in_button);

    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.sign_in_button:
                    signIn();
                    break;
                // ...
            }
        }
    });
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

}
private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        // The Task returned from this call is always completed, no need to attach
        // a listener.
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);

        Intent intent = new Intent(MainActivity.this, MenuActivity.class);
        startActivity(intent);
    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        Log.w("TAG", "signInResult:failed code=" + e.getStatusCode());
        // updateUI(null);
    }
}

}

登录后的目标活动:

public class MenuActivity extends AppCompatActivity  {

GoogleSignInClient mGoogleSignInClient;
Button logoutBtn;
TextView userName;
ImageView profileImage;
private GoogleSignInOptions gso;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    logoutBtn=(Button)findViewById(R.id.button_wyl);
    profileImage=(ImageView)findViewById(R.id.profileImage);
    userName = findViewById(R.id.name);
    logoutBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                // ...
                case R.id.button_wyl:
                    signOut();
                    break;
                // ...
            }
        }
    });

    GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);
    if (acct != null) {
        String personName = acct.getDisplayName();
        Uri personPhoto = acct.getPhotoUrl();

        userName.setText(personName);
        Glide.with(this).load(String.valueOf(personPhoto)).into(profileImage);

    }
}

private void signOut() {
    mGoogleSignInClient.signOut()
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    Toast.makeText(MenuActivity.this, "Signed out Successfully", Toast.LENGTH_LONG).show();
                    finish();
                }
            });
}

}

例外:

E/AndroidRuntime: 致命异常: main 进程:com.example.goodmath,PID:1780 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.goodmath/com.example.goodmath.MenuActivity}: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6669) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 引起:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 com.example.goodmath.MenuActivity.onCreate(MenuActivity.java:46) 在 android.app.Activity.performCreate(Activity.java:7136) 在 android.app.Activity.performCreate(Activity.java:7127) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6669) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) I/Process:发送信号。 PID:1780 SIG:9

【问题讨论】:

    标签: java android xml firebase-authentication


    【解决方案1】:

    您没有在onCreate()MenuActivity 中调用setContentView(),因此您的findViewById() 查找将失败。因此,logoutBtnnull,因此当您尝试对其调用方法时,您会以 NullPointerException 崩溃。

    【讨论】:

    • logoutBtn=(Button)findViewById(R.id.button_wyl); profileImage=(ImageView)findViewById(R.id.profileImage);用户名 = findViewById(R.id.name); setContentView(R.layout.activity_menu);
    • @Lamps:先调用setContentView(),然后再调用findViewById()
    • setContentView(R.layout.activity_menu); logoutBtn=(Button)findViewById(R.id.button_wyl);
    • @Lamps:那么显然,res/layout/activity_menu.xml 中没有带有android:id="@+id/button_wyl" 的小部件。
    【解决方案2】:

    实际错误是:

    java.lang.NullPointerException: Attempt to invoke virtual method 'void 
    android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 
    on a null object
    

    所以 findViewById 失败了。

    在 super.onCreate() 之后直接做setContentView(R.layout.activity_menu)

    如果这不起作用,您确定您引用的视图在 activity_menu 布局文件中吗?

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2013-03-23
      相关资源
      最近更新 更多