'android.content.Context.getApplicationInfo()' 上的空指针异常

Null pointer exception on 'android.content.Context.getApplicationInfo()'

我是 android 的新手,只是把它当作周末开发的乐趣,试图制作一个非常简单的应用程序,但我遇到了错误。

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gwallet">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    Context context;
    final TextView txtNewDailyDetailDescription = findViewById(R.id.txt_new_daily_detail_description);
    final TextView txtNewDailyDetailSpend = findViewById(R.id.txt_new_daily_detail_spend);
    final Button btnNewDailyDetail = findViewById(R.id.btn_new_daily_detail);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        txtNewDailyDetailSpend.setInputType(InputType.TYPE_CLASS_NUMBER);
        btnNewDailyDetail.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                btnNewDailyDetailClick();
            }
        });
    }

    protected void btnNewDailyDetailClick() {
        //Some irrelevant code.
    }

}

错误的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gwallet, PID: 32507
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gwallet/com.gwallet.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:3027)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
    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:1955)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7091)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
 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:164)
    at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
    at android.content.Context.obtainStyledAttributes(Context.java:677)
    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.gwallet.MainActivity.<init>(MainActivity.java:18)
    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:1219)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3015)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264) 
    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:1955) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7091) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

我确实知道正在尝试使用未初始化的东西,但我不知道它是什么。我一直在阅读活动文档。有帮助吗?

让我知道这是否可行!

public class MainActivity extends AppCompatActivity {

    Context context;
    TextView txtNewDailyDetailDescription;
    TextView txtNewDailyDetailSpen;
    Button button;

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

txtNewDailyDetailDescription=findViewById(R.id.txt_new_daily_detail_description);
txtNewDailyDetailSpend = findViewById(R.id.txt_new_daily_detail_spend);
 btnNewDailyDetail = findViewById(R.id.btn_new_daily_detail);

        context = this;
        txtNewDailyDetailSpend.setInputType(InputType.TYPE_CLASS_NUMBER);
        btnNewDailyDetail.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                btnNewDailyDetailClick();
            }
        });
    }

    protected void btnNewDailyDetailClick() {
        //Some irrelevant code.
    }

}