简体   繁体   English

来自两个类的android Intent getextras

[英]android Intent getextras from two classes

I have the following method: 我有以下方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView name=(TextView)findViewById(R.id.nom);
    Bundle extras = getIntent().getExtras();//from class 1
    Bundle extras2=getIntent().getExtras();//from class/
    name.setText(extras.getString("nom"));//if methode in class1 works
    name.setText(extras2.getString("nombank"));//if methode in class 2 works
}

But i get empty name . 但是我namename

I need to get pass two Strings from two different classes(class1 and class2) to the MainActivity.java 我需要从两个不同的类(class1和class2)传递两个String到MainActivity.java

Activity cannot be started by two different activities, just by one, so you must not use two different extras in your main class - you can use just one extra and put data with the same key value in both activities. 活动不能由两个不同的活动开始,而不能由一个活动开始,因此您不能在主类中使用两个不同的附加项-您只能使用一个附加项并将两个键值相同的数据放入两个活动中。

So, for example, if you have Class1 and Class2, when you create intent in both classes you should use: 因此,例如,如果您具有Class1和Class2,则在两个类中都创建意图时,应使用:

Intent int = new Intent(this,MainActivity.class);
int.putExtra("nom",your_String);
...//Another code

And in MainActivity you get just one extra: MainActivity您只能得到一个额外的MainActivity

Bundle extra = getIntent().getExtras();
name.setText(extra.getString("nom"));

If Class1 and Class2 are started from MainActivity , use startActivityForResult() method instead. 如果从MainActivity启动Class1和Class2,请改用startActivityForResult()方法。

You don't need any of that. 您不需要任何。 Just do as shown below: 只需如下所示:

 name.setText(getIntent().getStringExtra("nom")); name.setText(getIntent().getStringExtra("nombank")); 

That's It, This is All You Need. 就这样,这就是您所需要的。

But you can't Put Two Intent Extras in the Same TextView. 但是您不能在同一个TextView中放置两个Intent Extras。 You can Only USE ONE 您只能使用一个

Just pass extra with same key and get in activity 只需使用相同的密钥传递其他内容即可参与活动

Class 1 1类

Intent class1 = new Intent(this,MainActivity.class);
class1.putExtra("same_key","class 1 string"); 

Class 2 2级

Intent class2 = new Intent(this,MainActivity.class);
class2.putExtra("same_key","class 2 string"); 

MainActivity 主要活动

   Bundle extras = getIntent().getExtras();

    //Called from Class1 show "class 1 string" 
    //Called from Class2 show "class 2 string" 
     name.setText(extras.getString("same_key"));

If MainActivity will called from Class 1 extra string will be from Class 1, if from Class 2 extra string will be from Class2. 如果从Class 1调用MainActivity,则从Class 1调用额外的字符串;如果从Class 2调用,则从Class2调用额外的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空对象引用上的错误 android.content.Intent.getExtras()' - error android.content.Intent.getExtras()' on a null object reference Android:intent.getExtras()。getSerializable(Constants.codiceMainActivity)为null - Android: intent.getExtras().getSerializable(Constants.codiceMainActivity) is null 第二个活动无法从意图中读取 getExtras - Second activity can't read getExtras from intent Intent.getExtras无法正常工作 - Intent.getExtras not working Intent 中的 getExtras() 有时是 null - getExtras() in Intent is sometimes null 如何在 react-native 中从本地广播 Intent 中获取额外内容 - Howto getExtras from a local broadcast Intent in react-native 尝试在空对象引用上调用虚拟方法“android.os.Bundle android.content.Intent.getExtras()” - Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference 我有以下错误:android.os.Bundle android.content.Intent.getExtras()' on a null ZA8CFDE6331BD4BEB6 - I have the following error:android.os.Bundle android.content.Intent.getExtras()' on a null object reference Android getIntent()。getExtras()NullPointerException - Android getIntent().getExtras() NullPointerException Android 编程:Intent.getExtras() 似乎以某种方式停止了方法中的执行流程 - Android Programming: Intent.getExtras() seems to be stopping execution flow in a method somehow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM