我正在尝试设置一个名为“GetIntents”的Intent助手类。我的问题是,如何从活动中调用我的GetIntents助手类?
这是我的GetIntents助手类:
public class GetIntents extends Intent {
public static Intent i;
public static String username = i.getExtras().getString("uid");
public static String password = i.getExtras().getString("pwd");
public static String firstName = i.getExtras().getString("firstName");
public static String lastName = i.getExtras().getString("lastName");
public static String lastLatitudeUpdate = i.getExtras().getString("lastLatitudeUpdate");
public static int checkedin = i.getExtras().getInt("checkedin");
public static int Minute = i.getExtras().getInt("Minute");
public static int Hour = i.getExtras().getInt("Hour");
}
现在我不知道我怎么能从我的活动中调用它。到目前为止,我已经尝试过:
GetIntents intent = new GetIntents();
intent.getIntent();
我还尝试了其他几种方法,但没有运气。关于我可以尝试的任何建议?
我的日志:
06-21 11:26:18.249: D/AndroidRuntime(8978): Shutting down VM
06-21 11:26:18.249: W/dalvikvm(8978): threadid=1: thread exiting with uncaught exception (group=0x40015578)
06-21 11:26:18.253: E/AndroidRuntime(8978): FATAL EXCEPTION: main
06-21 11:26:18.253: E/AndroidRuntime(8978): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jetdelivery.mobile/com.jetdelivery.mobile.screen1}: java.lang.NullPointerException
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.os.Looper.loop(Looper.java:130)
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-21 11:26:18.253: E/AndroidRuntime(8978): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 11:26:18.253: E/AndroidRuntime(8978): at java.lang.reflect.Method.invoke(Method.java:507)
06-21 11:26:18.253: E/AndroidRuntime(8978): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-21 11:26:18.253: E/AndroidRuntime(8978): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-21 11:26:18.253: E/AndroidRuntime(8978): at dalvik.system.NativeStart.main(Native Method)
06-21 11:26:18.253: E/AndroidRuntime(8978): Caused by: java.lang.NullPointerException
06-21 11:26:18.253: E/AndroidRuntime(8978): at com.jetdelivery.mobile.GetIntentHelper.getDataModel(GetIntentHelper.java:9)
06-21 11:26:18.253: E/AndroidRuntime(8978): at com.jetdelivery.mobile.screen1.<init>(screen1.java:66)
06-21 11:26:18.253: E/AndroidRuntime(8978): at java.lang.Class.newInstanceImpl(Native Method)
06-21 11:26:18.253: E/AndroidRuntime(8978): at java.lang.Class.newInstance(Class.java:1409)
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-21 11:26:18.253: E/AndroidRuntime(8978): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
06-21 11:26:18.253: E/AndroidRuntime(8978): ... 11 more
答案 0 :(得分:1)
你可以通过几种方式做到这一点。 您的GetIntent类应该只是一堆静态方法的包装器。 你的函数调用看起来像
String username = GetIntent.getUsername(intent);
或者您可以在创建类
时传递意图GetIntent mIntent = new GetIntent(getIntent());
无论哪种方式,你仍然必须传递一个Intent,你将无法静态地将值分配给你的成员函数,除非这是一个内部类。但是,如果你这样做,那么为什么甚至在第一时间使用Class时你可以使用变量。
答案 1 :(得分:1)
你有点亲近。
创建一个名为DataModel的java类,如下所示:
public class DataModel {
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastLatitudeUpdate() {
return lastLatitudeUpdate;
}
public void setLastLatitudeUpdate(String lastLatitudeUpdate) {
this.lastLatitudeUpdate = lastLatitudeUpdate;
}
public int getCheckedin() {
return checkedin;
}
public void setCheckedin(int checkedin) {
this.checkedin = checkedin;
}
public int getMinute() {
return Minute;
}
public void setMinute(int minute) {
Minute = minute;
}
public int getHour() {
return Hour;
}
public void setHour(int hour) {
Hour = hour;
}
private String username;
private String password;
private String firstName;
private String lastName;
private String lastLatitudeUpdate;
private int checkedin;
private int Minute;
private int Hour;
}
然后创建另一个名为GetIntentHelper的类,如此
public class GetIntentHelper {
public static DataModel getDataModel(Intent i) {
DataModel m = new DataModel();
m.setUsername(i.getExtras().getString("uid"));
m.setPassword(i.getExtras().getString("pwd"));
m.setFirstName(i.getExtras().getString("firstName"));
m.setLastName(i.getExtras().getString("lastName"));
m.setLastLatitudeUpdate(i.getExtras().getString("lastLatitudeUpdate"));
m.setCheckedin(i.getExtras().getInt("checkedin"));
m.setMinute(i.getExtras().getInt("Minute"));
m.setHour(i.getExtras().getInt("Hour"));
return m;
}
}
至于使用情况,请在您的活动中致电:
DataModel mydata = GetIntentHelper.getDataModel(myIntent);
将返回一个名为mydata的DataModel实例,您的数据非常好,可以让您玩。
答案 2 :(得分:0)
谢谢大家的帮助。我决定采用另一种方式,而不是使用辅助类。
Intent i = getIntent();
username = i.getExtras().getString("uid");
password = i.getExtras().getString("pwd");
firstName = i.getExtras().getString("firstName");
lastName = i.getExtras().getString("lastName");
lastLatitudeUpdate = i.getExtras().getString("lastLatitudeUpdate");
checkedin = i.getExtras().getInt("checkedin");
Minute = i.getExtras().getInt("Minute");
Hour = i.getExtras().getInt("Hour");