我在做一个应用程序,我需要从用户采取投入和追加该输入以字符串,并以此作为数据解析 但编辑文本内容为空,即使一个网址我我在的EditText 输入文本我转换的EditText内容串如下EDITTEXT内容显示为零
EditText edit = (EditText)findViewById(R.Id.tv5);
而且里面onclicklistener
String data = edit.getText().toString();
任何人能告诉我为什么data.length()
是给我为零? 我的完整的主要活动是如下:
public class Pnr extends Activity {
EditText edit;
TextView text1;
Button button;
String pnr, check;
HttpClient client;
JSONObject json;
int s;
String URL = "pnrbuddy.com/pnrstatus=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pnr);
edit = (EditText) findViewById(R.id.tv5);
text1 = (TextView) findViewById(R.id.textView2);
button = (Button) findViewById(R.id.button1);
client = new DefaultHttpClient();
pnr = String.valueOf(edit.getText());
s = pnr.length();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Read().execute("trainNo");
}
});
}
public JSONObject pnrStatus(String key) throws ClientProtocolException,
IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(key);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
JSONObject last = new JSONObject();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
last = new JSONObject(data);
return last;
} else {
return last;
}
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = pnrStatus(pnr);
return json.getString(arg0[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return s + "";
}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
text1.setText(" " + result);
}
}
} 当我输入的字符串,而不是从EditText上接受的字符串URL与exsisting串代码工作 URL =“pnrbuddy/pnrstatus =会将myText”一起的; 为什么读音字无法从EDITTEXT得到字符串?
你确定你使用的是正确的ID活动创建不? –
忘了我的意见..这只是在TextView的类 – donfuxx
什么呢'提供的System.out.println(“您输入:” +数据);'打印logcat的? –