Home > Enterprise >  FATAL EXCEPTION when Forwarding Intent (URL) in kotlin
FATAL EXCEPTION when Forwarding Intent (URL) in kotlin

Time:08-02

WireFrame & Guide about what i want to ask

---> Full Code for more details https://github.com/BUdiAKmal/CaseApp

After passing URL from "RecyclerView" to Button on "DetailsActivity" , i try to forwarding and load the URL to "WebViewActivity" when user click the "Button on DeetailsActivity".

and finally i get this error :

    FATAL EXCEPTION: main
    Process: com.caseapp, PID: 5139
    java.lang.StackOverflowError: stack size 8MB
        at android.os.Parcel.-get0(Unknown Source:0)
        at android.os.Parcel$ReadWriteHelper.writeString(Parcel.java:353)
        at android.os.Parcel.writeString(Parcel.java:675)
        at android.content.ComponentName.writeToParcel(ComponentName.java:320)
        at android.content.ComponentName.writeToParcel(ComponentName.java:335)
        at android.content.Intent.writeToParcel(Intent.java:9566)
        at android.os.Parcel.writeParcelable(Parcel.java:1791)
        at android.os.Parcel.writeValue(Parcel.java:1697)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
        at android.os.Bundle.writeToParcel(Bundle.java:1232)
        at android.os.Parcel.writeBundle(Parcel.java:878)
        at android.content.Intent.writeToParcel(Intent.java:9599)
        at com.caseapp.ui.activities.DetailsActivity.initView$lambda-2$lambda-1(DetailsActivity.kt:68)
        at com.caseapp.ui.activities.DetailsActivity.$r8$lambda$JHNmedXwzuffRCNJln7AS9uz4NI(Unknown Source:0)
        at com.caseapp.ui.activities.DetailsActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)

MainActivity.kt

class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // Start - full screen
        val window = window
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN

        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        ) // End - full screen
        
        setUpTabs() // Setup TabLayout

    } // End - OnCreate

    // Start - TabLayout
    private fun setUpTabs() {
        val adapter = ViewPagerAdapter(supportFragmentManager)
        adapter.addFragment(MainTabFragment1(), "MainTab1")
        adapter.addFragment(MainTabFragment1(), "MainTab2")
        viewPager_tabLayout.adapter = adapter
        tabs.setupWithViewPager(viewPager_tabLayout)
    } // End - TabLayout

} // End - Class

MainTabFragment1.kt

class MainTabFragment1 : Fragment() {
    
    lateinit var mainTabFragment1Adapter: MainTabFragment1Adapter  // Initialize Adapter
    private val sLM = LinearLayoutManager(context) // Initialize layoutManager
    val addMainTabFragment1ModelList: MutableList<MainTabFragment1Model> = ArrayList() // Initialize listModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment1_main_tab, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initViewMainTabFragment1() // SetUp initView listModel
        actionMainTabFragment1() // SetUp actionMainTabFragment1
        
    } // End - OnCreate

    // Start - intView listModel
    private fun initViewMainTabFragment1() {
        rv_mainTabFragment1.layoutManager = sLM
        mainTabFragment1Adapter = MainTabFragment1Adapter(requireActivity())
        rv_mainTabFragment1.adapter = mainTabFragment1Adapter

        addMainTabFragment1ModelList.add(
            MainTabFragment1Model(
                "https://images.unsplash.com/photo-1585435465945-bef5a93f8849",
                "Title RVItemNo.1 MainTabFragment1",
                "Desc RVItenNo.1 MainTabFragment1",
                "https://google.com"
            )
        )
        addMainTabFragment1ModelList.add(
            MainTabFragment1Model(
                "https://images.unsplash.com/photo-1487846698364-db1316e3d140",
                "Title RVItemNo.2 MainTabFragment1",
                "Desc RVItenNo.2 MainTabFragment1",
                "https://youtube.com"
            )
        )
        addMainTabFragment1ModelList.add(
            MainTabFragment1Model(
                "https://images.unsplash.com/photo-1512295767273-ac109ac3acfa",
                "Title RVItemNo.3 MainTabFragment1",
                "Desc RVItenNo.3 MainTabFragment1",
                "https://gmail.com"
            )
        )
        addMainTabFragment1ModelList.add(
            MainTabFragment1Model(
                "https://images.unsplash.com/photo-1525896969906-0a4806967ef0",
                "Title RVItemNo.4 MainTabFragment1",
                "Desc RVItenNo.4 MainTabFragment1",
                "https://blgger.com"
            )
        )

        mainTabFragment1Adapter.setMainTabFragment1(addMainTabFragment1ModelList)
    } // End - intView listModel

    // Start - putExtra 
    fun actionMainTabFragment1() {
        mainTabFragment1Adapter.setOnClickItemListenerMainTabFragment1(object : OnItemClickListener {
            override fun onItemClick(item: View, position: Int) {
                val i = Intent(context, DetailsActivity::class.java)
                i.putExtra(
                    "titleTextMainTabFragment1",
                    mainTabFragment1Adapter
                        .getMainTabFragment1()
                        .get(position)
                        .titleTextMainTabFragment1
                )
                i.putExtra(
                    "descTextMainTabFragment1",
                    mainTabFragment1Adapter
                        .getMainTabFragment1()
                        .get(position)
                        .descTextMainTabFragment1
                )
                i.putExtra(
                    "imgURLMainTabFragment1",
                    mainTabFragment1Adapter
                        .getMainTabFragment1()
                        .get(position)
                        .imgURLMainTabFragment1
                )
                i.putExtra(
                    "btnURLMainTabFragment1",
                    mainTabFragment1Adapter
                        .getMainTabFragment1()
                        .get(position)
                        .btnURLMainTabFragment1
                )
                startActivity(i)
            }
        })
    } // End - putExtra 
    
}

DetailsActivity.kt

class DetailsActivity : AppCompatActivity() {

    var prodBundle: Bundle? = null // Initialize getString (bundle) from MainActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_details)


        // Start - full screen
        val window = window
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN

        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        ) // End - full screen


        initView() // Setup getString from RecyclerView Main Activity

    } // End - OnCreate


    // Start - getString from RecyclerView Main Activity
    fun initView() {
        prodBundle = intent.extras


        tv_titleText.text = prodBundle?.getString("titleTextMainTabFragment1")
        tv_descText.text = prodBundle?.getString("descTextMainTabFragment1")

        prodBundle?.getString("imgURLMainTabFragment1").let {
            Glide.with(this).asBitmap()
                .load(it)
                .into(iv_imgURL)

            Glide.with(this).asBitmap()
                .load(it)
                .centerCrop()
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
        }

        prodBundle?.getString("btnURLMainTabFragment1").let {
          btn_wvURL.setOnClickListener {
              val gotoWebView = Intent(this@DetailsActivity, WebViewActivity::class.java)
              gotoWebView.putExtra("btnURLMainTabFragment1", gotoWebView)
              startActivity(gotoWebView)
          }
        }
        
        } //  End  - getString from RecyclerView Main Activity }

} // End - Class

MainTabFragment1Adapter.kt

class MainTabFragment1Adapter(private val context: Context) :
    RecyclerView.Adapter<MainTabFragment1Adapter.MainTabFragment1ViewHolder>() {

    private val MainTabFragment1Model: MutableList<MainTabFragment1Model> = mutableListOf()  // Initialize listModel
    private lateinit var onSelectedListenerMainTabFragment1: OnItemClickListener // Initialize onItemListener


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainTabFragment1ViewHolder {
        return MainTabFragment1ViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_maintab_fragment1_layout, parent, false)
        )
    } // End - onCreateViewHolder
    
    override fun getItemCount(): Int {
        return MainTabFragment1Model.size
    }  // End - getItem
    
    override fun onBindViewHolder(holder: MainTabFragment1ViewHolder, position: Int) {
        holder.maintabfragment1BindView(MainTabFragment1Model[position])
    } // End - onBindViewHolder

    // Start - setMainTabFragment1
    fun setMainTabFragment1(data: List<MainTabFragment1Model>) {
        MainTabFragment1Model.clear()
        MainTabFragment1Model.addAll(data)
        notifyDataSetChanged()
    } // End - setMainTabFragment1
    
    // Start - getMainTabFragment1
    fun getMainTabFragment1(): MutableList<MainTabFragment1Model> {
        return MainTabFragment1Model
    } // End - getMainTabFragment1
    
    // Start - MainTabFragment1ViewHolder
    inner class MainTabFragment1ViewHolder(maintabfragment1view: View) : RecyclerView.ViewHolder(maintabfragment1view) {
        val ivMainTabFragment1 = maintabfragment1view.findViewById<ImageView>(R.id.iv_RVItem_MainTabFragment1)
        val tvTitleMainTabFragment1 = maintabfragment1view.findViewById<TextView>(R.id.tv_titleText_MainTabFragment1)
        val cvMainTabFragment1: MaterialCardView = maintabfragment1view.findViewById(R.id.cv_RVItem_MainTabFragment1)
        
        fun maintabfragment1BindView(MainTabFragment1Model: MainTabFragment1Model) {
            MainTabFragment1Model.apply {
                val image = MainTabFragment1Model.imgURLMainTabFragment1
                image.let {
                    Glide.with(itemView.context)
                        .load(it)
                        .into(ivMainTabFragment1)

                    Glide.with(itemView.context)
                        .load(it)
                        .centerCrop()
                        .fitCenter()
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true)
                }
            }
            tvTitleMainTabFragment1.text = MainTabFragment1Model.titleTextMainTabFragment1
        }

        init {
            cvMainTabFragment1.setOnClickListener { onSelectedListenerMainTabFragment1.onItemClick(it, layoutPosition) }
        }

    } // End - MainTabFragment1BindViewHolder

    // Start - OnItemListener
    fun setOnClickItemListenerMainTabFragment1(onItemClickListener: OnItemClickListener) {
        this.onSelectedListenerMainTabFragment1 = onItemClickListener
    } // End - OnItemListener

} // End - Class

MainTabFragment1Model.kt

data class MainTabFragment1Model
    (var imgURLMainTabFragment1: String?,
     var titleTextMainTabFragment1: String?,
     var descTextMainTabFragment1: String?,
     val btnURLMainTabFragment1: String?)

Listener.kt

interface OnItemClickListener {
    fun onItemClick(item: View, position:Int)
}

WebViewActivity.kt

class WebViewActivity : AppCompatActivity() {
    
    private lateinit var webView: WebView // Initialize WebView
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_webview)
        
        // Start - Assign Open New URL
        val wBS = intent
        val webSite = wBS.getStringExtra("btnURLMainTabFragment1")
        // End - Assign Open New URL
        
        // Start - full screen
        val window = window
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN

        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        ) // End - full screen

        // Start - WebView
        webView = findViewById(R.id.wv)
        webView.setWebViewClient(object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                view?.visibility = View.INVISIBLE

                if (Build.VERSION.SDK_INT >= 19) {
                    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
                } else {
                    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
                }
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                swipe?.isRefreshing = false
                super.onPageFinished(view, url)
                view?.visibility = View.VISIBLE
            }

            // Start - Fix Whatsapp Acces
            override fun shouldOverrideUrlLoading(wv: WebView, url: String): Boolean {
                if (url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                    val intent = Intent(Intent.ACTION_VIEW)
                    intent.data = Uri.parse(url)
                    startActivity(intent)
                    webView.goBack()
                    return true
                }
                return false
            } // End - Fix Whatsapp Acces

        }) // End - WebView

        refreshApp() // SetUp SwipeRefresh

        // Start - WebView Setting
        if (webSite != null) {
            webView.loadUrl(webSite)
        }

        val webSettings = webView.settings
        webSettings.javaScriptEnabled = true // js active
        webSettings.domStorageEnabled = true // componenet load
        webSettings.allowContentAccess = true
        webSettings.loadsImagesAutomatically = true
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH)
        webSettings.setEnableSmoothTransition(true)
        // End - WebView Setting

    }// End -OnCreate

    // Start - BackPressed CurrentView
    override fun onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack()
        } else
            super.onBackPressed()
    } // End - BackPressed CurrentView
    
    // Start - SwipeRefresh
    private fun refreshApp() {
        swipe.setOnRefreshListener {
            webView?.reload()
            Toast.makeText(this, "Refresh!", Toast.LENGTH_SHORT).show()
        }
    } // End- SwipeRefresh


} // finish MainActivity

I know there must be something wrong in my "DetailsActivity.kt",

especially on this line :

 prodBundle?.getString("btnURLMainTabFragment1").let {
          btn_wvURL.setOnClickListener {
              val gotoWebView = Intent(this@DetailsActivity, WebViewActivity::class.java)
              gotoWebView.putExtra("btnURLMainTabFragment1", gotoWebView)
              startActivity(gotoWebView)
          }

How to solve it?

I hope someone can solve this case. And thanks for taking your time.

CodePudding user response:

Here you are sending the intent as an extra to the intent itself:

val gotoWebView = Intent(this@DetailsActivity, WebViewActivity::class.java)
>>> gotoWebView.putExtra("btnURLMainTabFragment1", gotoWebView)

You can try:

prodBundle?.getString("btnURLMainTabFragment1")?.let { url->
          btn_wvURL.setOnClickListener {
              val gotoWebView = Intent(this@DetailsActivity, WebViewActivity::class.java)
              gotoWebView.putExtra("btnURLMainTabFragment1", url)
              startActivity(gotoWebView)
          }

This should work.

  • Related