使用Intent.putextra

时间:2014-10-27 06:51:16

标签: android android-intent

String sRow = SignIn.getRow(email);             
Intent intent = new Intent(SignIn.this,HomePage.class);
                intent.putExtra("rowID", sRow);
                startActivity(intent);

此代码在我的SignIn活动中,我想将String值sRow传递给HomePage类。

Bundle extras = getIntent().getExtras();
    String rowID = extras.getString("rowID");

从我的HomePage类中,我使用它来调用sRow的值并将其分配给rowID。我做得对吗?因为我收到了调用HomePage.class的错误和错误;

Logcat:

10-27 02:55:19.040: D/dalvikvm(1722): Not late-enabling CheckJNI (already on)

10-27 02:55:19.380: D/(1722): HostConnection::get() New Host Connection established 0xb96340e0, tid 1722

10-27 02:55:19.650: W/EGL_emulation(1722): eglSurfaceAttrib not implemented

10-27 02:55:19.780: D/OpenGLRenderer(1722): Enabling debug mode 0

10-27 02:55:19.800: D/dalvikvm(1722): GC_CONCURRENT freed 131K, 8% free 3092K/3348K, paused 1ms+1ms, total 4ms

10-27 02:55:52.960: W/EGL_emulation(1722): eglSurfaceAttrib not implemented

10-27 02:56:00.141: W/EGL_emulation(1722): eglSurfaceAttrib not implemented

10-27 02:56:00.181: D/AndroidRuntime(1722): Shutting down VM

10-27 02:56:00.181: W/dalvikvm(1722): threadid=1: thread exiting with uncaught exception (group=0xb0cacb20)

10-27 02:56:00.181: E/AndroidRuntime(1722): FATAL EXCEPTION: main

10-27 02:56:00.181: E/AndroidRuntime(1722): Process: com.example.packageit, PID: 1722

10-27 02:56:00.181: E/AndroidRuntime(1722): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.packageit/com.example.packageit.HomePage}: java.lang.NullPointerException

10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)

10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.os.Looper.loop(Looper.java:136)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at java.lang.reflect.Method.invoke(Method.java:515)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at dalvik.system.NativeStart.main(Native Method)
10-27 02:56:00.181: E/AndroidRuntime(1722): Caused by: java.lang.NullPointerException
10-27 02:56:00.181: E/AndroidRuntime(1722):     at com.example.packageit.HomePage.<init>(HomePage.java:14)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at java.lang.Class.newInstanceImpl(Native Method)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at java.lang.Class.newInstance(Class.java:1208)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-27 02:56:00.181: E/AndroidRuntime(1722):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
10-27 02:56:00.181: E/AndroidRuntime(1722):     ... 11 more

3 个答案:

答案 0 :(得分:1)

你犯了一个小错误。

你把字符串额外的意图直接放在意图中而不是捆绑。当您检索时使用Bundle。

所以简单地这样写。

String rowID = getIntent().getStringExtra("rowID");

答案 1 :(得分:0)

Try this help you

HomePage class:

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String rowID = extras.getString("rowID");

答案 2 :(得分:0)

在HomePage类中获取rowID时,请执行以下操作:

Intent intent = getIntent();
String rowID = intent.getStringExtra("rowID");

希望这绝对可以。

另外,请检查清单以确认HomePage.class是否已注册。