I am calling intents alot in my program and I thought that I could write a public class that would have all of my intents so I can just call something like intents.testIntent(params) and then have it fire off the intent.
I tried it out but am getting a force close with a null pointer exception. This is my whole class. I know it is probably sloppy but I have just been learning java for 2 months so I still am an amateur.
Any way, my code is below and I would much appreciate your help.
public class intents extends Activity{
String classpkg = "com.MyApp";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void testIntent(String name, String match){
Log.v("AI","made It");
Intent myIntent = new Intent();
myIntent.setClassName(classpkg, classpkg+".Search");
Log.v("AI","Launching Intent"); //This is where it dies
startActivity(myIntent);
Log.v("AI","Launced Intent");
}
}
Log
/AI (17056): made It
V/AI (17056): Launching Intent
D/AndroidRuntime(17056): Shutting down VM
W/dalvikvm(17056): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
E/AndroidRuntime(17056): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(17056): java.lang.NullPointerException
E/AndroidRuntime(17056): at android.app.Activity.startActivityForResult(Activity.java:2749)
E/AndroidRuntime(17056): at android.app.Activity.startActivity(Activity.java:2855)
E/AndroidRuntime(17056): at com.MyApp.intents.testIntent(intents.java:26)
E/AndroidRuntime(17056): at com.MyApp.Starting$1.onClick(Starting.java:52)
E/AndroidRuntime(17056): at android.view.View.performClick(View.java:2364)
E/AndroidRuntime(17056): at android.view.View.onTouchEvent(View.java:4179)
E/AndroidRuntime(17056): at android.widget.TextView.onTouchEvent(TextView.java:6541)
E/AndroidRuntime(17056): at android.view.View.dispatchTouchEvent(View.java:3709)
E/AndroidRuntime(17056): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime(17056): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime(17056): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime(17056): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime(17056): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime(17056): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime(17056): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
E/AndroidRuntime(17056): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
E/AndroidRuntime(17056): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
E/AndroidRuntime(17056): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
E/AndroidRuntime(17056): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
E/AndroidRuntime(17056): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(17056): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(17056): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(17056): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(17056): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(17056): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(17056): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(17056): at dalvik.system.NativeStart.main(Native Method)
SOLUTION: A solution, maybe not THE solution. Was to build the intent in the intents class(renamed MyIntents) and then start it from my other class.
MyIntents intents1 = new MyIntents();
Intent arIntent = intents1.testIntent("fe", "feet");
startActivity(arIntent);
This will make my life alot easier. :)