当我将包从一个 activity 传递到 android 中的另一个时,为什么它不传递数据?
Why does it not pass data when I pass bundle from one activity to another in android?
我正在尝试将私有字符串从 MainActivity 传递到另一个 activity。这是代码:
MainActivity
public class MainActivity extends AppCompatActivity {
private String data1;
private String data2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Direct to data Input page and then to the next page
TextView text = findViewById(R.id.data_text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.datalayout);
EditText data = findViewById(R.id.Data1);
MainActivity.this.data1 = data.getText().toString();
data = findViewById(R.id.Data2);
MainActivity.this.data2 = data.getText().toString();
v = (ImageView) findViewById(R.id.continueNext);
MainActivity.this.redirectToAnotherPage(v, MainActivity.this, AnotherActivity.class);
}
});
private void redirectToAnotherPage(View view, final Context baseActivity, final Class newActivity) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(baseActivity, newActivity);
//Pass data between MainActivity and the launching activity using bundle
Bundle bundle = new Bundle();
bundle.putString("Data1",data1);
bundle.putString("Data2",data2);
myIntent.putExtras(bundle);
//End MainActivity before starting new activity
MainActivity.this.finish();
startActivity(myIntent);
}
});
}
AnotherActivity
public class AnotherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
//Get data from MainActivity using Bundle
Bundle viewData = getIntent().getExtras();
String dataToInclude = viewData.getString("Data1");
Toast toast = Toast.makeText(getApplicationContext(),dataToInclude,Toast.LENGTH_LONG);
toast.show();
}
当我传递String类型的data1和data2时,我没有看到toast消息。我在这里做错了什么?
您没有调用 toast.show()。这样做 -
Toast toast = Toast.makeText(getApplicationContext(),dataToInclude,Toast.LENGTH_LONG);
toast.show();
或
Toast.makeText(getApplicationContext(), dataToInclude, Toast.LENGTH_LONG).show();
你的代码难以理解:
1)你在做什么? setContentView(R.layout.datalayout);
2) data.getText().toString();
中的data
是什么
如果您看到 toast 中没有文字,则表示 dataToInclude
为 null
或为空。
所以重点检查 data1
和 data2
变量。
编辑:当你点击text
监听器立即执行:
新布局显示为空的 EditText,它们的空字符串存储在 data1
和 data2
中,您打开新的 activity 并在包中包含空字符串。
您可以像下面这样按意图使用 putExtra 方法
myIntent.putExtra("key", value);
而在另一个 activity 中,只需获取具有以下意图的数据
String value = getIntent().getStringExtra("key");
我正在尝试将私有字符串从 MainActivity 传递到另一个 activity。这是代码:
MainActivity
public class MainActivity extends AppCompatActivity {
private String data1;
private String data2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Direct to data Input page and then to the next page
TextView text = findViewById(R.id.data_text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.datalayout);
EditText data = findViewById(R.id.Data1);
MainActivity.this.data1 = data.getText().toString();
data = findViewById(R.id.Data2);
MainActivity.this.data2 = data.getText().toString();
v = (ImageView) findViewById(R.id.continueNext);
MainActivity.this.redirectToAnotherPage(v, MainActivity.this, AnotherActivity.class);
}
});
private void redirectToAnotherPage(View view, final Context baseActivity, final Class newActivity) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(baseActivity, newActivity);
//Pass data between MainActivity and the launching activity using bundle
Bundle bundle = new Bundle();
bundle.putString("Data1",data1);
bundle.putString("Data2",data2);
myIntent.putExtras(bundle);
//End MainActivity before starting new activity
MainActivity.this.finish();
startActivity(myIntent);
}
});
}
AnotherActivity
public class AnotherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
//Get data from MainActivity using Bundle
Bundle viewData = getIntent().getExtras();
String dataToInclude = viewData.getString("Data1");
Toast toast = Toast.makeText(getApplicationContext(),dataToInclude,Toast.LENGTH_LONG);
toast.show();
}
当我传递String类型的data1和data2时,我没有看到toast消息。我在这里做错了什么?
您没有调用 toast.show()。这样做 -
Toast toast = Toast.makeText(getApplicationContext(),dataToInclude,Toast.LENGTH_LONG);
toast.show();
或
Toast.makeText(getApplicationContext(), dataToInclude, Toast.LENGTH_LONG).show();
你的代码难以理解:
1)你在做什么? setContentView(R.layout.datalayout);
2) data.getText().toString();
data
是什么
如果您看到 toast 中没有文字,则表示 dataToInclude
为 null
或为空。
所以重点检查 data1
和 data2
变量。
编辑:当你点击text
监听器立即执行:
新布局显示为空的 EditText,它们的空字符串存储在 data1
和 data2
中,您打开新的 activity 并在包中包含空字符串。
您可以像下面这样按意图使用 putExtra 方法
myIntent.putExtra("key", value);
而在另一个 activity 中,只需获取具有以下意图的数据
String value = getIntent().getStringExtra("key");