Я пытался внести некоторые изменения в свой AndroidManifest.xml, но теперь, когда я перехожу на вторую активность (страницу), а затем нажимаю кнопку «Назад», приложение выходит из строя.приложение падает, когда я нажимаю кнопку «назад»
Hers является мой 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);
}
}
Вот мой 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();
}
}
Вот мой 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>
Что такое моя ошибка ?? Пожалуйста, ответьте, любая помощь будет оценена по достоинству.
Thanks, С уважением.
сообщение logcat, пожалуйста, без сообщения об ошибке, которое у вас есть, никто не может вам помочь. – Yurets
вы должны 'setResult (RESULT_OK)' или 'setResult (RESULT_CANCEL)' при завершении операции В 'onDestroy()' перед суперпользователем write 'setResult (RESULT_CANCEL)' –
Я думаю, что журнал ошибок может быть NPE в 'String nameSentBack = data.getStringExtra ("UsersName"); ' – Euporie