Reputation: 53
I was trying to do some modifications to my AndroidManifest.xml but now when I go to the second activity(page) and then hit the back button, the app crashes.
Hers's is my MainActivity.java:
package com.example.ananaybatra.rape_freeindia;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onGetNameClick(View view) {
Intent getNameScreenIntent = new Intent(this, SecondScreen.class);
// We ask for the Activity to start and don't expect a result to be sent back
// startActivity(getNameScreenIntent);
final int result = 1;
// To send data use putExtra with a String name followed by its value
getNameScreenIntent.putExtra("callingActivity", "MainActivity");
startActivityForResult(getNameScreenIntent, result);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Create the TextView so I can put the users name on it
TextView usersNameMessage = (TextView) findViewById(R.id.textView2);
// Get the users name from the previous Activity
String nameSentBack = data.getStringExtra("UsersName");
// Add the users name to the end of the textView
usersNameMessage.append(" " + nameSentBack);
}
}
Here's my SecondScreen.java :
package com.example.ananaybatra.rape_freeindia;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.TextView;
import android.view.View;
import android.widget.EditText;
public class SecondScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout1);
Intent activityThatCalled = getIntent();
String previousActivity = activityThatCalled.getExtras().getString("callingActivity");
TextView callingActivityMessage = (TextView)
findViewById(R.id.textView7);
callingActivityMessage.append(" " + previousActivity);
}
public void onSendUsersName(View view) {
// Get the users name from the EditText
EditText usersNameET = (EditText) findViewById(R.id.textView4);
// Get the name typed into the EditText
String usersName = String.valueOf(usersNameET.getText());
// Define our intention to go back to ActivityMain
Intent goingBack = new Intent();
// Define the String name and the value to assign to it
goingBack.putExtra("UsersName", usersName);
// Sends data back to the parent and can use RESULT_CANCELED, RESULT_OK, or any
// custom values starting at RESULT_FIRST_USER. RESULT_CANCELED is sent if
// this Activity crashes
setResult(RESULT_OK, goingBack);
// Close this Activity
this.finish();
}
}
Here's my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ananaybatra.rape_freeindia" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondScreen"
android:label = "Get Name"
android:theme = "@style/AppTheme"/>
</application>
</manifest>
What is my Error ?? Please reply, any help would be appreciated.
Thanks, Regards.
Upvotes: 1
Views: 3900
Reputation: 812
just Override the onBackPressed() function in your SecondScreen.class and add this..
@Override
public void onBackPressed() {
//pass whatevere you want
intent.putExtra("result","Back Press");
setResult(Activity.RESULT_CANCELED,intent);
finish();
super.onBackPressed();
}
And in The MainActivity.class file, check this
if (resultCode == Activity.RESULT_CANCELED) {
//Write code if there's no result
String result=data.getStringExtra("result");
}
in onActivityResult function
Upvotes: 1
Reputation: 2664
Because you are not sending the data from the SecondActivity when onbackpressed. you are only sending the data when onSendUsersName
was clicked in the SecondActivity.
So in the FirstActivity the onActivityResult
was called with data(Intent)
was null.
So you are struck with the NullPointerException
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Create the TextView so I can put the users name on it
TextView usersNameMessage = (TextView) findViewById(R.id.textView2);
// data is NULL when back pressed on the SecondActivity So better check the Null validations
// Get the users name from the previous Activity
if (data != null) {
String nameSentBack = data.getStringExtra("UsersName");
// Add the users name to the end of the textView
usersNameMessage.append(" " + nameSentBack);
}
}
Upvotes: 4