I am new to android development and scripting with java. I am trying to create an app that allows me to toggle the internal microphone on and off via a switch. I found two scirpts (a switch script and a microphone control script) and I have peaced them together, correcting any debugging issues as I go and currently the script shows up clean. However when it is run on a phone or simulator it crashes immediately posting the following error,
android.content.Context android.content.Context.getApplicationContext()' on a null object reference"focused around my use of import android.content.Context;.
The error appears to be based on the Context.getApplicationContext request failing to find a result and thus posting NULL.
I have looked at a long list of other people suffering from similar areas, but all solutions seem to focus on changes to different areas of their code which I cannot relate back to my own scripts.
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);
}
}
});
}
}
The expected result, even if the script doesn't work, is that I am able to run the app, currently the app crashes on startup and posts the following error message...
[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)
Your issue is that you are attempting to retrieve the context at initialization ie.
Context context = getApplicationContext();
There is no guarantee that getApplicationContext();
will return a valid value until the activity has been created. In this case it is null and when you attempt to access it on the next line you get a null pointer exception. You need to instead assign the variable context
within the onCreate()
along with the audio manager.
For example, like so:
Context context;
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
audioManager = ((AudioManager)context.getSystemService(Context.AUDIO_SERVICE));
...
}
Another thing to note is that the Activity
class is a subclass of Context
, so you do not need to retrieve the application context you can instead write
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
audioManager = ((AudioManager)getSystemService(Context.AUDIO_SERVICE));
...
}
I guess, the problem here is this line:
Context context = getApplicationContext();
Instead of initializing it as a class field, initialize it inside onCreate()
method. For Example:
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);
.
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.