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

时间:2014-05-05 23:10:19

标签: 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_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();
            }
    }

}}

&#34; sendsentence&#34;在上面的php端正确显示了DoSocketProgramming类,但是当我尝试使用GetText()或SetText()函数向它添加一些Text时,我的应用程序崩溃了。 我该怎么办?

2 个答案:

答案 0 :(得分:1)

这个问题并没有太大的余地,但我会试一试。

显示使用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为EditText的{​​{1}},表明特定的R.id.id_st1_date_edit不在您的EditText布局文件中。

请记住,您在几个地方调用activity_main.xml我猜它实际上位于setContentView(...)布局文件(或其他布局文件)中。

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

findViewById(...)

答案 1 :(得分:0)

使用此代码在不创建对象i的情况下获取TextView的值。 e。通过findViewById(int)方法:

String text = ((TextView)findViewById(R.id.testtextview)).getText().toString();