简体   繁体   中英

"null object reference" when trying to stop a service

I would like to stop a service but get this error:

Attempt to invoke virtual method 'boolean android.content.Context.stopService(android.content.Intent)' on a null object reference

This is my method (in class public class Dialog extends AppCompatActivity ) to stop the service:

public void stopService(Context context) {
    Intent serviceIntent = new Intent(context, PicovoiceService.class);
    stopService(serviceIntent);
}

This is how I invoke that method in another class:

@SuppressLint("ValidFragment")
public class Popup extends DialogFragment {
    private final int _layout;
    private final Dialog mDialog = new Dialog();
    public boolean dialogIsActive = false;

    @SuppressLint("ValidFragment")
    public Popup(int layout) {
        _layout = layout;
    } 

    @SuppressLint({"ClickableViewAccessibility", "ResourceType"})
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view =  inflater.inflate(_layout, container, false);

        // Display fragment_dialog
        if (_layout == R.layout.fragment_dialog) {

            // Toggle the listener
            view.findViewById(R.id.dialogCta).setOnClickListener(v -> {
                if (!dialogIsActive) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (mDialog.hasRecordPermission(getContext())) {
                            mDialog.startService(getContext());
                            dialogIsActive = true;
                        }
                    }
                }
                else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        mDialog.stopService(getContext()); // <-- ERROR
                    }
                    dialogIsActive = false;
                }
            });
        }           
        return view;
    }       
}

mDialog.startService(getContext()); works fine but what's wrong with mDialog.stopService(getContext()); ?

Update

I have also tried it with Intent serviceIntent = new Intent(getBaseContext(), PicovoiceService.class); and Intent serviceIntent = new Intent(getApplicationContext(), PicovoiceService.class); but I get the same error.

This is how I could make it work:

private Intent mServiceIntent = null;

public void startService(Context context) {
    mServiceIntent = new Intent(context, PicovoiceService.class);
    ContextCompat.startForegroundService(context, mServiceIntent);
}

public void stopTheService(Context context) {
    if (mServiceIntent != null) {
        context.stopService(mServiceIntent);
    }
}

The error was gone when I used context before stopService() .

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.

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