字符串不传递给第二Android的活动 [英] String not passing to second Android activity
问题描述
我想开始一个新的活动在我的Android应用程序,我想从第一个活动传递一个简单的字符串,以第二个活动。我想在一个TextView现在在第二个活动的字符串来显示,但它只是将无法正常工作。我究竟做错了什么?
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?
最初的活动:
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);
}
}
我知道TextView的正确显示在第二活动;它的工作原理与任何虚拟文本,只是不变量。
I know the textview displays properly in the second activity; it works with any dummy text, just not the variable.
推荐答案
您实例化字符串为时尚早,创建侦听器时,相反,你应该得到它时,监听器被调用:
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);
}
顺便说一句,请遵循命名约定,用小写字母开头的变量(editTextURL)就会使code减少混乱为其他人(比如我:))
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 :) )
这篇关于字符串不传递给第二Android的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!