我在我的程序中调用很多意图,我认为我可以编写一个公共类,它可以拥有我所有的意图,所以我可以调用类似intents.testIntent(params)之类的东西,然后让它触发意图。
我试了一下,但是因为空指针异常而关闭了一个力。这是我全班同学。我知道它可能很草率,但我刚刚学习java 2个月,所以我还是个业余爱好者。
无论如何,我的代码如下,我非常感谢您的帮助。
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");
}
}
日志
/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)
解: 解决方案,也许不是解决方案。是在Intents类(重命名为MyIntents)中构建intent,然后从我的其他类启动它。
MyIntents intents1 = new MyIntents();
Intent arIntent = intents1.testIntent("fe", "feet");
startActivity(arIntent);
这将使我的生活更轻松。 :)
答案 0 :(得分:2)
我认为你正在做什么(类名的字符串,每次你需要一个意图时实例化一个助手类)都很混乱。
我建议使用像这样的工厂方法的辅助类:
public static final String FIRST_NAME = "first_name";
public static Intent getNameIntent(Context ctx, String firstName) {
Intent intent = new Intent(ctx, NameActivity.class);
intent.putExtra(FIRST_NAME, firstName);
return intent;
}
然后从Activity
:
Intent intent = getNameIntent(this, "alex");
startActivity(intent);
// or
startActivityForResult(intent, 1024);