我正在使用WAMP Server。而且我很难解决这些错误。

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

预约表格

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)*


它指向字符串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) 

最佳答案

您试图在将视图小部件引用分配给实际小部件之前对其进行访问。

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

android:onClick="btnSave"


您确实应该在调用小部件引用之前对其进行检查,但是如果将尝试将其信息提取出的代码移到btbSave方法中,则至少应有机会工作。在调用它时,已调用onCreate,并且引用指向实际的小部件(假设您已正确使用所有findViewById),并假设用户填写了表格,则数据应已准备就绪从视图中提取出来。

因此,将这段代码移到btnSave(View v)方法中

public void btnSave (View view){

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

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

关于android - 使用Android将数据保存到MySQL数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47368636/

10-12 00:38
查看更多