Home > Mobile >  Android Studio "Intent" not working even after importing "android.content.Intent"
Android Studio "Intent" not working even after importing "android.content.Intent"

Time:04-29

I am trying out a very simple intent example follow this youtube video. However, i facing a very weird error where this particular line cannot work:

Intent myIntent = new Intent(this, DisplayActivity.class)

It provide me error as shown in the picture: Error

I also had tried out the "bulb" button in AS to debug it but it didn't show me a valid solution. The suggested action is as shown in the picture Original AS code editor image

The full code is shown below:

package com.example.parcelsort_ar

import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.budiyev.android.codescanner.*
import com.example.parcelsort_ar.databinding.ActivityMainBinding
import android.content.Intent

private const val CAMERA_REQUEST_CODE = 101

class MainActivity : AppCompatActivity() {
    private lateinit var codeScanner: CodeScanner
    private lateinit var binding: ActivityMainBinding 
    val btn_click_me = findViewById(R.id.idBtnPost) as Button

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)

            //View binding
            binding = ActivityMainBinding.inflate(layoutInflater)
            val view = binding.root
            setContentView(view)

            binding.idBtnPost.setOnClickListener {
                openActivity2();
            }

  
            setupPermission()
            codeScanner()
    }

    public fun openActivity2() {
        Intent myIntent = new Intent(this, DisplayActivity.class);
        startActivity(intent);
    }
}

I had spending almost a day searching online but couldn't found any issue that meet my problem. Any help is aprreciated.

CodePudding user response:

Intent myIntent = new Intent(this, DisplayActivity.class); is Java, not Kotlin. So it's getting to that first Intent and it doesn't know what to do with it.

You want this:

val myIntent = Intent(this, DisplayActivity::class.java)

The IDE should have caught it and offered to convert it to Kotlin, if you pasted the line in

CodePudding user response:

moving from activity to activity

val myIntent = Intent(this, DisplayActivity::class.java)  //you will use this code

moving from fragment to activity

val myIntent = Intent(requireContext(), DisplayActivity::class.java)
  • Related