I have created one class as
public class FormActivity extends Activity
{
TextView tv =new TextView(this);
GridLayout gl=new GridLayout(this);
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
paint();
}
public void paint()
{
tv.setText("In new File");
gl=(GridLayout) findViewById(R.id.gl);
gl.addView(tv);
}
}
and I want to access the paint() method from a different class (java file in same package).
I have tried this:
public class FileSystemDemoActivity extends Activity
{
FormActivity f1=new FormActivity();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
f1.paint();
}
}
But this doesn’t work. The emulator gives unfortunately stopped error. Please help me.
you should not try to access the other Activity function as
1- You should not try to create the instance of other activity in your activity.
2- findViewById(R.id.gl) is linked with the current activity layout it will search id in that particular activity in which activity code is written.
…