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

问题描述 投票:1回答:2

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

}}

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

android gettext settext
2个回答
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为EditTextR.id.id_st1_date_edit,这表明特定的EditText不在您的activity_main.xml布局文件中。

考虑到您要在多个地方调用setContentView(...),我想它实际上是在browse_new.xml布局文件(或另一个布局文件)中。

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

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.