I have a one android class which extends Activity.
public class MainAct extends Activity{
Context context;
SourceJava srcClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
context = this;
System.out.println("inside oncreate");
setContentView(R.layout.main);
srcClass = new SourceJava();
}
}
I have another Java class as like below and which this is calling from MainAct class.
public class SourceJava {
public SourceJava(){
System.out.println("inside constructor***");
MyClass myClass = new MyClass();
if(myClass != null){
System.out.println("**not null");
myClass.powerOff();
}
}
}
In the SourceJava I am calling another class. i.e
public class MyClass extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("**inside myclass");
powerOff();
}
public void powerOff(){
System.out.println("**inside powerOff");
Intent call = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:5555"));
startActivity(call);
}
}
}
I am getting NullPointerException on line startActivity(call),
myClass.powerOff(); and
srcClass = new SourceJava();
what is the problem with this code?
You are treating
MyClass(which is an Activity class) as regular Java class. TheMyClassactivity is neither registered nor loaded so call tostartActivityis pointless here. To do so you need to pass reference ofContextto theSourceClassandMyClassvia constructors.