我的应用程序中有一个微调器。该微调器将填充来自服务器的一些数据。在我的 logcat 中,我成功解析了 JSON 数据,但我不知道为什么当我尝试设置适配器时出现java.lang.NullPointerException
错误,这是我的日志:
10-31 04:59:01.607: E/AndroidRuntime(28692): FATAL EXCEPTION: main
10-31 04:59:01.607: E/AndroidRuntime(28692): java.lang.NullPointerException
10-31 04:59:01.607: E/AndroidRuntime(28692): at java.util.Arrays$ArrayList.<init>(Arrays.java:47)
10-31 04:59:01.607: E/AndroidRuntime(28692): at java.util.Arrays.asList(Arrays.java:169)
10-31 04:59:01.607: E/AndroidRuntime(28692): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
10-31 04:59:01.607: E/AndroidRuntime(28692): at com.example.androidsql.NewProductActivity$getCity$1.run(NewProductActivity.java:437)
10-31 04:59:01.607: E/AndroidRuntime(28692): at android.app.Activity.runOnUiThread(Activity.java:3743)
10-31 04:59:01.607: E/AndroidRuntime(28692): at com.example.androidsql.NewProductActivity$getCity.onPostExecute(NewProductActivity.java:433)
10-31 04:59:01.607: E/AndroidRuntime(28692): at com.example.androidsql.NewProductActivity$getCity.onPostExecute(NewProductActivity.java:1)
10-31 04:59:01.607: E/AndroidRuntime(28692): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-31 04:59:01.607: E/AndroidRuntime(28692): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-31 04:59:01.607: E/AndroidRuntime(28692): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-31 04:59:01.607: E/AndroidRuntime(28692): at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 04:59:01.607: E/AndroidRuntime(28692): at android.os.Looper.loop(Looper.java:130)
10-31 04:59:01.607: E/AndroidRuntime(28692): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-31 04:59:01.607: E/AndroidRuntime(28692): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 04:59:01.607: E/AndroidRuntime(28692): at java.lang.reflect.Method.invoke(Method.java:507)
10-31 04:59:01.607: E/AndroidRuntime(28692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
10-31 04:59:01.607: E/AndroidRuntime(28692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
10-31 04:59:01.607: E/AndroidRuntime(28692): at dalvik.system.NativeStart.main(Native Method)
这是我的java类:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
ctx = this;
//define all input text
longitude = (EditText)findViewById(R.id.et_longitude);
latitude = (EditText)findViewById(R.id.et_latitude);
merchant = (EditText)findViewById(R.id.et_merchant);
alamat = (EditText)findViewById(R.id.et_alamat);
telp = (EditText)findViewById(R.id.et_telpon);
hp = (EditText)findViewById(R.id.et_hape);
bb = (EditText)findViewById(R.id.et_bb);
email = (EditText)findViewById(R.id.et_email);
//spinner
spinCategory = (Spinner)findViewById(R.id.spin_category);
dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCategory.setAdapter(dataAdapter1);
new getCity().execute();
spinCategoryDetail = (Spinner)findViewById(R.id.spin_category_detail);
//define path directory
et_uploadlogo = (EditText)findViewById(R.id.et_path_uploadlogo);
et_uploadfoto_f = (EditText)findViewById(R.id.et_path_uploadfoto_f);
et_uploadfoto_s = (EditText)findViewById(R.id.et_path_uploadfoto_s);
//define image
img_logo = (ImageView)findViewById(R.id.imgview_logo);
img_foto_f = (ImageView)findViewById(R.id.imgview_foto_f);
img_foto_s = (ImageView)findViewById(R.id.imgview_foto_s);
//OK button
imgbtn_ok = (ImageButton)findViewById(R.id.imgbtn_ok);
//refresh button
btn_refresh = (Button)findViewById(R.id.btn_refreshlonglat);
//button browse
btn_browse_ulogo = (Button)findViewById(R.id.btn_browse_uploadlogo);
btn_browse_ufoto_f = (Button)findViewById(R.id.btn_browse_uploadfoto_f);
btn_browse_ufoto_s = (Button)findViewById(R.id.btn_browse_uploadfoto_s);
//location service
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1000, this.locationListener);
//get CurretLocation
showCurrentLocation();
//action click buttton browse image logo
btn_browse_ulogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent ig = new Intent();
ig.setType("image/*");
ig.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(ig, "Select Picture"), IMAGE_GALLERY_LOGO);
}
});
//action click buttton browse image foto1
btn_browse_ufoto_f.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent ig = new Intent();
ig.setType("image/*");
ig.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(ig, "Select Picture"), IMAGE_GALLERY_FOTO_F);
}
});
//action click buttton browse image foto2
btn_browse_ufoto_s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent ig = new Intent();
ig.setType("image/*");
ig.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(ig, "Select Picture"), IMAGE_GALLERY_FOTO_S);
}
});
//refresh longitude,latitude
btn_refresh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
imgbtn_ok = (ImageButton)findViewById(R.id.imgbtn_ok);
imgbtn_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//post data
new CreateNewProduct().execute();
}
});
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_GALLERY_LOGO) {
if (resultCode == RESULT_OK) {
fileUri_logo = data.getData();
selectedImagePath = getPath(fileUri_logo);
et_uploadlogo.setText(selectedImagePath);
img_logo.setImageURI(fileUri_logo);
}
} else {
if (requestCode == IMAGE_GALLERY_FOTO_F) {
fileUri_foto_f = data.getData();
selectedImagePath = getPath(fileUri_foto_f);
et_uploadfoto_f.setText(selectedImagePath);
img_foto_f.setImageURI(fileUri_foto_f);
} else {
if (requestCode == IMAGE_GALLERY_FOTO_S) {
fileUri_foto_s = data.getData();
selectedImagePath = getPath(fileUri_foto_s);
et_uploadfoto_s.setText(selectedImagePath);
img_foto_s.setImageURI(fileUri_foto_s);
}
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
protected void showCurrentLocation() {
Location location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
String lat = String.valueOf(location.getLatitude());
String lon = String.valueOf(location.getLongitude());
latitude.setText(lat);
longitude.setText(lon);
}
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewProductActivity.this);
pDialog.setMessage("Loading, saving data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating data
* */
protected String doInBackground(String... args) {
slongitude = longitude.getText().toString();
slatitude = latitude.getText().toString();
smerchant = merchant.getText().toString();
salamat = alamat.getText().toString();
stelp = telp.getText().toString();
shp = hp.getText().toString();
sbb = bb.getText().toString();
semail = email.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Pk_Merchant_ID", ""));
params.add(new BasicNameValuePair("Fk_Ms_DataLevel2_ID", "2"));
params.add(new BasicNameValuePair("Fk_Ms_City_ID", "2"));
params.add(new BasicNameValuePair("Fk_Ms_Sales_ID_CreatedBy", "1"));
params.add(new BasicNameValuePair("FK_Ms_Sales_ID_LastUpdateBy", "1"));
params.add(new BasicNameValuePair("CreatedDated", ""));
params.add(new BasicNameValuePair("LastUpdateDated", ""));
params.add(new BasicNameValuePair("merchant", smerchant));
params.add(new BasicNameValuePair("alamat", salamat));
params.add(new BasicNameValuePair("telp", stelp));
params.add(new BasicNameValuePair("hp", shp));
params.add(new BasicNameValuePair("bb", sbb));
params.add(new BasicNameValuePair("email", semail));
params.add(new BasicNameValuePair("longitude", slongitude));
params.add(new BasicNameValuePair("latitude", slatitude));
params.add(new BasicNameValuePair("Is_Deleted", ""));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_merchant,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Log.d("Berhasil", "Good");
Intent i = new Intent(getApplicationContext(), PreviewActivity.class);
i.putExtra("lon", slongitude);
i.putExtra("lat", slatitude);
i.putExtra("mer", smerchant);
i.putExtra("ala", salamat);
i.putExtra("tel", stelp);
i.putExtra("hp", shp);
i.putExtra("pbb", sbb);
i.putExtra("ema", semail);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
Log.d("Gagal", e.toString());
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
class getCity extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
List<NameValuePair> param = new ArrayList<NameValuePair>();
JSONObject json = jsonParser.makeHttpRequest(url_spincategory, "GET", param);
Log.d("Get List Spinner Category :", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
data = json.getJSONArray(TAG_Ms_DataLevel1);
for (int i=0; i<data.length(); i++) {
JSONObject j = data.getJSONObject(i);
Pk_Ms_DataLevel1_ID[i] = j.getString(TAG_Pk_Ms_DataLevel1_ID);
Descriptions[i] = j.getString(TAG_Descriptions);
list_category = new ArrayList<String>();
list_category.add(Descriptions[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(ctx,
android.R.layout.simple_spinner_item, Descriptions);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCategory.setAdapter(dataAdapter1);
}
});
}
}
}
我需要您的建议、评论和您的所有帮助。感谢提前。