0

所以,如果数据库中是否存在 id,我会进行检查,id 是数字字符串,但数据库中的类型是 char(6)。

所以这是我的代码

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

第一个检查是检查输入是否为空,它现在是正确的,第二个检查是检查数据库中是否有一个 id,这就是问题所在。当我尝试一些像“0001”或“02013”​​这样的数值ID时,它很好,并且可以运行。但是当我刚要放“abushd”时,它就被迫关闭了。有谁知道我为什么得到这个?

这是 list.class

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

        Intent i = getIntent();
        String nopel = i.getStringExtra("nopel");
        String nama = i.getStringExtra("nama");
        String alamat = i.getStringExtra("alamat");
        String golongan = i.getStringExtra("golongan");

        TextView t1 = (TextView)findViewById(R.id.textView1);
        TextView t2= (TextView)findViewById(R.id.textView2);
        TextView t3 = (TextView)findViewById(R.id.textView3);
        TextView t4 = (TextView)findViewById(R.id.textView4);

        t1.setText(nopel);
        t2.setText(nama);
        t3.setText(alamat);
        t4.setText(golongan);
    }
}

这就是 logcat 上的内容

06-28 16:14:18.244: W/KeyCharacterMap(253): No keyboard for id 0
06-28 16:14:18.254: W/KeyCharacterMap(253): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-28 16:14:22.394: D/AndroidRuntime(253): Shutting down VM
06-28 16:14:22.404: W/dalvikvm(253): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-28 16:14:22.404: E/AndroidRuntime(253): Uncaught handler: thread main exiting due to uncaught exception
06-28 16:14:22.414: E/AndroidRuntime(253): java.lang.NullPointerException
06-28 16:14:22.414: E/AndroidRuntime(253):  at pdam.tirta.raharja.input$1.onClick(input.java:47)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.view.View.performClick(View.java:2364)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.view.View.onTouchEvent(View.java:4179)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.view.View.dispatchTouchEvent(View.java:3709)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-28 16:14:22.414: E/AndroidRuntime(253):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-28 16:14:22.414: E/AndroidRuntime(253):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-28 16:14:22.414: E/AndroidRuntime(253):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.os.Looper.loop(Looper.java:123)
06-28 16:14:22.414: E/AndroidRuntime(253):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-28 16:14:22.414: E/AndroidRuntime(253):  at java.lang.reflect.Method.invokeNative(Native Method)
06-28 16:14:22.414: E/AndroidRuntime(253):  at java.lang.reflect.Method.invoke(Method.java:521)
06-28 16:14:22.414: E/AndroidRuntime(253):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-28 16:14:22.414: E/AndroidRuntime(253):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-28 16:14:22.414: E/AndroidRuntime(253):  at dalvik.system.NativeStart.main(Native Method)
06-28 16:14:22.424: I/dalvikvm(253): threadid=7: reacting to signal 3
06-28 16:14:22.424: E/dalvikvm(253): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
4

1 回答 1

1

你正在使用jobj_dp.getInt("row"). 当然,如果“row”中包含的值不是整数,它会崩溃。

改为使用getString()

于 2012-06-28T09:14:38.610 回答