我試圖從mysql數據庫中檢索一些數據以便用該數據填充微調器。我設法將數據插入微調數據庫到MySQL數據庫,但當我嘗試檢索回它顯示在Logcat中,微調框爲空! 我在網上搜索,但我找不到解決我的問題。如何使用mysql數據庫中的數據填充Android微調器
這裏是從MySQL數據庫中檢索數據的Java代碼:
private static final String TAG_FACULTY_NAME = "faculty_name";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editactivity); //Connected to XML file for the interface
// save button
btnSave = (Button) findViewById(R.id.lecturerSave);
//Sharedpreference to get lecturer details
SharedPreferences sp = getSharedPreferences("logindetails", 0);
uid = sp.getString("uid", "-1");
// Getting complete lecturer details in background thread
new GetLecturerDetails().execute();
// save button click event
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// starting background task to update lecturer account
new SaveLecturerDetails().execute();
}
});
}
/**
* Background Async Task to Get complete lecturer account details
* */
class GetLecturerDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditActivity.this);
pDialog.setMessage("Loading. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting lecturer account details in background thread
* */
protected String doInBackground(String... params) {
//updating UI from Background Thread
// runOnUiThread(new Runnable() {
// public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("uid", uid));
// getting lecturer account details by making HTTP request
// Note that lecturer account details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_lecturer_details, "GET", params1);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received lecturer account details
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT); // JSON Array
// get first lecturer account object from JSON Array
final JSONObject product = productObj.getJSONObject(0);
// lecturer account with this uid found
// Edit Text
runOnUiThread(new Runnable() {
public void run() {
txtdrop_down = (Spinner) findViewById(R.id.spinner_faculty);
// display lecturer data in EditText
try
{
txtdrop_down.setTag(product.getString(TAG_FACULTY_NAME));
}catch (JSONException e)
{
e.printStackTrace();
}
}
});
} else{
// lecturer account with uid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
這是錯誤,我從logcat的有:
11-23 12:25:02.543: E/AndroidRuntime(2499): FATAL EXCEPTION: main
11-23 12:25:02.543: E/AndroidRuntime(2499): java.lang.NullPointerException
11-23 12:25:02.543: E/AndroidRuntime(2499): at android.widget.Spinner$DialogPopup.show(Spinner.java:935)
11-23 12:25:02.543: E/AndroidRuntime(2499): at android.widget.Spinner.performClick(Spinner.java:614)
11-23 12:25:02.543: E/AndroidRuntime(2499): at android.view.View$PerformClick.run(View.java:17721)
11-23 12:25:02.543: E/AndroidRuntime(2499): at android.os.Handler.handleCallback(Handler.java:730)
11-23 12:25:02.543: E/AndroidRuntime(2499): at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 12:25:02.543: E/AndroidRuntime(2499): at android.os.Looper.loop(Looper.java:137)
11-23 12:25:02.543: E/AndroidRuntime(2499): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-23 12:25:02.543: E/AndroidRuntime(2499): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 12:25:02.543: E/AndroidRuntime(2499): at java.lang.reflect.Method.invoke(Method.java:525)
11-23 12:25:02.543: E/AndroidRuntime(2499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-23 12:25:02.543: E/AndroidRuntime(2499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 12:25:02.543: E/AndroidRuntime(2499): at dalvik.system.NativeStart.main(Native Method)
能不能請你幫我解決這個問題。 謝謝。
取值並顯示它沒有u得到微調ID:像 微調S =(微調)findviewbyid(R.id.Spinner); – Dev
@iCodeAtAndroid感謝您的回覆,是的,我做到了:txtdrop_down =(Spinner)findViewById(R.id.spinner_faculty); – Mack
嗨,我設法解決與Android微調問題。問題是我沒有爲spinner聲明setOnItemSelectedListener,這就是爲什麼它顯示nullpointerexception。感謝你們。 – Mack