getText()和setText()在android代码中不起作用
您好,我正在创建一个android应用程序,它填充一个表单,并以字符串的形式将详细信息发送到服务器端的php脚本。但问题是我无法提取或设置EditText字段的任何新值。请帮忙。。 我的代码有点像这样getText()和setText()在android代码中不起作用,android,gettext,settext,Android,Gettext,Settext,您好,我正在创建一个android应用程序,它填充一个表单,并以字符串的形式将详细信息发送到服务器端的php脚本。但问题是我无法提取或设置EditText字段的任何新值。请帮忙。。 我的代码有点像这样 EditText id_st1_event_edit,id_st1_date_edit,id_st1_time_edit,id_st1_venue_edit; EditText id_st2_name_edit,id_st2_phone_edit,id_st2_phone_edit2,id_st2
EditText id_st1_event_edit,id_st1_date_edit,id_st1_time_edit,id_st1_venue_edit;
EditText id_st2_name_edit,id_st2_phone_edit,id_st2_phone_edit2,id_st2_email_edit,id_st2_email_edit2;
EditText id_st3_desc_edit;
//Declaration of static IP Address of the server side
public static String addr = "10.0.2.2";
public String sendsentence;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Attaching the entities with their proper ID Components
welcome=(TextView)findViewById(R.id.welcome_message);
create_event=(TextView)findViewById(R.id.id_create_event);
browse_event=(TextView)findViewById(R.id.id_browse_event);
about_us=(TextView)findViewById(R.id.id_about_us);
id_st1_time=(TextView)findViewById(R.id.id_st1_time);
id_st1_venue=(TextView)findViewById(R.id.id_st1_venue);
id_st1_event=(TextView)findViewById(R.id.id_st1_event);
id_st1_date=(TextView)findViewById(R.id.id_st1_date);
id_st1_next=(TextView)findViewById(R.id.id_st1_next);
id_st2_name=(TextView)findViewById(R.id.id_st2_name);
id_st2_phone=(TextView)findViewById(R.id.id_st2_phone);
id_st2_email=(TextView)findViewById(R.id.id_st2_email);
about_organiser=(TextView)findViewById(R.id.about_organiser);
id_st3_event_description=(TextView)findViewById(R.id.id_st3_event_description);
id_st1_time_edit=(EditText)findViewById(R.id.id_st1_time_edit);
id_st1_date_edit=(EditText)findViewById(R.id.id_st1_date_edit);
id_st1_event_edit=(EditText)findViewById(R.id.id_st1_event_edit);
id_st1_venue_edit=(EditText)findViewById(R.id.id_st1_venue_edit);
id_st2_name_edit=(EditText)findViewById(R.id.id_st2_name_edit);
id_st2_phone_edit=(EditText)findViewById(R.id.id_st2_phone_edit);
id_st2_phone_edit2=(EditText)findViewById(R.id.id_st2_phone_edit2);
id_st2_email_edit=(EditText)findViewById(R.id.id_st2_email_edit);
id_st2_email_edit2=(EditText)findViewById(R.id.id_st2_email_edit2);
id_st3_desc_edit=(EditText)findViewById(R.id.id_st3_desc_edit);
butt_submit=(Button)findViewById(R.id.butt_submit);
}
public void onBackPressed() {
Toast.makeText(getApplicationContext()," Exiting Application ", Toast.LENGTH_SHORT).show();
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void createNew(View V)
{
setContentView(R.layout.create_new_event);
tabhost= (TabHost)findViewById(R.id.tabhost);
tabhost.setup();
TabHost.TabSpec spec;
spec=tabhost.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("Step 1");
tabhost.addTab(spec);
spec=tabhost.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Step 2");
tabhost.addTab(spec);
spec=tabhost.newTabSpec("tag3");
spec.setContent(R.id.tab3);
spec.setIndicator("Step 3");
tabhost.addTab(spec);
}
public void aboutUs(View V)
{
setContentView(R.layout.about_us);
}
public void browseEvent(View V)
{
setContentView(R.layout.browse_new);
}
public void to_next(View v)
{
tabhost= (TabHost)findViewById(R.id.tabhost);
tabhost.setCurrentTab((tabhost.getCurrentTab())+1);
}
public void to_previous(View v)
{
tabhost= (TabHost)findViewById(R.id.tabhost);
tabhost.setCurrentTab((tabhost.getCurrentTab())-1);
}
public void submit_data(View V)
{
try{
new DoSocketProgramming(this).execute("10.0.2.2");
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "problem here", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "created till here", Toast.LENGTH_SHORT).show();
}
public class DoSocketProgramming extends AsyncTask<String, Void, String>
{
String sendsentence="create table ";
String recvsentence;
Context context;
//sendsentence=id_st1_event_edit.getText().toString();
private DoSocketProgramming(Context context){
this.context = context;
}
protected void onPreExecute()
{
Toast.makeText(getApplicationContext(), "this is preExecute", Toast.LENGTH_LONG).show();
}
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
Socket con=new Socket(addr,1678);
DataInputStream dis= new DataInputStream(con.getInputStream());
DataOutputStream dos=new DataOutputStream(con.getOutputStream());
dos.writeUTF(sendsentence);
recvsentence=dis.readUTF();
}catch(Exception e)
{
e.printStackTrace();
}
Log.d("myApp","i've reached at doinbackground");
return recvsentence;
}
protected void onPostExecute(String result) {
try{
Log.d("myApp","i've reached on PostExecute");
Toast.makeText(context, "this is post execute "+ result, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}}
EditText id\u st1\u event\u edit、id\u st1\u date\u edit、id\u st1\u time\u edit、id\u st1\u vention\u edit;
EditText id_st2_name_edit,id_st2_phone_edit,id_st2_phone_edit2,id_st2_email_edit2;
编辑文本id_st3_desc_edit;
//服务器端静态IP地址的声明
公共静态字符串addr=“10.0.2.2”;
公开字句;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//将实体与其正确的ID组件连接在一起
欢迎=(TextView)findViewById(R.id.welcome_消息);
create\u event=(TextView)findViewById(R.id.id\u create\u event);
browse\u event=(TextView)findViewById(R.id.id\u browse\u event);
about_us=(TextView)findViewById(R.id.id_about_us);
id\u st1\u time=(TextView)findViewById(R.id.id\u st1\u time);
id\u st1\u场馆=(文本视图)findViewById(R.id.id\u st1\u场馆);
id\u st1\u事件=(TextView)findViewById(R.id.id\u st1\u事件);
id\u st1\u date=(TextView)findViewById(R.id.id\u st1\u date);
id\u st1\u next=(TextView)findViewById(R.id.id\u st1\u next);
id_st2_name=(TextView)findViewById(R.id.id_st2_name);
id_st2_phone=(TextView)findViewById(R.id.id_st2_phone);
id_st2_email=(TextView)findViewById(R.id.id_st2_email);
about_Organizer=(TextView)findViewById(R.id.about_Organizer);
id\u st3\u event\u description=(TextView)findViewById(R.id.id\u st3\u event\u description);
id\u st1\u time\u edit=(EditText)findViewById(R.id.id\u st1\u time\u edit);
id\u st1\u date\u edit=(EditText)findViewById(R.id.id\u st1\u date\u edit);
id\u st1\u event\u edit=(EditText)findViewById(R.id.id\u st1\u event\u edit);
id\u st1\u vention\u edit=(EditText)findViewById(R.id.id\u st1\u vention\u edit);
id\u st2\u name\u edit=(EditText)findViewById(R.id.id\u st2\u name\u edit);
id\u st2\u phone\u edit=(EditText)findViewById(R.id.id\u st2\u phone\u edit);
id_st2_phone_edit2=(EditText)findViewById(R.id.id_st2_phone_edit2);
id_st2_email_edit=(EditText)findViewById(R.id.id_st2_email_edit);
id_st2_email_edit2=(EditText)findViewById(R.id.id_st2_email_edit2);
id_st3_desc_edit=(EditText)findViewById(R.id.id_st3_desc_edit);
butt_submit=(按钮)findViewById(R.id.butt_submit);
}
public void onBackPressed(){
Toast.makeText(getApplicationContext(),“正在退出应用程序”,Toast.LENGTH_SHORT.show();
完成();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共视图新建(视图V)
{
setContentView(R.layout.create_new_事件);
tabhost=(tabhost)findViewById(R.id.tabhost);
tabhost.setup();
TabHost.TabSpec;
spec=tabhost.newTabSpec(“tag1”);
规格设置内容(R.id.tab1);
规格设置指示器(“步骤1”);
tabhost.addTab(spec);
spec=tabhost.newTabSpec(“tag2”);
规格设置内容(R.id.tab2);
规格设置指示器(“步骤2”);
tabhost.addTab(spec);
spec=tabhost.newTabSpec(“tag3”);
规格设置内容(R.id.tab3);
规格设置指示器(“步骤3”);
tabhost.addTab(spec);
}
公共空间(视图五)
{
setContentView(R.layout.about_us);
}
公共无效浏览事件(视图五)
{
setContentView(R.layout.browse\u new);
}
下一页的公共作废(视图五)
{
tabhost=(tabhost)findViewById(R.id.tabhost);
tabhost.setCurrentTab((tabhost.getCurrentTab())+1);
}
公共作废至上一页(视图五)
{
tabhost=(tabhost)findViewById(R.id.tabhost);
tabhost.setCurrentTab((tabhost.getCurrentTab())-1);
}
公共作废提交数据(视图五)
{
试一试{
新的DoSocketProgramming(本)。执行(“10.0.2.2”);
}
捕获(例外e){
Toast.makeText(getApplicationContext(),“这里有问题”,Toast.LENGTH\u SHORT.show();
}
Toast.makeText(getApplicationContext(),“创建到这里”,Toast.LENGTH\u SHORT.show();
}
公共类DoSocketProgramming扩展了异步任务
{
String sendstension=“创建表”;
字符串接收;
语境;
//sendstation=id_st1_event_edit.getText().toString();
专用DoSocketProgramming(上下文){
this.context=上下文;
}
受保护的void onPreExecute()
{
Toast.makeText(getApplicationContext(),“这是预先执行的”,Toast.LENGTH\u LONG.show();
}
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
试试{
插座con=新插座(地址1678);
DataInputStream dis=新的DataInputStream(con.getInputStream());
DataOutputStream dos=新的DataOutputStream(con.getOutputStream());
dos.writeUTF(发送句子);
recvsentence=dis.readUTF();
}捕获(例外e)
{
e、 printStackTrace();
}
Log.d(“myApp”,“我已经到达doinbackground”);
回执;
}
受保护的void onPostExecute(字符串结果){
试一试{
Log.d(“myApp”,“我在执行后已到达”);
Toast.makeText(上下文,“这是后期执行”+结果,Toast.LENGTH_LONG.show();
}
捕获(例外e)
{
e、 printStackTrace();
}
}
}}
DoSocketProgramming类中的“sendSession”在上面的php端正确显示,但当我尝试使用GetText()或settText()函数向其添加一些文本时,我的应用程序崩溃。
我该怎么办?这个问题其实没什么可谈的,但我会试一试 使用
getText()
显示的唯一一行是
//sendsentence=id_st1_event_edit.getText().toString();
正如在对que的评论中提到的
id_st1_date_edit=(EditText)findViewById(R.id.id_st1_date_edit);
public void browseEvent(View V)
{
setContentView(R.layout.browse_new);
// Call findViewById(...) for any widgets in browse_new.xml here
}
String text = ((TextView)findViewById(R.id.testtextview)).getText().toString();