Hi everyone, I am taking a mobile android development class and they are using Java but I was allowed to do it in Kotlin. In one of the examples, there is an interface being used in java as a variable.
I have been trying to get it to work in Kotlin, but it keeps crashing.
How would I be able to get it to function the same?
Java Code
public interface FragmentTracker {
public void fragmentVisible(String s);
public void goNext();
public void goBack();
public void saveNameAndLastName(String name, String lname);
public void saveCityAndZip(String city, String zip);
public void saveDetail(String detail);
public void finished();
}
// class
public class Fragment1 extends Fragment {
private FragmentTracker ft; // this is where it is being initialized as a variable
private View v;
public static final String fragmentTitle="Personal Info";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Toast.makeText(getContext(),"Visible",Toast.LENGTH_SHORT).show();
ft.fragmentVisible(fragmentTitle);
// Inflate the layout for this fragment
v= inflater.inflate(R.layout.first_fragment, container, false);
Button b_next=v.findViewById(R.id.next_button);
b_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ft.goNext();
}
});
return v;
}
This is what I tried in Kotlin
interface FragmentTracker {
fun fragmentVisible(s: String)
fun goNext();
fun goBack();
fun saveNameAndLastName(name: String, lname: String);
fun saveCityAndZip(city: String, zip: String);
fun saveDetail(detail: String);
fun finished();
}
// the class
private lateinit var ft: FragmentTracker // kotlin lets me do this but it fails below
private var v: View? = null;
companion object {
const val fragmentTitle = "Personal Info";
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
ft.fragmentVisible(fragmentTitle) // it fails here
v = inflater.inflate(R.layout.fragment_1, container, false)
v?.findViewById<Button>(R.id.next_button)?.setOnClickListener {
ft.goNext();
}
return v;
}
Any help/guidance would be awesome