当我按下后退按钮时app崩溃了

时间:2015-04-24 07:53:04

标签: android

我试图对我的AndroidManifest.xml进行一些修改,但现在当我转到第二个活动(页面)然后点击后退按钮时,应用程序崩溃了。

她的是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>

我的错误是什么? 请回复,任何帮助将不胜感激。

谢谢, 问候。

2 个答案:

答案 0 :(得分:2)

因为您在onbackpressed时没有从 SecondActivity 发送数据。您只是在 SecondActivity 中点击onSendUsersName时发送数据。

因此,在FirstActivity中,使用onActivityResult调用的data(Intent)为空。

所以你对 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);
    }
}

答案 1 :(得分:0)

只需覆盖SecondScreen.class中的onBackPressed()函数并添加它..

@Override
    public void onBackPressed() {
        //pass whatevere you want 
        intent.putExtra("result","Back Press");
        setResult(Activity.RESULT_CANCELED,intent);
        finish();
        super.onBackPressed();
    }

在MainActivity.class文件中,选中此

 if (resultCode == Activity.RESULT_CANCELED) {
            //Write code if there's no result
            String result=data.getStringExtra("result");
        }
onActivityResult函数中的