简体   繁体   English

intent.putExtra不工作

[英]intent.putExtra not working

I need to pass information between two activities, but for some reason the information isn't sent / recieved. 我需要在两个活动之间传递信息,但由于某种原因,信息不会被发送/接收。

LogCat doesn't give me any errors. LogCat不会给我任何错误。 The dubugger clearly shows something is added to the intent (variabl: mExtras), but it's hard to interpret exactly what is added. dubugger清楚地显示了意图中添加了一些内容(variabl:mExtras),但是很难准确地解释添加的内容。 After that it gives me "source not found" and doesn't help me further. 之后,它给了我“找不到来源”,并没有帮助我。

But first things first. 但首先要做的事情。 Am I doing things right so far? 我到目前为止做得对吗?

Sending: 发送:

Intent intent = new Intent ( this, TaskListActivity.class );
intent.putExtra ( ProjectManager.ID, mId.toString () );
startActivity ( intent );

Recieving: Recieving:

Intent intent = getIntent ();
mId = UUID.fromString ( intent.getStringExtra ( ProjectManager.ID ) );

add following code after intent: 意图后添加以下代码:

Bundle extras = intent.getExtras();
String exampleString = extras.getString(ProjectManager.ID);

what is ProjectManager.ID ?, you should pass same unique key while recieving data from putExtra even way of receiving data is wrong, check below code: 什么是ProjectManager.ID ?,你应该在从putExtra接收数据时传递相同的唯一key ,即使接收数据的方式是错误的,检查下面的代码:

Sending: 发送:

Intent intent = new Intent ( this, TaskListActivity.class );
intent.putExtra (ProjectManager.ID, mId.toString () );
startActivity ( intent );

Recieving: Recieving:

Bundle extras = intent.getExtras();
    if(extras!=null){
  String _Str = extras.getString(ProjectManager.ID);
}

Try This to Receive Extra: 试试这个以获得额外收益:

Bundle extras = getIntent().getExtras(); 
String id;

if (extras != null) {
    id= extras.getString(key);

}

In FirstActivity.java write these code. 在FirstActivity.java中编写这些代码。

 Intent i = new Intent(FirstActivity.this,SecondActivity.class);
 i.putExtra("KEY",value);
 startActivity(i);

In SecondActivity.java: 在SecondActivity.java中:

Bundle extras=getIntent().getExtras();
String name=extras.getString("key"); //if data you are sending is String.
int i=extras.getInt("key"); //if data you are sending is integer.

To retrieve the extras in the new activity: 要检索新活动中的额外内容:

String valueOfExtra;
Intent i = getIntent();
//check first
if(i.hasExtra("extra1")){
  valueOfExtra = i.getStringExtra("extra1");
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM