【问题标题】:Attempt to invoke virtual method 'void android.graphics.Canvas.drawText()' on a null object reference尝试在空对象引用上调用虚拟方法“void android.graphics.Canvas.drawText()”
【发布时间】:2018-06-24 21:30:07
【问题描述】:

我想在屏幕上绘制文字,但收到错误消息:

java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void android.graphics.Canvas.drawText(java.lang.String, float, float, android.graphics.Paint)'

我的代码:

package com.example.root.runningmanv2

import android.content.Context
import android.graphics.Paint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.SurfaceView
import android.view.WindowManager

class GamingActivity : AppCompatActivity() {
    private lateinit var gameView: SurfaceGame

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //set fullscreen
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)
        gameView = SurfaceGame(this)
        setContentView(gameView)
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        gameView.start()
    }

    inner class SurfaceGame(context: Context): SurfaceView(context), Runnable{
        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        val ourHolder = holder
        val canvas = ourHolder.lockCanvas()

        fun start(){
            val thread = Thread(this)
            thread.start()
        }

        override fun run() {
            drawFigure()
        }

        fun drawFigure(){
            paint.setARGB(255, 255, 255, 255)
            canvas.drawText("Hello", 500f, 500f, paint)
            ourHolder.unlockCanvasAndPost(canvas)
        }

    }
}

如何解决?

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    您自己从 SurfaceView.Callback 类中获取 holder 而不是在 SurfaceGame 类中获取它

    class GamingActivity : SurfaceHolder.Callback, AppCompatActivity() {
    
        private var holder: SurfaceHolder? = null
        private var pos = 0
    
        public override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            val view = SurfaceView(this)
            setContentView(view)
            view.holder.addCallback(this)
        }
    
        override fun surfaceCreated(holder: SurfaceHolder) {
            this.holder = holder
            pos = 0
            Thread({
                while (this.holder != null) {
                    drawFigure(this.holder)
                    Thread.sleep(100)
                }
            }).start()
        }
    
        override fun surfaceChanged(holder: SurfaceHolder, frmt: Int, w: Int, h: Int) {
            this.holder = holder
        }
    
        override fun surfaceDestroyed(holder: SurfaceHolder) {
            this.holder = null
        }
    
        private fun drawFigure(holder: SurfaceHolder?) {
    
            if (holder == null) return
    
            val canvas = holder.lockCanvas()
            val paint = Paint(Paint.ANTI_ALIAS_FLAG)
            paint.setARGB(255, 0, 0, 0)
            paint.textSize = 96f
            canvas.drawText("Hello", 100f + pos, 500f, paint)
            pos++
            holder.unlockCanvasAndPost(canvas)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 2018-05-22
      相关资源
      最近更新 更多