I am going to build app which shows the bus connection.
My problem is that I get a nullpointer exception when I am putting a string into an intent and want to display this String in a TextView.
Here is my MainActivity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
HaltestelleName = intent.getStringExtra("HaltestelleName");
final String Zeit = intent.getStringExtra("Zeit");
if(resultCode == Activity.RESULT_OK && requestCode == requestCode)
{
if(rgStatus.getCheckedRadioButtonId() == R.id.rdAbfahrt)
{
txtHaltestelleStart.setText("HTL Neufelden");
txtHaltestelleZiel.setText(HaltestelleName);
}
else
{
txtHaltestelleStart.setText(HaltestelleName);
txtHaltestelleZiel.setText("HTL Neufelden");
}
}
txtZeit.setText(Zeit);
super.onActivityResult(requestCode, resultCode, intent);
}
Here is my Subactivity Code:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent haltestelle = new Intent(getApplicationContext(),
PlanActivity.class);
String item = ((TextView)view).getText().toString();
haltestelle.putExtra("HaltestelleName", item);
setResult(RESULT_OK,haltestelle);
finish();
}
hi please add the line
haltestelle.putExtra("Zeit", "yours passed value");
just below
haltestelle.putExtra("HaltestelleName", item);
Hi You didn't pass any value in your subactivity in "Zeit" name just pass some value from your sub activity to avoid null pointer exception
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
HaltestelleName = intent.getStringExtra("HaltestelleName");
final String Zeit;
if(resultCode == Activity.RESULT_OK && requestCode == requestCode)
{
if(rgStatus.getCheckedRadioButtonId() == R.id.rdAbfahrt)
{
txtHaltestelleStart.setText("HTL Neufelden");
txtHaltestelleZiel.setText(HaltestelleName);
}
else
{
txtHaltestelleStart.setText(HaltestelleName);
txtHaltestelleZiel.setText("HTL Neufelden");
}
}
if (!getIntent().hasExtra("HaltestelleName")) {
Zeit = intent.getStringExtra("Zeit");
txtZeit.setText(Zeit);
}
super.onActivityResult(requestCode, resultCode, intent);
}
The simple logic behind sending the data across two activities:
Activity A:
Intent intent = new Intent(ActivityA.this);
intent.putExtra("key", "value");
startActivity(intent);
Activity B:
Intent intent = getIntent();
String value = intent.getStringExtra("key");
Related
My onActivityResult method on activity always returns null for Intent extra. I'm directly calling these methods on activities. please help to find a solution for this.
private void onClickShopNameLayout() {
shopNameLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(OrderActivity.this,CustomerListActivity.class);
startActivityForResult(i,CUSTOMER_REQUEST_CODE);
}
});
}
Starting CustomerListActivity from OrderActivity
#Override
public void onItemClick(View v, int position) {
Customers customers = customersData.get(position);
Intent intent = new Intent();
intent.putExtra("testing","String value");
intent.putExtra("selected_customer",customers);
setResult(Activity.RESULT_OK,intent);
finish();
}
This is an Interface method from Recycler view Adapter class which triggers on list item click listner. implemented in CustomerListActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CUSTOMER_REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
String testing = getIntent().getStringExtra("testing");
Customers customers = getIntent().getParcelableExtra("selected_customer");
String businessName = customers.getBusinessName();
Log.d(TAG,"customer name "+businessName +" testing "+testing);
}
}
}
onActivityResult override method in OrderActivity.
Your onActivityResult() provides an Intent in your code its named as data. You should use this data instead of getIntent() to get intent extra values .
Try this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CUSTOMER_REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
String testing = data.getStringExtra("testing");
Customers customers = data.getParcelableExtra("selected_customer");
String businessName = customers.getBusinessName();
Log.d(TAG,"customer name " + businessName +" testing "+testing);
}
break; // Don't forget to use break for multiple cases
}
}
You are getting the extras from the activity intent, instead of the activity result intent.
Instead of
String testing = getIntent().getStringExtra("testing");
Customers customers = getIntent().getParcelableExtra("selected_customer");
do
String testing = data.getStringExtra("testing");
Customers customers = data.getParcelableExtra("selected_customer");
In case there are more than 2 Activities defined in Application (in Manifest) I had to be specific in Intent when using setResult(result, data).
On your example it would be:
#Override
public void onItemClick(View v, int position) {
Customers customers = customersData.get(position);
Intent intent = new Intent(CustomerListActivity.this, OrderActivity.class);
intent.putExtra("testing","String value");
intent.putExtra("selected_customer",customers);
setResult(Activity.RESULT_OK,intent);
finish();
}
And startActivityForResult() is deprecated now. New approach would look like:
ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
// Handle the Intent
}
}
});
// and later in the code
mStartForResult.launch(new Intent(this, ResultProducingActivity.class));
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 900) {
if (resultCode == getActivity().RESULT_OK) {
Bundle b=data.getExtras();
if(b!=null){
Playlist playlist = (Playlist) b.getSerializable("obj");
int playlistId = data.getIntExtra("PLAYLIST_ID", 0);
Log.d("---->Data ID", String.valueOf(playlistId));
}
}
}
How can I send that playlistId Value in onCreate() method?
ResultActivity:
intent.putExtra("yourKeyName", "hello");
setResult(900, intent);
Get the result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode== 900) {
if (resultCode == getActivity().RESULT_OK) {
String hello = data.getStringExtra("yourKeyName");
}
}
You dont have to create a new Bundle, just get extra content from the "Intent data".
Hope this helps.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode== 900) {
if (resultCode == getActivity().RESULT_OK) {
Bundle b=data.getExtras();
if(b!=null){
Playlist playlist = (Playlist) b.getSerializable("obj");
int playlistId = data.getIntExtra("PLAYLIST_ID", 0);
Log.d("---->Data ID", String.valueOf(playlistId));
}
}
}
use this in another activity to get the result back
Intent intent = new Intent();
intent.putExtra("key",value);
setResult(900,intent );
I want to receive data in previous Activity from next activity like (1 <--- 2 ). I tried but data is not received from second to first activity .
This is First Cativity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("ActivityId", getItemActivity);
i.putExtra("Vessel", strVesselName);
i.putExtra("HashTag", strHashTag);
i.putExtra("RemarkTitle", strRemark);
i.putExtra("ShortRGN", strShortTypeRGN);
i.putExtra("VessId", strvesselid);
startActivity(i);
This is second Activity
Intent intent = getIntent();
strActivityId = intent.getStringExtra("ActivityId");
strVesselName = intent.getStringExtra("Vessel");
strHashTag = intent.getStringExtra("HashTag");
strRemark = intent.getStringExtra("RemarkTitle");
strShortRGN = intent.getStringExtra("ShortRGN");
strVessId = intent.getStringExtra("VessId");
img_AddPostAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Edit_Post.this, EditRecord.class);
i.putExtra("EditVesselId", strVessId);
i.putExtra("EditActivityId" , strActivityId);
startActivity(i);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String audioString=data.getStringExtra("AUDIO_STRING");
Log.e(" audioString "," = "+audioString);
}
}
This is Third Activity
Intent intent = getIntent();
vesselId = intent.getStringExtra("EditVesselId");
strActivityId = intent.getStringExtra("EditActivityId");
Intent intent=new Intent(EditRecord.this, Edit_Post.class);
intent.putExtra("AUDIO_STRING",newAudioFile);
setResult(REQUEST_CODE, intent);
finish();
do this when you are calling the second activity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("HashTag", strHashTag);
startActivityForResult(i, REQUEST_CODE);
Now you need to set the result what you want on CustomActionActivity
e.g.
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(REQUEST_CODE,intent);
finish();
Now you will get this data to the your first activity
e.g.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
let me know in case of any issue
Use SharedPreferences
Store the data in a SharedPreference and access it in from the SharedPreference in the other activity.
Add the data to the SharedPreference in the onStop for the 2nd Activity and access it in the onCreate of the other Activity
#Override
public void onStop(){//Where you wish to insert data
SharedPreferences data=getSharedPreferences(PREFS_FILE,0);
SharedPreferences.Editor editor= count.edit();
editor.put("data","DATA");
editor.apply();
super.onStop();
}
in onCreate() of the other Activity:
SharedPreferences data = getApplicationContext().getSharedPreferences(PREFS_FILE, 0);
String dataString=data.get("Data",0);
Hope this helps. Cheers.
I am doing this way its works for me:
Activity 2:
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("MESSAGE", strtext + "");
setResult(2, intent);
if (isclose) {
finish();
} else {
super.onBackPressed();
}
}
}
Activity1:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String sSuName = data.getStringExtra("MESSAGE");
//txtfavouratecount.setText(sSuName);
}
in Your onclick listener
Intent itemintent = new Intent(context,your target Activity.class);
Bundle b = new Bundle();
b.putStringArray("iarray", Qtag);
b.putInt("mflag", 0);
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivityForResult(itemintent,2);
I would do something like this:
In Activity A:
private static final int REQUEST_CODE = 9001;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_CODE);
In Activity B:
Intent data = new Intent();
data.putExtra("key", parameter);
setResult(CommonStatusCodes.SUCCESS, data);
finish();
And finally, in the Activity A, receive result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
String result = data.getStringExtra("key");
}
} else {
//Error to receive data
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Good luck!
I have 2 activities in my Android application, Activity1 and Activity2.
I want to display the data retrieved from a String in Activity2 and this string value set text in Activity 1 textview when back is pressed from Activity2.
Please anyone give the solution for this problem,thanks in advance.
Activity 2: here I pass the string value to Activity 1 when I back press the Activity 2 this will be retrieved to Activity 1.
public void onBackPressed() {
// TODO Auto-generated method stub
NoolDataBaseHelper db = new NoolDataBaseHelper(NoolDashboardDetailPage.this);
int strtext = db.getProfilesCount();
db.close();
Intent intent = new Intent();
intent.putExtra("Obj", strtext+"");
setResult(Activity.RESULT_OK, intent);
if (isclose) {
finish();
}
else
{
if (!isplays) {
inflateLoginlayout.setVisibility(View.GONE);
topview.setVisibility(View.VISIBLE);
isplays = true;
//edtnames.getText().clear();
}
else
{
super.onBackPressed();
}
}
}
Activity1: here i retreive the string from Activity 2 and set the string value to my textview
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
String sSuName = data.getStringExtra("Obj");
txtfavouratecount.setText(sSuName);
} else if (resultCode == 0) {
}
}
}
You should follow this structure
In Activity 1
Intent intent=new Intent(Activity1.this,Activity2.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check the request code here is 2
if(requestCode==2){
if(resultCode == 3){ // check the result code
String message=data.getStringExtra("MESSAGE");
// set text for your textview
textView1.setText(message);
}
}
}
In Activity 2
public void onBackPressed() {
String message = "abc";
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(3,intent); // 3 is result code
super.onBackPressed();
}
Hope this help
I have the next activities:
Activity1
//declare
private static final int SAVE_DATA_FROM_ACTIVITY = 203;
//........... not important code
//button to open second Activity
public void btn_openSecondActivity(View view)
{
Intent intent = new Intent(Activity2.this, Activity1.class);
startActivityForResult(intent, SAVE_DATA_FROM_ACTIVITY);
}
}
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
}
//....... not important code
Activity2
On Second Activity I have two buttons:
Cancel
Save
//............
//declare
private static final int OK_RESULT_CODE = 1;
//Cancel button
public void btn_cancel(View view)
{
finish();
}
//Save button
public void btn_save (View view)
{
Intent intent = new Intent();
intent.putExtra("Name",et_name.getText().toString());
setResult(OK_RESULT_CODE, intent);
finish();
}
PROBLEM
When I click Save button all works perfect, but the problem it's when I click Cancel button, then it's reports an error:
Failure delivering result ResultInfo{who=null, request=203, result=0, data=null} to activity {com.example.alvaro.project/com.alvaro.project.Activity1}: java.lang.NullPointerException
I understand the problem, when I cancel is not the same result code but i don't know how I can solve it
Any suggestions?
You have issue in onActivityResult method. You don't check result.
Change your condition from :
if (requestCode == SAVE_DATA_FROM_activity)
to:
if (resultCode == OK_RESULT_CODE && requestCode == SAVE_DATA_FROM_activity)
Change this
if (requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
into
if (requestCode == SAVE_DATA_FROM_activity&&resultCode==RESULT_OK)
{
name= data.getStringExtra("Name");
}
and
Your cancel method is like
public void btn_cancel(View view)
{
setResult(RESULT_CANCELED);
finish();
}
And instead of OK_RESULT_CODE use Android default ok like Activity.RESULT_OK
Check if you manage to set the result
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == SAVE_DATA_FROM_activity && resultCode = Activity2.OK_RESULT_CODE)
{
name= data.getStringExtra("Name");
} else {
//probably btn_cancel pressed
}
}
check resultCode in onActivityResult
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (resultCode == 1/*OK_RESULT_CODE from Second Activity */ && requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
}
Activity1
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SAVE_DATA_FROM_activity) {
if (resultCode == Activity.RESULT_OK) {
name = data.getStringExtra("Name");
} else if (resultCode == Activity.RESULT_CANCELED){
// TODO something
}
}
}
Activity2
delete field OK_RESULT_CODE
//Cancel button
public void btn_cancel(View view) {
setResult(Activity.RESULT_CANCELED, new Intent());
finish();
}
//Save button
public void btn_save(View view) {
Intent intent = new Intent();
intent.putExtra("Name", et_name.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}