getStringExtra() 函数 returns Intent {(有附加功能)}
getStringExtra() function returns Intent { (has extras) }
我正在将字符串从 activity A 传递到 activity B,并在 activity B 中使用传递的字符串。
将数据从 A 传递给 B 的代码(data
是一个字符串 。但为了消除任何疑问,我也使用 toString()
):
Intent intent = new Intent(this,Class.forName("com.example.mylibrary.BActivity"));
intent.putExtra("user",data.toString());
startActivityForResult(intent,1);
在activity B:
中获取传递数据的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
String text = getIntent().getStringExtra("user");
System.out.println(text);
TextView textView = (TextView)findViewById(R.id.textView4);
textView.setText(text);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
}
错误:
但是字符串 text
的值是 Intent { (has extras) }
。这是 activity B 在控制台和应用程序屏幕上打印的内容,因为 text
被放入 textView4
。 text
的期望值应该是用户最后 activity 输入的字符串。谁能告诉我为什么 getIntent().getStringExtra("user");
没有 return 实际传递的字符串而是 Intent { (has extras) }
?App screen
System.out.println(文本)
的控制台输出
V/FA: onActivityCreated
I/System.out: Intent { (has extras) }
V/FA: Activity resumed, time: 7152294
D/EGL_emulation: eglMakeCurrent: 0xef57f8e0: ver 2 0 (tinfo 0xe43eef50)
我已经复制了与 Activity 类 和布局 XML 提到的相同场景:
activity_a.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AActivity" >
<Button
android:id="@+id/button_navigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/navigate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
AActivity.java
public class AActivity extends AppCompatActivity {
private View.OnClickListener navigateClickListener = view ->
navigateToSecondActivity();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
initializeViews();
}
private void initializeViews() {
findViewById(R.id.button_navigate)
.setOnClickListener(navigateClickListener);
}
private void navigateToSecondActivity() {
String data = "Some Data"; // Here I'm setting a String as the data to be sent.
Intent intent = new Intent(AActivity.this, BActivity.class);
intent.putExtra("user", data.toString());
startActivityForResult(intent, 1);
}
}
activity_b.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BActivity" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
BActivity.java
public class BActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
String text = getIntent().getStringExtra("user");
System.out.println(text);
TextView textView = (TextView)findViewById(R.id.textView4);
textView.setText(text);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
}
}
当我构建 APK,在模拟器上部署 APK 时,代码 运行 就好了。
我怀疑您代码中的 data
可能已设置如下值:
data = someIntent.toString();
我怀疑你得到的价值是 Intent { (has extras) }
因此,我建议通过在每次更改时打印值来检查 data
是如何更改的。
此外,在 AActivity
上调用方法 startActivityForResult()
之前打印 data
的值,以确保它是您打算发送给 BActivity
的值。
一些注意事项:
在AActivity.java
中,使用data.toString()
是没有用的,因为data
本身就是一个String
,调用data.toString()
returns 相同的值,这使得调用它变得多余。
要打印 Console/Logcat 中的任何值,请使用 android.util.Log
而不是 System.out.println()
。 Here's why!
使用Class.forName("com.example.mylibrary.BActivity")
和BActivity.class
一样,后者更方便
我正在将字符串从 activity A 传递到 activity B,并在 activity B 中使用传递的字符串。
将数据从 A 传递给 B 的代码(data
是一个字符串 。但为了消除任何疑问,我也使用 toString()
):
Intent intent = new Intent(this,Class.forName("com.example.mylibrary.BActivity"));
intent.putExtra("user",data.toString());
startActivityForResult(intent,1);
在activity B:
中获取传递数据的代码 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
String text = getIntent().getStringExtra("user");
System.out.println(text);
TextView textView = (TextView)findViewById(R.id.textView4);
textView.setText(text);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
}
错误:
但是字符串 text
的值是 Intent { (has extras) }
。这是 activity B 在控制台和应用程序屏幕上打印的内容,因为 text
被放入 textView4
。 text
的期望值应该是用户最后 activity 输入的字符串。谁能告诉我为什么 getIntent().getStringExtra("user");
没有 return 实际传递的字符串而是 Intent { (has extras) }
?App screen
System.out.println(文本)
的控制台输出V/FA: onActivityCreated
I/System.out: Intent { (has extras) }
V/FA: Activity resumed, time: 7152294
D/EGL_emulation: eglMakeCurrent: 0xef57f8e0: ver 2 0 (tinfo 0xe43eef50)
我已经复制了与 Activity 类 和布局 XML 提到的相同场景:
activity_a.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AActivity" >
<Button
android:id="@+id/button_navigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/navigate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
AActivity.java
public class AActivity extends AppCompatActivity {
private View.OnClickListener navigateClickListener = view ->
navigateToSecondActivity();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
initializeViews();
}
private void initializeViews() {
findViewById(R.id.button_navigate)
.setOnClickListener(navigateClickListener);
}
private void navigateToSecondActivity() {
String data = "Some Data"; // Here I'm setting a String as the data to be sent.
Intent intent = new Intent(AActivity.this, BActivity.class);
intent.putExtra("user", data.toString());
startActivityForResult(intent, 1);
}
}
activity_b.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BActivity" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
BActivity.java
public class BActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
String text = getIntent().getStringExtra("user");
System.out.println(text);
TextView textView = (TextView)findViewById(R.id.textView4);
textView.setText(text);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
}
}
当我构建 APK,在模拟器上部署 APK 时,代码 运行 就好了。
我怀疑您代码中的 data
可能已设置如下值:
data = someIntent.toString();
我怀疑你得到的价值是 Intent { (has extras) }
因此,我建议通过在每次更改时打印值来检查 data
是如何更改的。
此外,在 AActivity
上调用方法 startActivityForResult()
之前打印 data
的值,以确保它是您打算发送给 BActivity
的值。
一些注意事项:
在
AActivity.java
中,使用data.toString()
是没有用的,因为data
本身就是一个String
,调用data.toString()
returns 相同的值,这使得调用它变得多余。要打印 Console/Logcat 中的任何值,请使用
android.util.Log
而不是System.out.println()
。 Here's why!使用
Class.forName("com.example.mylibrary.BActivity")
和BActivity.class
一样,后者更方便