不访问Android Intent-Extras

时间:2015-12-16 14:16:33

标签: android-intent bundle extras

所以我一直试图通过Intent将Extra(单个String)从一个Activity传递给另一个Activity。在下面的Activity中,它应该从最后一个intent获取String,并将其用作Activity中另一个更改的条件。 我不知道Extras是不是Put,不是Gotten还是没有发送。

以下是应该使用Intent发送附加内容的代码:

   public void btnDiffClicked(View view) {
    int difficulty = view.getId();
    if (difficulty == R.id.btnDiffEasy) {
        Intent toeasy = new Intent(getApplicationContext(), EmotionCheckActivity.class);
        Bundle b = new Bundle();
        b.putString("diff", "EASY");
        toeasy.putExtra("android.intent.extra.INTENT", b);
        startActivity(toeasy);
    } else if (difficulty == R.id.btnDiffMedium) {
        Intent tomedium = new Intent(getApplicationContext(), EmotionCheckActivity.class);
        Bundle b = new Bundle();
        b.putString("diff", "MEDIUM");
        tomedium.putExtra("android.intent.extra.INTENT", b);
        startActivity(tomedium);
    } else if (difficulty == R.id.btnDiffHard) {
        Intent tohard = new Intent(getApplicationContext(), HardActivity.class);
        startActivity(tohard);
    }
}

以下是应该抓住Extras的代码:             @覆盖         protected void onCreate(Bundle savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.activity_emotion_check);

    final Intent startingIntent = getIntent();

    Button btnHelpEmoCheck = (Button) findViewById(R.id.btnHelpEmoCheck);
    btnHelpEmoCheck.setBackgroundColor(Color.TRANSPARENT);

    Button btnCont = (Button) findViewById(R.id.btnCont);
    btnCont.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View view) {
                Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");
                String diff = b.getString("diff");
                    if (diff == "MEDIUM") {
                        Intent tomedium = new Intent(getApplicationContext(), MediumActivity.class);
                        startActivity(tomedium);
                    } else if (diff == "EASY") {
                        Intent toeasy = new Intent(getApplicationContext(), EasyActivity.class);
                        startActivity(toeasy);
            }
        }
    });

当我点击调用“btnContClicked”的按钮时,活动崩溃了。当变量在onCreate()方法之外时,我也尝试在onCreate()中获取额外内容。它不会崩溃然后按钮不会做任何事情。

12-17 13:04:19.078 5900-5900/? I/art: Late-enabling -Xcheck:jni
12-17 13:04:19.078 5900-5900/? I/art: VMHOOK: rlim_cur : 0 pid:5900
12-17 13:04:19.458 5900-5900/asap.easel D/Atlas: Validating map...
12-17 13:04:19.508 5900-5957/asap.easel I/Adreno-EGL:              <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.030_msm8974_refs/tags/AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.030__release_AU ()
                                                  OpenGL ES Shader Compiler Version: E031.25.03.00
                                                  Build Date: 12/11/14 Thu
                                                  Local Branch: 
                                                  Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.030
                                                  Local Patches: NONE
                                                  Reconstruct Branch: NOTHING
12-17 13:04:19.668 5900-5900/asap.easel I/InputMethodManager:    [startInputInner] EditorInfo { packageName=asap.easel, inputType=0x61,    imeOptions=0x4006, privateImeOptions=null },    windowGainingFocus=android.view.ViewRootImpl$W@2916a2c,    mServedView=android.support.v7.widget.AppCompatEditText{304bccf5 VFED..CL   .F....I. 552,99-1182,222 #7f0b00a7 app:id/textFieldStart}, mServedInputConnectionWrapper=android.view.inputmethod.InputMethodManager$ControlledInputConnectionWrapper@23371a8a
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:    java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream java.net.Socket.getOutputStream()' on a null object reference
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at asap.easel.ConnectionManager.send(ConnectionManager.java:45)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at asap.easel.LogTherActivity.btnTherClicked(LogTherActivity.java:37)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at android.view.View.performClick(View.java:4785)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at android.view.View$PerformClick.run(View.java:19858)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at android.os.Looper.loop(Looper.java:155)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5696)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
12-17 13:04:22.198 5900-5900/asap.easel W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
12-17 13:04:22.198 5900-6274/asap.easel D/libc: [NET] android_getaddrinfofornet+,hn 13(0x3139322e313638),sn(),hints(known),family  0,flags 4
12-17 13:04:22.198 5900-6274/asap.easel D/libc: [NET]  android_getaddrinfofornet-, SUCCESS
12-17 13:04:22.228 5900-5900/asap.easel W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream java.net.Socket.getOutputStream()' on a null object  reference
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at asap.easel.ConnectionManager.send(ConnectionManager.java:45)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at asap.easel.ComfyActivity.onCreate(ComfyActivity.java:20)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.app.Activity.performCreate(Activity.java:5958)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.os.Looper.loop(Looper.java:155)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5696)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
12-17 13:04:22.228 5900-5900/asap.easel W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
12-17 13:04:22.228 5900-6275/asap.easel D/libc: [NET] android_getaddrinfofornet+,hn 13(0x3139322e313638),sn(),hints(known),family 0,flags 4
12-17 13:04:22.228 5900-6275/asap.easel D/libc: [NET] android_getaddrinfofornet-, SUCCESS
12-17 13:04:24.388 5900-5915/asap.easel I/art: WaitForGcToComplete blocked for 16.356ms for cause HeapTrim
12-17 13:04:25.188 5900-5900/asap.easel W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.

我想这是最后一行,但我不知道如何处理它。所有其他行都指我的TCP / IP连接。它在服务器未运行时抛出错误,这不是问题。

0 个答案:

没有答案