gpt4 book ai didi

android - 使用android将数据保存到MySQL数据库

转载 作者:行者123 更新时间:2023-11-29 18:11:24 25 4
gpt4 key购买 nike

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

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

预约表格

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

}

错误如下:它告诉我们

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.(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) 

最佳答案

您正尝试在将 View 小部件引用分配给实际小部件之前访问它们。

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

android:onClick="btnSave"

在对小部件调用方法之前,您确实应该检查小部件引用,但如果您将尝试从中提取信息的代码移至 btbSave,那么您至少应该有机会工作。 > 方法。当它被调用时, onCreate 已经被调用,并且引用指向实际的小部件(假设你得到了所有 findViewById 的正确信息),并假设用户填写了在表单中,数据应该准备好从 View 中提取。

因此将此代码移至 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com