EditText.getText() returning empty string where EditText is not empty; it is set by android:hint=“Enter Bus Number”

I have 3 EditTexts. I am checking 1 of them named text. But I am not able to retrieve its value through getText(). It seems to be returning empty string. I stored text.getText.tostring into String Command. Then copied Command into Command2. Then try to set the contents of HTTPResult EditText using setText. But it is empty. I am so much confused. CODE:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:orientation="vertical"
    android:gravity="fill_vertical" android:layout_margin="10dp">


    <EditText
        android:id="@+id/text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:inputType="text"
        android:lines="1"

        android:hint="Enter Bus Number">

    </EditText>
    <EditText
        android:id="@+id/Bus_number"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:inputType="number"
        android:lines="1"
        android:hint="Bus Number is:">

    </EditText>

    <Button
        android:id="@+id/button_Connect"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Register new bus" />
    <EditText
        android:id="@+id/HTTPResult"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:inputType="number"
        android:lines="10"
        android:hint="Waiting for Result ...">

    </EditText>

</LinearLayout>

Activity:

public class MainActivity extends AppCompatActivity {
    Button btn_Connect ;
    EditText busNumberEdit;
    EditText text1;
    EditText HTTPResult;
    String busNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_Connect = (Button) findViewById(R.id.button_Connect);
        busNumberEdit = (EditText) findViewById(R.id.Bus_number);
        text1 = (EditText) findViewById(R.id.text);
        HTTPResult = (EditText) findViewById(R.id.HTTPResult);
        String Command = (String) text1.getText().toString();
        String Command2 = Command;
        HTTPResult.setText(Command2);
        String stop = (String) busNumberEdit.getText().toString();
        if(Command.compareTo("Enter Bus Number") == 0)
        {
            Toast.makeText(getApplicationContext(),"You download is resumed2",Toast.LENGTH_LONG).show();
            busNumber = (String) busNumberEdit.getText().toString();
            //MyTaskParams paramObj = new MyTaskParams(3, myServer, busNumber, Command2, "");
            //paramObj.print();
            //HTTPResult.setText(Command2);
            //conn.execute(paramObj);
        }
    }
}
评论