gpt4 book ai didi

android - getText() 和 setText() 在 android 代码中不起作用

转载 作者:行者123 更新时间:2023-11-30 02:54:42 26 4
gpt4 key购买 nike

你好我正在创建一个 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_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();
}
}

}}

DoSocketProgramming 类中的“sendsentence”在上面的 php 端正确显示,但是当我尝试使用 GetText() 或 SetText() 函数向其添加一些文本时,我的应用程序崩溃了。我该怎么办??

最佳答案

这个问题真的没什么可继续的,但我会试一试。

使用 getText() 显示的唯一一行是这一行...

//sendsentence=id_st1_event_edit.getText().toString();

正如问题评论中提到的,最可能的原因是 NullPointerException。如果是这种情况,则 id_st1_event_edit 为空。

如果是这种情况,则 onCreate(...) 中的以下行

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

...无法找到资源 ID 为 R.id.id_st1_date_editEditText,这表明特定的 EditText 不在你的 activity_main.xml 布局文件。

记住你在几个地方调用 setContentView(...) 我猜它实际上是在 browse_new.xml 布局文件(或另一个布局文件)。

如果是这种情况,那么您的问题是您只能使用 findViewById(...) 在当前内容 View 中查找 View 。将对 findViewById(...) 的调用移动到更改内容 View 的方法。示例...

public void browseEvent(View V)
{
setContentView(R.layout.browse_new);
// Call findViewById(...) for any widgets in browse_new.xml here
}

关于android - getText() 和 setText() 在 android 代码中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23483404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com