0

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?

shiv1229
  • 357
  • 2
  • 6
  • 19

1 Answers1

2

You are treating MyClass (which is an Activity class) as regular Java class. The MyClass activity is neither registered nor loaded so call to startActivity is pointless here. To do so you need to pass reference of Context to the SourceClass and MyClass via constructors.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • I am asking one more question to u. Is it possible access android internal API's. i.e com.android.internal.telephony. – shiv1229 Jan 12 '12 at 13:23
  • @shiv1229 - I'd say No. com.android.internal is third party API. Have a look at SO thread - http://stackoverflow.com/questions/2001146/reflection-to-access-advanced-telephony-features and discussion at google - http://groups.google.com/group/android-developers/browse_thread/thread/b0b8b223a565319f?pli=1 – KV Prajapati Jan 12 '12 at 13:30
  • I posted one more question here I request u to see and plz help me to resolve this problem – shiv1229 Jan 13 '12 at 04:38