简体   繁体   中英

What is parameters in constructor (context, attrs)? How I can create new object this class? Java and Kotlin

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)
        

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

I found answer, i create

waveform = findViewById(R.id.waveformView); 

this is view)))

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

val wview= WaveformView(context)

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