Java Android Studio应用程序startet nicht aber mein代码是nicht Schuld

Java Android Studio应用程序startet nicht aber mein代码是nicht Schuld,java,android,nullpointerexception,Java,Android,Nullpointerexception,我是一个项目组,我是一个项目组 Hier ist die main activity.java package com.example.test; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button public class M

我是一个项目组,我是一个项目组

Hier ist die main activity.java

package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button

public class MainActivity extends AppCompatActivity {
      Button test = (Button) findViewById(R.id.button);
}
Hier ist meine activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="124dp"
android:layout_marginLeft="124dp"
android:layout_marginTop="152dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我不知道我的密码是什么,但我不知道

他是费勒梅尔东:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 8644
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
    at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
    at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
    at android.content.Context.obtainStyledAttributes(Context.java:675)
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
    at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
    at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
    at com.example.test.MainActivity.<init>(MainActivity.java:20)
    at java.lang.Class.newInstance(Native Method)
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
    at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
E/AndroidRuntime:致命异常:主
进程:com.example.test,PID:8644
java.lang.RuntimeException:无法实例化活动组件信息{com.example.test/com.example.test.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()”
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
位于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:尝试对空对象引用调用虚拟方法“android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()”
位于android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
在android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)中
位于android.content.Context.ActainStyledAttributes(Context.java:675)
位于androidx.appcompat.app.appcompatedelegateimpl.createSubDecor(appcompatedelegateimpl.java:692)
在androidx.appcompat.app.appcompatedelegateimpl.ensureubdecor(appcompatedelegateimpl.java:659)
位于androidx.appcompat.app.appcompatedelegateimpl.findViewById(appcompatedelegateimpl.java:479)
在androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)上
位于com.example.test.MainActivity。(MainActivity.java:20)
位于java.lang.Class.newInstance(本机方法)
位于android.app.AppComponentFactory.InstanceActivity(AppComponentFactory.java:69)
位于androidx.core.app.CoreComponentFactory.InstanceActivity(CoreComponentFactory.java:41)
位于android.app.Instrumentation.newActivity(Instrumentation.java:1215)
位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)

您不能在方法之外使用
findViewById

请为您的活动类实现
onCreate
,并将其放入其中

public class MainActivity extends AppCompatActivity {
      Button test;

      @Override
      public void onCreate(Bundle b) {
         test = (Button) findViewById(R.id.button)
      } 
}

请将您的问题翻译成英语-本网站的语言。否则很多人就无法理解你的问题。用上面的按钮做这个,“我的密码是nicht Falsch”-错误的前提谢谢