从多个活动向活动发送附加内容

时间:2016-05-20 06:13:32

标签: android android-intent bundle

我有5个活动,即 D_EngNameScreen D_LocNameScreen D_GenCharScreen D_PathSNameScreen Disease_InformationScreen < / em>的。这里的前四个活动是通过 Intent Bundle extras 发送到最后一个活动( Disease_InformationScreen )。 代码是 -

// from the D_EngNameScreen activity

  Intent intentDENG = new Intent(D_EngNameScreen.this, Disease_InformationScreen.class);
    d_eng_Name_Bundle.putString("CROPNAME_EN",crop_en);
    d_eng_Name_Bundle.putString("DENGNAME",d_EngName );
    intentDENG.putExtra("NAVIGATIONTYPE","EnglishName");
    intentDENG.putExtras(d_eng_Name_Bundle);
    startActivity(intentDENG);


//from the D_LocNameScreen activity

    Intent intentDLN = new Intent(D_LocNameScreen.this, Disease_InformationScreen.class);
    d_local_name_bundle.putString("CROPNAME_LN",crop_ln);
    d_local_name_bundle.putString("DLOCALNAME",d_Locname);
    intentDLN.putExtra("NAVIGATIONTYPE","LocalName");
    intentDLN.putExtras(d_local_name_bundle);
    startActivity(intentDLN);

 //from the D_GenCharScreen activity

Intent intentDGENCHAR = new Intent(D_GenCharScreen.this, Disease_InformationScreen.class);
    d_gen_char_bundle.putString("CROPNAME_GC",crop_gc);
    d_gen_char_bundle.putString("SPREADMODE",d_spread_Items);
    d_gen_char_bundle.putString("PATHOGENCONENV",d_patho_environ_Items);
    intentDGENCHAR.putExtra("NAVIGATIONTYPE","GeneralChar");
    intentDGENCHAR.putExtras(d_gen_char_bundle);
    startActivity(intentDGENCHAR);


//from the D_PathSNameScreen activity

Intent intentDPSN = new Intent(D_PathSNameScreen.this,Disease_InformationScreen.class);
        d_pathoName_bundle.putString("CROPNAME_PN",crop_pn);
    d_pathoName_bundle.putString("DPATHOSCINAME",d_pathosci_name);
    intentDPSN.putExtra("NAVIGATIONTYPE","PathogenName");
    intentDPSN.putExtras(d_pathoName_bundle);
    startActivity(intentDPSN);

我在Disease_InformationScreen活动中收到了额外内容 -

  if (getIntent().getExtras() != null) {
              if (getIntent().getStringExtra("NAVIGATIONTYPE").contentEquals("EnglishName")) {
            Bundle getcrop = getIntent().getExtras();
            cropname_en = getcrop.getString("CROPNAME_EN");
            Log.d("CNAME: ", cropname_en);

            d_EngName = getcrop.getString("DENGNAME");
            Log.d("DENGNAME: ", d_EngName);
            danme12.setText(d_EngName);

        } else if (getIntent().getStringExtra("NAVIGATIONTYPE").contentEquals("LocalName")) {
            Bundle getcrop = getIntent().getExtras();
            cropname_ln = getcrop.getString("CROPNAME_LN");
            Log.d("CNAME: ", cropname_ln);
            d_LocName = getcrop.getString("DLOCALNAME");

        } else if (getIntent().getStringExtra("NAVIGATIONTYPE").contentEquals("PathogenName")) {
            Bundle getcrop = getIntent().getExtras();
            cropname_pn = getcrop.getString("CROPNAME_PN");
            d_PathName = getcrop.getString("DPATHOSCINAME");

        } else if (getIntent().getStringExtra("NAVIGATIONTYPE").contentEquals("GeneralChar")) {
            Bundle getcrop = getIntent().getExtras();
            cropname_gc = getcrop.getString("CROPNAME_GC");
            d_SprdMode = getcrop.getString("SPREADMODE");
            d_PathCE = getcrop.getString("PATHOGENCONENV");
        }
    }

在这种情况下,D_EngNameScreen活动正在成功向Disease_InformationScreen活动发送额外内容,但其余活动并未执行相同操作。它在 -

显示空指针异常
d_local_name_bundle.putString("CROPNAME_LN",crop_ln);//for example

在将剩余部分发送到Disease_InformationScreen活动时剩余的三个活动中。 Logcat是 -

05-20 11:40:27.365 15619-15619/com.thesiswork.ashraf.ashrafresearchwork E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      java.lang.NullPointerException
                                                                                          at com.thesiswork.ashraf.ashrafresearchwork.D_LocNameScreen.onClick(D_LocNameScreen.java:123)
                                                                                          at android.view.View.performClick(View.java:4212)
                                                                                          at android.view.View$PerformClick.run(View.java:17476)
                                                                                          at android.os.Handler.handleCallback(Handler.java:800)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:100)
                                                                                          at android.os.Looper.loop(Looper.java:194)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5371)
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                                          at dalvik.system.NativeStart.main(Native Method)

所以请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

我认为你没有创建

的对象
  

d_local_name_bundle