构造函数中的参数(上下文、属性)是什么?我如何创建这个类的新对象? Java 和 Kotlin

发布于 2025-01-12 23:57:07 字数 4716 浏览 0 评论 0原文

我尝试创建声谱,我创建了显示频谱的类视图,但为了它工作,我必须创建这个类的新对象

我的视图类:

class WaveformView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private var paint = Paint()
    private var amplitudes = ArrayList<Float>()
    private var spikes = ArrayList<RectF>()
    private var radius = 6f
    private var w = 9f
    init {
        paint.color = (Color.rgb(244, 81,30))
    }
    fun addAmplitude(amp: Float){
        amplitudes.add(amp)

        var left = 0f
        var top = 0f
        var right = left + w
        var bottom = amp

        spikes.add(RectF(left, top, right, bottom))
    }

    override fun draw(canvas: Canvas?) {
        super.draw(canvas)
        /*canvas?.drawRoundRect(RectF(20f, 30f, 20+30f, 30f + 60f), 6f, 6f, paint)
        canvas?.drawRoundRect(RectF(60f, 60f, 60+80f, 60f + 360f), 6f, 6f, paint)*/
        spikes.forEach{
            canvas?.drawRoundRect(it, radius, radius, paint)
        }
    }

我的主类:

//Volume
    MediaRecorder mediaRecorder;
    WaveformView waveform = new WaveformView(MainActivity.this, null); <--- Error


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);..............

我在 Kotlin 类视图上编写,但 MainAct 我在 Java 上编写(我现在不使用 Kotlin,但我找到了决定)

我需要创建一个新对象来使用它

public void startRecording() {
        requestPerms();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

            waveform.addAmplitude(mediaRecorder.getMaxAmplitude()); <---here
        } else {
        Toast.makeText(this, "Sorry, your MIC don't work", Toast.LENGTH_SHORT).show();
        }
    }

我的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.imagewithcamera, PID: 889
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.imagewithcamera/com.example.imagewithcamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3737)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022)
        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:2336)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8653)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.content.ContextWrapper.getResources(ContextWrapper.java:101)
        at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
        at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
        at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:543)
        at android.view.View.<init>(View.java:5682)
        at android.view.View.<init>(View.java:5886)
        at android.view.View.<init>(View.java:5849)
        at android.view.View.<init>(View.java:5828)
        at com.example.imagewithcamera.WaveformView.<init>(WaveformView.kt:11)
        at com.example.imagewithcamera.MainActivity.<init>(MainActivity.java:62)
        at java.lang.Class.newInstance(Native Method)
        

 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

I try create sound spectrum, I created class view, which show spectrum, but to it work, i must create new object this class

My View class:

class WaveformView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private var paint = Paint()
    private var amplitudes = ArrayList<Float>()
    private var spikes = ArrayList<RectF>()
    private var radius = 6f
    private var w = 9f
    init {
        paint.color = (Color.rgb(244, 81,30))
    }
    fun addAmplitude(amp: Float){
        amplitudes.add(amp)

        var left = 0f
        var top = 0f
        var right = left + w
        var bottom = amp

        spikes.add(RectF(left, top, right, bottom))
    }

    override fun draw(canvas: Canvas?) {
        super.draw(canvas)
        /*canvas?.drawRoundRect(RectF(20f, 30f, 20+30f, 30f + 60f), 6f, 6f, paint)
        canvas?.drawRoundRect(RectF(60f, 60f, 60+80f, 60f + 360f), 6f, 6f, paint)*/
        spikes.forEach{
            canvas?.drawRoundRect(it, radius, radius, paint)
        }
    }

My Main class:

//Volume
    MediaRecorder mediaRecorder;
    WaveformView waveform = new WaveformView(MainActivity.this, null); <--- Error


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);..............

I wrote on Kotlin class view, but MainAct i wrote on Java (I don't now Kotlin, but i found decision)

I need create a new object to use it

public void startRecording() {
        requestPerms();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

            waveform.addAmplitude(mediaRecorder.getMaxAmplitude()); <---here
        } else {
        Toast.makeText(this, "Sorry, your MIC don't work", Toast.LENGTH_SHORT).show();
        }
    }

My Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.imagewithcamera, PID: 889
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.imagewithcamera/com.example.imagewithcamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3737)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022)
        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:2336)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8653)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.content.ContextWrapper.getResources(ContextWrapper.java:101)
        at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
        at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
        at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:543)
        at android.view.View.<init>(View.java:5682)
        at android.view.View.<init>(View.java:5886)
        at android.view.View.<init>(View.java:5849)
        at android.view.View.<init>(View.java:5828)
        at com.example.imagewithcamera.WaveformView.<init>(WaveformView.kt:11)
        at com.example.imagewithcamera.MainActivity.<init>(MainActivity.java:62)
        at java.lang.Class.newInstance(Native Method)
        

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2025-01-19 23:57:07

我找到了答案,我创建了

waveform = findViewById(R.id.waveformView); 

这个视图)))

I found answer, i create

waveform = findViewById(R.id.waveformView); 

this is view)))

最舍不得你 2025-01-19 23:57:07

而不是使用主构造函数,而是使用辅助构造函数 constructor(context: Context?, attrs: AttributeSet?)
并将对象正常方式定义为 kotlin

val wview= WaveformView(context)

instead of using primary constructor used secondary constructor constructor(context: Context?, attrs: AttributeSet?)
and define object normal way as kotlin

val wview= WaveformView(context)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文