Forced closed only when put alphabetical string in edit text

Posted by Abdullah Al Mubarok on Stack Overflow See other posts from Stack Overflow or by Abdullah Al Mubarok
Published on 2012-06-28T09:05:14Z Indexed on 2012/06/28 9:15 UTC
Read the original article Hit count: 223

Filed under:
|

So, I make a checker if an id is in the database or not, the id is in numerical string, the type in database is char(6) though.

So this is my code

public class input extends Activity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.input);

        final EditText edittext = (EditText)findViewById(R.id.editText1);
        Button button = (Button)findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String nopel = edittext.getText().toString();               

                if(nopel.length() == 0){
                    Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
                }else{
                    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                    pairs.add(new BasicNameValuePair("nopel", nopel));

                    JSON json_dp = new JSON();
                    JSONObject jobj_dp = json_dp.getJSON("http://10.0.2.2/KP/pdam/nopel.php", pairs);

                    try {
                        if(jobj_dp.getInt("row") == 0){
                            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
                        }else{
                            String snopel = jobj_dp.getString("nopel");
                            String snama = jobj_dp.getString("nama");
                            String salamat = jobj_dp.getString("alamat");
                            String sgolongan = jobj_dp.getString("golongan");

                            Intent i = new Intent(input.this, list.class);
                            i.putExtra("nopel", snopel);
                            i.putExtra("nama", snama);
                            i.putExtra("alamat", salamat);
                            i.putExtra("golongan", sgolongan);
                            startActivity(i);
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   
                }
            }           
        });
    }
}

the first check is to check if an input is null, it's going right for now, the second check is to check if an id in the database, and it's the problem. When I try some id in numerical value like "0001" or "02013" it's fine, and can run. but when I just got to put "abushd" it forced close. anyone know why I got this?

© Stack Overflow or respective owner

Related posts about android

Related posts about android-edittext