简体   繁体   English

Android Studio 活动未启动

[英]Android Studio Activity doesn't start

When starting my activity there is always an error message and the app does not open.开始我的活动时,总是有一条错误消息,并且应用程序无法打开。

Error:错误:

2022-05-11 23:18:12.470 17222-17222/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 17222
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapplication/com.example.feuerwerkzndanlage.Biometric_Authentication}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3455)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3699)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2135)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8056)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:133)
        at com.example.feuerwerkzndanlage.Biometric_Authentication.<init>(Biometric_Authentication.java:23)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1254)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3443)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3699) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2135) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:236) 
        at android.app.ActivityThread.main(ActivityThread.java:8056) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 

Activity:活动:

package com.example.feuerwerkzndanlage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;

import java.util.concurrent.Executor;

public class Biometric_Authentication extends AppCompatActivity {

    private TextView authStatusTv;
    private BiometricPrompt biometricPrompt;
    private BiometricPrompt.PromptInfo promptInfo;

    Context context = getApplicationContext();
    CharSequence error_txt = "Indentifizierung Fehlgeschlagen: ";
    CharSequence suceed_txt = "Indentifizierung Erfolgreich";
    CharSequence failed_txt = "Indentifizierung Fehlgeschlagen";
    int duration = Toast.LENGTH_SHORT;


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

        authStatusTv = findViewById(R.id.authStatusTv);
        Button btn_auth = findViewById(R.id.btn_auth);

        Executor executor = ContextCompat.getMainExecutor(this);
        biometricPrompt = new BiometricPrompt(Biometric_Authentication.this, executor, new BiometricPrompt.AuthenticationCallback() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                authStatusTv.setText("Indentifizierung Fehlgeschlagen: " + errString);
                Toast.makeText(context, error_txt, duration).show();
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                authStatusTv.setText("Indentifizierung Erfolgreich");
                Toast.makeText(context, suceed_txt, duration).show();
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                authStatusTv.setText("Indentifizierung Fehlgeschlagen");
                Toast.makeText(context, failed_txt, duration).show();
            }
        });

        promptInfo = new BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometrische Indentifizierung")
                .setSubtitle("Login")
                .setNegativeButtonText("Benutzer Passwort")
                .build();

        btn_auth.setOnClickListener(view -> biometricPrompt.authenticate(promptInfo));

    }
}
Context context = getApplicationContext();

You can't do that at init time.你不能在初始化时这样做。 You can't call any Context related functions, including getApplicationContect() until onCreate is called.在调用 onCreate 之前,您不能调用任何与 Context 相关的函数,包括 getApplicationContect()。 This is because the framework hasn't fully initialized the activity before then.这是因为在此之前框架还没有完全初始化活动。 To make this work, you'd need to move this line into onCreate, and move any code that depends on this variable being set into onCreate or later as well.要完成这项工作,您需要将此行移到 onCreate 中,并将依赖于此变量设置的任何代码移到 onCreate 或更高版本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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