String not passing to second Android activity

2019-09-16 18:17发布

问题:

I'm trying to start a new activity in my android app, and I'm trying to pass a simple string from the first activity to the second activity. I want the string to display in a textview for now in the second activity, but it just won't work. What am I doing wrong?

The initial activity:

package com.amritayalur.mypowerschool;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MyPowerSchoolActivity extends Activity {
Button buttonSubmit;
TextView TextViewTitle;
TextView TextViewDesc;
EditText EditTextURL;
String url = "";
 String str;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    TextViewTitle = (TextView) findViewById(R.id.TextViewTitle);
    TextViewDesc = (TextView) findViewById(R.id.TextViewDesc);

    EditTextURL = (EditText) findViewById(R.id.EditTextURL);


    //Start TextView
    TextViewTitle.setText("MyPowerSchool");


    //button listener
    buttonSubmit.setOnClickListener(new View.OnClickListener() {


        String url = EditTextURL.getText().toString();


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent( MyPowerSchoolActivity.this,
creds.class);    


            i.putExtra("pschoolurl", url);
            final int result = 1;
            startActivityForResult(i, result); 

        }

    });



}       
}

The second activity:

package com.amritayalur.mypowerschool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.webkit.WebView;
import android.widget.Toast;
import android.widget.TextView;



public class creds extends Activity {

String test;
TextView TextViewTest;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);




    Intent intent = getIntent();
    String test = intent.getExtras().getString("pschoolurl");

    TextViewTest = (TextView) findViewById(R.id.TextViewTest);
    TextViewTest.setText(test);

}
}

I know the textview displays properly in the second activity; it works with any dummy text, just not the variable.

回答1:

You instantiate the string way too early, when creating the listener, instead, you should get it when the listener gets called:

    @Override
    public void onClick(View v) {
        Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);    
        i.putExtra("pschoolurl", EditTextURL.getText().toString());
        // get the text here     ^^^
        final int result = 1;
        startActivityForResult(i, result); 
    }

BTW, please follow the naming conventions and start variables with a lower-case letter (editTextURL) it will make the code less confusing for other people (like me :) )



回答2:

The value for url is getting set at instantiation time for your OnClickListener, before you have put any value into it. Try reading the value from your TextView in the body of the onClick method instead.



回答3:

Its notting wrong in your code just remove the String beside url in buttonsubmit.onclicklistener method. you dont have to startanactivity for result if u juat want to send a string from one activity to another. Thy this in your second activity.

Bundle b=getIntent().getExtras();
String url = b.getInt("pschoolurl");


回答4:

check this out just put this code in your onclick of first activity Note:-> directly capture the edittext donot save the edit text in string and then pass it ,if you will do show then string will not pass. see the below example

public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i = new Intent(this,Web.class);
    i.putExtra("epuzzle",urlenter.getText().toString());
    final int result = 1;
        startActivityForResult(i, result);
    }

now in second activity put this ,Note:-> is you will try to save the passeed string of activity first in to another string in activity second then it will not work ,you should directly pass the string to loadurl instead of saving and passing to loadurl.

  Intent intent = getIntent();
   w.loadUrl(intent.getExtras().getString("epuzzle"));
   w.getSettings().setJavaScriptEnabled(true);

plz let me know if it worked for you or not.