使用android

时间:2017-11-18 16:56:57

标签: android mysql

我正在使用WAMP服务器。我很难解决这些错误。

我正在尝试使用android将数据保存到mysql数据库。当我试图保存数据时,我遇到了错误。这是我的活动。

appointment_form

public class appointment_form extends Activity{

//Instance Field


private EditText txtfname;
private EditText txtaddress;
private Spinner spoffice;
private Spinner spservices;
private EditText editdate;
private EditText edittime;

//getting data from edittext and spinner

String fname = txtfname.getText().toString();
String address = txtaddress.getText().toString();
String office = spoffice.getSelectedItem().toString();
String services = spservices.getSelectedItem().toString();
String date = editdate.getText().toString();
String time = edittime.getText().toString();

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appointment_form);

    txtfname =(EditText) findViewById(R.id.fnametxt);
    txtaddress =(EditText) findViewById(R.id.editAddress);

    editdate =(EditText) findViewById(R.id.schedDate);
    edittime =(EditText) findViewById(R.id.schedTime);


    spoffice =(Spinner) findViewById(R.id.SPoffice);
    spservices =(Spinner) findViewById(R.id.SPservice);

}
    public void btnSave (View view){

    inserttoDatabase(fname,address,date,time,office,services);
}

private void inserttoDatabase(final String fname,final String address,final String date,final String time,final String office,final String services) {
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            String paramUsename = params[0];

            List<NameValuePair> nameValuePairs = new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("fname", fname));
            nameValuePairs.add(new BasicNameValuePair("address", address));
            nameValuePairs.add(new BasicNameValuePair("date", date));
            nameValuePairs.add(new BasicNameValuePair("time", time));
            nameValuePairs.add(new BasicNameValuePair("office", office));
            nameValuePairs.add(new BasicNameValuePair("services", services));


            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://192.168.254.110/Android/insert.php");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity entity = response.getEntity();


            } catch (ClientProtocolException e) {

            } catch (IOException e) {

            }
            return "success";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        }
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(fname, address, date, time, office, services);
}

}

这是错误: 它告诉

  

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.text.Editable android.widget.EditText.getText()'                     在com.xtia.rockygwapo.iccharter.appointment_form。(appointment_form.java:58)*

它指向 String fname = txtfname.getText()。toString();

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.xtia.rockygwapo.iccharter, PID: 31930
              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xtia.rockygwapo.iccharter/com.xtia.rockygwapo.iccharter.appointment_form}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2477)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.app.ActivityThread.main(ActivityThread.java:5728)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at com.xtia.rockygwapo.iccharter.appointment_form.<init>(appointment_form.java:58)
                  at java.lang.Class.newInstance(Native Method)
                  at android.app.Instrumentation.newActivity(Instrumentation.java:1072)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2467)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                  at android.os.Looper.loop(Looper.java:207) 
                  at android.app.ActivityThread.main(ActivityThread.java:5728) 
                  at java.lang.reflect.Method.invoke(Native Method) 

1 个答案:

答案 0 :(得分:0)

您尝试在将视图窗口小部件引用分配给实际窗口小部件之前访问它们。

我假设您在布局文件中有一个具有以下属性的按钮,并且您希望有人填写表单,然后单击该按钮,对吧?

android:onClick="btnSave"

你真的应该在调用方法之前检查小部件引用,但是如果你移动试图从中提取信息到{{1}的代码,你至少应该有可能工作。 }} 方法。调用它时,btbSave已被调用,引用指向实际的小部件(假设您的所有onCreate都正确),并假设用户填写了表单,数据应该可以从视图中提取。

所以将此代码移到findViewById方法

btnSave(View v)