0

我正在尝试设置一个名为“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
4

3 回答 3

1

你可以通过几种方式做到这一点。您的 GetIntent 类应该只是一堆静态方法的包装器。你的函数调用看起来像

String username = GetIntent.getUsername(intent);

或者您可以在创建类时传入意图

GetIntent mIntent = new GetIntent(getIntent());

无论哪种方式,您仍然必须传递一个 Intent 并且您将无法将值静态分配给您的成员函数,除非这是一个内部类。但是,如果您这样做,那么当您可以使用我们的变量时,为什么还要首先使用 Class。

于 2012-06-21T18:07:31.750 回答
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 实例,其中包含所有精美且整洁的数据供您使用。

于 2012-06-21T18:07:40.743 回答
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");
于 2012-06-21T19:10:10.817 回答