Home > Software design >  getSystemService NullPointerException
getSystemService NullPointerException

Time:12-26

    package com.example.myapplication

import android.app.Activity
import android.content.Context
import android.os.*
import android.util.Log
import android.view.DragEvent
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintSet
import androidx.wear.widget.SwipeDismissFrameLayout

class MetroMapActivity : Activity()
{
    private lateinit var image:ImageView
    private var gesture:ScaleGestureDetector? = null
    private var scaleFactor = 1.0f
    private lateinit var swipeview:SwipeDismissFrameLayout
    private lateinit var toast: Toast
    private lateinit var buttonone:Button
    private lateinit var vibratorManager: VibratorManager
    private lateinit var vibrator: Vibrator
    @RequiresApi(Build.VERSION_CODES.S)
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_metro_map)
        swipeview = findViewById(R.id.swipe)
        toast = Toast.makeText(this,"a",Toast.LENGTH_SHORT)
        image = findViewById(R.id.metromap)
        buttonone = findViewById<Button>(R.id.button)
        gesture = ScaleGestureDetector(this, ScaleListener())
        swipeview.setOnTouchListener(swipetouch())
        buttonone.setOnTouchListener(buttontouch())
        vibratorManager = this.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
        //vibrator = vibratorManager.defaultVibrator
        //vibrator.vibrate(VibrationEffect.createOneShot(100, 30))
        //image.setOnTouchListener(imagetouch())
    }

i'm making wearOS applincation.
when i call getSystemService() i get NullPointerException. it seems getSystemService() keep returns null but i don't know why.
i also wrote <uses-permission android:name="android.permission.VIBRATE"/ > in AndroidManifest.xml.
i can't even get clue. please help...please.... i once wrote getSystemService() at OnResume() because someone told me that it something is not initialized yet(?) but it also was not working.
this is stacktrace:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 27345
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type android.os.VibratorManager
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3456)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3612)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2073)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:7690)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
     Caused by: java.lang.NullPointerException: null cannot be cast to non-null type android.os.VibratorManager
        at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:31)
        at android.app.Activity.performCreate(Activity.java:8009)
        at android.app.Activity.performCreate(Activity.java:7993)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3429)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3612) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2073) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:7690) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995) 
I/Process: Sending signal. PID: 27345 SIG: 9

CodePudding user response:

VIBRATOR_MANAGER_SERVICE was added in API level 31 (Android 12) and devices running on lower API levels know nothing about it.

On older API levels you can use VIBRATOR_SERVICE instead to retrieve a Vibrator (rather than a VibratorManager).

  • Related