java - How to use a different class from an application's activity? -


i created java file in same package main activity class named sup.

now, need use class in main activity file.

mainactivity.java:

package com.example.phy.myapplication;  import android.support.v7.app.appcompatactivity; import android.os.bundle;   public class mainactivity extends appcompatactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     }     sup mola = new sup(this);     mola.as();  } 

sup.java:

package com.example.phy.myapplication;  import android.content.context; import android.widget.toast;  public class sup {       public sup(context context){          charsequence text = "hello toast!";         int duration = toast.length_short;          toast toast = toast.maketext(context, text, duration);         toast.show();      }     void as(context context){          charsequence text = "as method";         int duration = toast.length_short;          toast toast = toast.maketext(context, text, duration);         toast.show();     }  } 

do have import class mainactivity? how?

sup class , mainactivity class located in same package, dont need import anything, calling method of classs sup in no defined scope... better if move

sup mola = new sup(this); mola.as(); 

inside of on create method like:

public class mainactivity extends appcompatactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         sup mola = new sup(this);         mola.as();     } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -