[英]app crashes when I hit the back button
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. 我试图对AndroidManifest.xml进行一些修改,但是现在当我转到第二个活动(页面),然后单击“后退”按钮时,应用程序崩溃了。
Hers's is my MainActivity.java: 她的是我的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 : 这是我的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: 这是我的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. 感谢和问候。
Because you are not sending the data from the SecondActivity when onbackpressed. 因为在onbackpressed时您不是从SecondActivity发送数据。 you are only sending the data when onSendUsersName
was clicked in the SecondActivity . 您仅在onSendUsersName
中单击onSendUsersName时发送数据。
So in the FirstActivity the onActivityResult
was called with data(Intent)
was null. 因此,在FirstActivity中,使用data(Intent)
为null调用onActivityResult
。
So you are struck with the NullPointerException 因此,您对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);
}
}
just Override the onBackPressed() function in your SecondScreen.class and add this.. 只需重写SecondScreen.class中的onBackPressed()函数并将其添加即可。
@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 然后在MainActivity.class文件中,选中此
if (resultCode == Activity.RESULT_CANCELED) {
//Write code if there's no result
String result=data.getStringExtra("result");
}
in onActivityResult function 在onActivityResult函数中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.