I’m new to android, please help me
i made a interface in MainActivity and tried to initialize it like =>
class MainActivity(private val sender: DateSender)
but it has to have it’s defualt constractor or such.
it was the compile error =>
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shohadav1/com.example.shohadav1.MainActivity}: java.lang.InstantiationException: class com.example.shohadav1.MainActivity has no zero argument constructor
i want to send my data to a Fragment, if you know any better approach please guide me.
thanks
here is my main Activity =>
package com.example.shohadav1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import com.example.shohadav1.databinding.ActivityMainBinding
import com.example.shohadav1.databinding.AddItemDialogBinding
class MainActivity(private val sender: DateSender) : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding.root)
//set AppName text
//set Navi button
val actionBarDrawerToggle = ActionBarDrawerToggle(
this,
binding.drawerLayoutMain,
binding.toolbarMain,
R.string.open,
R.string.close
)
binding.drawerLayoutMain.addDrawerListener(actionBarDrawerToggle)
actionBarDrawerToggle.syncState()
//Fragment setting
var transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container_main, MainItemsViewerFrag())
transaction.commit()
//add Item
binding.toolbarMain.setOnMenuItemClickListener {
when (it.itemId){
R.id.addItem_toolbar -> { additem()}
R.id.search_btn_toolbar -> { }
}
true
}
}
fun additem(){
var addDialog = AlertDialog.Builder(this).create()
var addDialogBinding = AddItemDialogBinding.inflate(layoutInflater)
addDialog.setView(addDialogBinding.root)
addDialog.show()
addDialogBinding.acceptBtnAddDialog.setOnClickListener {
var newName = addDialogBinding.addNameAddDialog.text.toString()
var birthdate = addDialogBinding.addBirthDateAddDialog.text.toString()
var deadDate = addDialogBinding.addDeadDateAddDialog.text.toString()
var vasiatname = addDialogBinding.addVasiatnameAddDialog.text.toString()
var newData = Data(newName, birthdate, deadDate, vasiatname)
sender.sendData(newData)
}
addDialogBinding.cancelBtnAddDialog.setOnClickListener { addDialog.dismiss() }
}
interface DateSender {
fun sendData (newData : Data)
}
}
2
Answers
If you’re trying to implement an interface in an
Activity
, you do it like this:Then you can pass that to your
Fragment
if you get a reference to it:Or the
Fragment
can assume that its parentActivity
is aDateSender
:The interface makes it general – you can pass it any object that implements
DateSender
, and theFragment
can use it as one (i.e. callsendData
on it). That’s why it doesn’t make sense to tie it toMainActivity
specifically and put it inside, so it’s aMainActivity.DateSender
. At that point you may as well just put yoursendData
function inMainActivity
as a normal, non-interface function since you’re assuming your parent Activity is going to be that class anyway:Your MainClass must extend the interface for implementing
class MainActivity() : AppCompatActivity(), DateSender {}
Now you can override your function sendData in MainActivity class