hello i'm creating an android application that fills up a form and send the details to the php script on the server side in form of a string . But the problem is that i'm not able to extract or put up any new value of EditText fields. Please help.. My code is somewhat like this..

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();
            }
    }

}}

the "sendsentence" in DoSocketProgramming class is shown properly at php side in the above but as soon as I try to append some Text to it using GetText() or SetText() functions, my application crashes. what should I do ??

有帮助吗?

解决方案

The question doesn't really have much to go on but I'll give it a shot.

The only line which shows using getText() is this one...

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

As mentioned in comments on the question the most likely cause is NullPointerException. If that's the case then id_st1_event_edit is null.

If that's the case then the following line in onCreate(...)

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

...is failing to find an EditText with resource id R.id.id_st1_date_edit which suggests that particular EditText isn't in your activity_main.xml layout file.

Bearing in mind you're calling setContentView(...) in several places I'm guessing it's actually in the browse_new.xml layout file (or another layout file).

If that's the case then your problem is that you can only use findViewById(...) to find views in the current content view. Move whichever calls to findViewById(...) to the methods which change the content view. Example...

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

其他提示

Use this code for getting value of TextView without creating object i. e. via findViewById(int) method:

String text = ((TextView)findViewById(R.id.testtextview)).getText().toString();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top