在空对象引用上接收 'android.content.Context android.content.Context.getApplicationContext()'
Receiving 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
我是 android 使用 java 开发和编写脚本的新手。我正在尝试创建一个允许我通过开关打开和关闭内部 microphone 的应用程序。我找到了两个脚本(一个开关脚本和一个 microphone 控制脚本),我将它们放在一起,纠正了所有调试问题,目前脚本显示干净。但是,当它在 phone 或模拟器上为 运行 时,它会立即崩溃并显示以下错误,
android.content.Context android.content.Context.getApplicationContext()' on a null object reference"focused around my use of import android.content.Context;.
错误似乎是由于 Context.getApplicationContext 请求未能找到结果并因此发布 NULL。
我看过一长串其他人也有类似问题,但所有解决方案似乎都侧重于更改他们代码的不同区域,而我无法将这些区域与我自己的脚本联系起来。
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
private Switch sw1;
Button btnGet;
Context context = getApplicationContext();
AudioManager audioManager = ((AudioManager)context.getSystemService(Context.AUDIO_SERVICE));
@Override
protected void onCreate(Bundle savedInstanceState) {
audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sw1 = findViewById(R.id.switch1);
btnGet = findViewById(R.id.getBtn);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sw1.isChecked()) {
audioManager.setMicrophoneMute(false);
}
else {
audioManager.setMicrophoneMute(true);
}
}
});
}
}
即使脚本不起作用,预期的结果是我能够 运行 应用程序,目前应用程序在启动时崩溃并发布以下错误消息...
[Logcat]
2019-08-20 13:23:01.710 5559-5559/com.example.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 5559
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: 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:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
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:106)
at com.example.myfirstapp.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
我猜,这里的问题是这一行:
Context context = getApplicationContext();
与其将其初始化为 class 字段,不如在 onCreate()
方法中对其进行初始化。
例如:
public class MainActivity extends AppCompatActivity {
private Switch sw1;
Button btnGet;
Context context;
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
audioManager = ((AudioManager)getSystemService(Context.AUDIO_SERVICE));
audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
context = getApplicationContext();
setContentView(R.layout.activity_main);
sw1 = findViewById(R.id.switch1);
btnGet = findViewById(R.id.getBtn);
.
.
您的问题是您试图在初始化时检索上下文,即。
Context context = getApplicationContext();
在创建 activity 之前,无法保证 getApplicationContext();
将 return 成为有效值。在这种情况下,它是空的,当你试图在下一行访问它时,你会得到一个空指针异常。您需要在 onCreate()
中分配变量 context
以及音频管理器。
比如像这样:
Context context;
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
audioManager = ((AudioManager)context.getSystemService(Context.AUDIO_SERVICE));
...
}
另一件需要注意的事情是 Activity
class 是 Context
的子 class,因此您不需要检索应用程序上下文,您可以取而代之写
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
audioManager = ((AudioManager)getSystemService(Context.AUDIO_SERVICE));
...
}
我是 android 使用 java 开发和编写脚本的新手。我正在尝试创建一个允许我通过开关打开和关闭内部 microphone 的应用程序。我找到了两个脚本(一个开关脚本和一个 microphone 控制脚本),我将它们放在一起,纠正了所有调试问题,目前脚本显示干净。但是,当它在 phone 或模拟器上为 运行 时,它会立即崩溃并显示以下错误,
android.content.Context android.content.Context.getApplicationContext()' on a null object reference"focused around my use of import android.content.Context;.
错误似乎是由于 Context.getApplicationContext 请求未能找到结果并因此发布 NULL。
我看过一长串其他人也有类似问题,但所有解决方案似乎都侧重于更改他们代码的不同区域,而我无法将这些区域与我自己的脚本联系起来。
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
private Switch sw1;
Button btnGet;
Context context = getApplicationContext();
AudioManager audioManager = ((AudioManager)context.getSystemService(Context.AUDIO_SERVICE));
@Override
protected void onCreate(Bundle savedInstanceState) {
audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sw1 = findViewById(R.id.switch1);
btnGet = findViewById(R.id.getBtn);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sw1.isChecked()) {
audioManager.setMicrophoneMute(false);
}
else {
audioManager.setMicrophoneMute(true);
}
}
});
}
}
即使脚本不起作用,预期的结果是我能够 运行 应用程序,目前应用程序在启动时崩溃并发布以下错误消息...
[Logcat]
2019-08-20 13:23:01.710 5559-5559/com.example.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 5559
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: 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:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
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:106)
at com.example.myfirstapp.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
我猜,这里的问题是这一行:
Context context = getApplicationContext();
与其将其初始化为 class 字段,不如在 onCreate()
方法中对其进行初始化。
例如:
public class MainActivity extends AppCompatActivity {
private Switch sw1;
Button btnGet;
Context context;
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
audioManager = ((AudioManager)getSystemService(Context.AUDIO_SERVICE));
audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
context = getApplicationContext();
setContentView(R.layout.activity_main);
sw1 = findViewById(R.id.switch1);
btnGet = findViewById(R.id.getBtn);
.
.
您的问题是您试图在初始化时检索上下文,即。
Context context = getApplicationContext();
在创建 activity 之前,无法保证 getApplicationContext();
将 return 成为有效值。在这种情况下,它是空的,当你试图在下一行访问它时,你会得到一个空指针异常。您需要在 onCreate()
中分配变量 context
以及音频管理器。
比如像这样:
Context context;
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
audioManager = ((AudioManager)context.getSystemService(Context.AUDIO_SERVICE));
...
}
另一件需要注意的事情是 Activity
class 是 Context
的子 class,因此您不需要检索应用程序上下文,您可以取而代之写
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
audioManager = ((AudioManager)getSystemService(Context.AUDIO_SERVICE));
...
}