StartActivityForResult在第二个活动中无法正常工作

时间:2018-07-07 17:01:23

标签: android

我是这个菜鸟,所以请保持简短。

这是一个练习应用程序,它将来自第一活动的数据作为消息发送到第二活动,然后第一活动作为消息从第二活动接收数据。我正在遵循的教程指示我使用StartActivityForResult()函数将数据从第二个活动提取到第一个活动。我有两个问题:

  1. 此代码有什么问题:

I。 MainActivity.java文件

package com.example.android.twoactivitiesredo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private static final String LOG_TAG = MainActivity.class.getSimpleName();
//LOG_TAG contains the name of the class package, encapsulated for ease

public static final String EXTRA_MESSAGE = 
"com.example.android.twoactivities.extra.MESSAGE";
//This will be used as the unique key to send data to the second Activity

public static final int TEXT_REQUEST = 1;
//This is used to define the key for a particular type of response that 
you're interested in.

private EditText mMessageEditText;
//This EditText is used to send the message to the second activity

private TextView mReplyHeadTextView;
private TextView mReplyTextView;
//These private variables hold the reply header and the reply TextViews

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mMessageEditText = (EditText) findViewById(R.id.editText_main);
    mReplyHeadTextView = (TextView) findViewById(R.id.textHeaderReply);
    mReplyTextView = (TextView) findViewById(R.id.textMessageReply);
}

public void launchSecondActivity(View view) {
    Log.d(LOG_TAG, "Button Clicked!");

    Intent intent = new Intent(this, SecondActivity.class);
    String message = mMessageEditText.getText().toString();
    //Gets the data from EditText, converts it to String, and stores it in message
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivityForResult(intent, TEXT_REQUEST);

}

public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode == TEXT_REQUEST){
        if(resultCode == RESULT_OK){
            String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
            mReplyHeadTextView.setVisibility(View.VISIBLE);

            mReplyTextView.setText(reply);
            mReplyTextView.setVisibility(View.VISIBLE);

        }
    }
    }
    }

II。 SecondActivity.java文件

    package com.example.android.twoactivitiesredo;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;

    public class SecondActivity extends AppCompatActivity{

    public static final String EXTRA_REPLY = 
    "com.example.android.twoactivities.extra.REPLY";
    //This tag will be used as a key to send reply to first activity

     private EditText mReply;


    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    Intent intent = getIntent();
    //Gets the intent that activated this activity

    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    //This extracts the extra text sent along with this key String, thus 
    receiving the text

    TextView textView = (TextView) findViewById(R.id.text_message);

    textView.setText(message);

    mReply = (EditText) findViewById(R.id.replyEditText);
    //Do not reuse the intent from the first activity, create a new one

    Intent replyIntent = new Intent();
    setResult(RESULT_OK,replyIntent);
    //RESULT_OK has the value -1 and is used as a Result code in the Activity 
    class
    //to check that the data is send without a complication
    finish();
    }


    public void returnReply(View view) {
    }
    }

III。 activity_main.xml文件

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android" 
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button_main"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:text="@string/sendButton"
    android:onClick="launchSecondActivity"/>

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText_main"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toStartOf="@id/button_main"
    android:layout_toLeftOf="@+id/button_main"
    android:hint="Enter Your Message Here"
    />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textHeaderReply"
    android:text="@string/textHeaderReply"
    android:visibility="invisible"/>

   <!--The visibility mode is used to select how the attribute will be before data is passed to it.-->
   <!--Invisible means that the attribute will be invisible before data is passed to it, hence not confusing the user-->
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textMessageReply"
    android:layout_below="@+id/textHeaderReply"
    android:visibility="invisible"/>

IV。 activity_second.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent"
   android:layout_height="match_parent">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_header"
    android:layout_marginBottom="20dp"
    android:text="@string/text_header"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
    android:textStyle="bold"/>
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button_main"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:text="@string/sendButton"
    android:onClick="launchSecondActivity"/>
    <TextView
    android:id="@+id/text_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_header"
    android:layout_margin="10dp"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>
    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/replyEditText"
    android:onClick="returnReply"
    android:layout_toLeftOf="@+id/button_main"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:hint="@string/replyText"
    />



</RelativeLayout>

V。 AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.twoactivitiesredo">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="@string/secondActivity"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"

 android:value="com.example.android.twoactivitiesredo.MainActivity"/>
    </activity>
</application>
</manifest>

很抱歉,如果代码占用了大量空间,但是在这里我真的需要帮助来澄清我的疑问和问题,以了解为什么该代码无法正常工作。

提前感谢所有答案。

1 个答案:

答案 0 :(得分:0)

用以下代码替换您的secondActivity.java:

package com.example.android.twoactivitiesredo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity{

public static final String EXTRA_REPLY = 
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity

 private EditText mReply;


protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

Intent intent = getIntent();

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);


TextView textView = (TextView) findViewById(R.id.text_message);

textView.setText(message);

mReply = (EditText) findViewById(R.id.replyEditText);

}

public void returnReply(View view) {
Intent replyIntent = new Intent();
String replyMessage = mReply.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, replyMessage);
setResult(RESULT_OK,replyIntent);
finish();
}
}

在activity_second.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_header"
android:layout_marginBottom="20dp"
android:text="@string/text_header"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Reply Button"
android:onClick="returnReply"
/>
<TextView
android:id="@+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_header"
android:layout_margin="10dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/replyEditText"
android:layout_toLeftOf="@+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="@string/replyText"
/>



</RelativeLayout>