Передача значений через связку и получение ее значения для другой активности
Я передаю значение через Bundle, как вы можете видеть в моем коде.
Теперь я хочу его значение в другом действии onCreate()
. Я попробовал его получить его значение, но он показывает nullpointerexception.
Пожалуйста, помогите мне решить проблему.
Bundle bundle = new Bundle();
String url = "http://www.google.com";
bundle.putString("url", url);
Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras(bundle);
context.startService(myIntent);
Получить код значения:
if (!getIntent().getExtras().getString("url").contains(null)) {
// Do something
}
Ответы
Ответ 1
Это должна быть процедура.
Создайте новый Intent с помощью пакета и запустите его.
Intent i = new Intent(context, ActivityName.Class);
i.putExtra("key", mystring);
startActivity(i);
Возьмите узел, как это, в новой операции внутри onCreate
Bundle extras = getIntent().getExtras();
String value;
if (extras != null) {
value = extras.getString("key");
}
Ответ 2
Привет, надеюсь, этот код поможет вам.
Bundle bundle = new Bundle();
bundle.putString("name", "Android");
bundle.putString("iname", "iPhone");
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
intent.putExtras(bundle);
startActivity(intent);
В MyActivity.class
public Bundle getBundle = null;
getBundle = this.getIntent().getExtras();
String name = getBundle.getString("name");
String id = getBundle.getString("iname");
Ответ 3
if (getIntent().getExtras().getString("url") != null) {
// retrieve the url
}
вам нужно проверить нулевые значения
Ответ 4
//помещаем значение в намерение, подобное этому
Intent in = new Intent(MainActivity.this, Booked.class);
in.putExtra("filter", "Booked");
startActivity(in);
//получить значение из пучка, подобного этому
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String filter = bundle.getString("filter");
Ответ 5
попробуйте это
String url = "http://www.google.com";
Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras("url", url);
startActivity(myIntent);
а в другой активности получится
Bundle extra = getIntent().getExtras();
if(extra != null) {
String value = extra.getString("url")
//use this value where ever you want
}
Ответ 6
Заменить startService(...)
→ startActivity(...)
И также замените
if (getIntent().getExtras().getString("url").contains(null)) {
// retrieve the url
}
К
if (getIntent().getExtras().getString("url") != null) {
// retrieve the url
}
Ответ 7
Поскольку вы помещаете свои строки внутри пакета
Bundle youtbundle = new Bundle();
String url = "http://www.google.com";
yourbundle.putString("url", url);
И затем добавьте пакет в дополнение к намерению
Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras(yourbundle);
context.startService(myIntent);
В начале Вы должны разбить комплект из дополнительных функций
Bundle yourbundle = getIntent().getExtras().getBundle("yourbundle") ;
Затем получите строки из вашего списка
if (!yourbundle.getString("url").contains(null)) {
// Do something
}
Ответ 8
Строковое value = bundle.getString( "запрос" );
Ответ 9
Строка userID = aim.getStringExtra( "url" );
вернуться START_STICKY;
}
Ответ 10
Попробуйте добавить это внутри NotificationService.class
:
String url = getIntent().getStringExtra("url");