我在类DriverDetails
中定义了以下方法。
每当我设置图像时,我都会得到这些NullPointerException
。谁能引导我通过这个?
08-25 13:10:28.156: E/AndroidRuntime(6890): FATAL EXCEPTION: main
08-25 13:10:28.156: E/AndroidRuntime(6890): java.lang.NullPointerException
08-25 13:10:28.156: E/AndroidRuntime(6890): at in.veho.vehodriver.DriverDetails.setCarImage(DriverDetails.java:692)
08-25 13:10:28.156: E/AndroidRuntime(6890): at in.veho.vehodriver.DriverDetails.access$5(DriverDetails.java:683)
08-25 13:10:28.156: E/AndroidRuntime(6890): at in.veho.vehodriver.DriverDetails$UploadCarImageTask.onPostExecute(DriverDetails.java:632)
08-25 13:10:28.156: E/AndroidRuntime(6890): at in.veho.vehodriver.DriverDetails$UploadCarImageTask.onPostExecute(DriverDetails.java:1)
08-25 13:10:28.156: E/AndroidRuntime(6890): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-25 13:10:28.156: E/AndroidRuntime(6890): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-25 13:10:28.156: E/AndroidRuntime(6890): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-25 13:10:28.156: E/AndroidRuntime(6890): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 13:10:28.156: E/AndroidRuntime(6890): at android.os.Looper.loop(Looper.java:137)
08-25 13:10:28.156: E/AndroidRuntime(6890): at android.app.ActivityThread.main(ActivityThread.java:5227)
08-25 13:10:28.156: E/AndroidRuntime(6890): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 13:10:28.156: E/AndroidRuntime(6890): at java.lang.reflect.Method.invoke(Method.java:511)
08-25 13:10:28.156: E/AndroidRuntime(6890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-25 13:10:28.156: E/AndroidRuntime(6890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-25 13:10:28.156: E/AndroidRuntime(6890): at dalvik.system.NativeStart.main(Native Method)
=============================================== ===============================
private void setDriverImage(String pValue) {
byte[] decodeString;
try {
Gson gson = new GsonBuilder().create();
Content content = gson.fromJson(pValue, Content.class);
String imageData = content.imagedata;
imageData = imageData.substring(imageData.indexOf(",") + 1);
decodeString = Base64.decode(imageData);
if (decodeString != null && decodeString.length > 0) {
mDriverImageBitmap = BitmapFactory.decodeByteArray(
decodeString, 0, decodeString.length);
byte[] lImage = TextUtilis.getBytes(mDriverImageBitmap);
TukxiCache.saveDataToDisk(Constants.UPDATED_DRIVER_IMAGE_TAG,
DriverDetails.this, true);
TukxiCache.saveDataToDisk(Constants.DRIVER_IMAGE_TAG,
DriverDetails.this, lImage);
mDriverImageField.setImageBitmap(mDriverImageBitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
}
=============================================== ===========================
public class UploadCarImageTask extends AsyncTask<Bitmap, Void, String> {
@Override
protected void onPreExecute() {
showCustomDialogs(PROGRESS_DIALOG);
}
@Override
protected String doInBackground(Bitmap... params) {
if (!NetworkUtility.isNetworkConnected(DriverDetails.this)) {
return getString(R.string.internet_fail);
}
String lResponse = TukxiDriverServerUtils.uploadCarImage(
DriverDetails.this, params[0], mUserInfo.getPhoneNumber(),
filePath); //here i m made change from file path to uri of the image
if (lResponse != null) {
return lResponse;
}
return null;
}
@Override
protected void onPostExecute(String result) {
dismissCustomDialogs(PROGRESS_DIALOG);
if (TextUtils.isEmpty(result)) {
DialogUtility.getOKDialog(DriverDetails.this, null,
getString(R.string.image_uploaded_successfully_text),
null).show();
setCarImage(result);
mUploadedCarImage = true;
updateContinueBtn();
} else {
DialogUtility.getOKDialog(DriverDetails.this, null,
getString(R.string.image_uploaded_failed_text), null)
.show();
}
}
}
=============================================== ======
public class Content
{
@SerializedName("imagedata")
public String imagedata;
}
=============================================== ===========
public static String uploadCarImage(Context pContext, Bitmap pBitmap,
String pPhoneNumber, String filePath) {
String type = "carphoto";
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT > Build.VERSION_CODES.FROYO) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// Making HTTP request
try {
// new url for new API
String result;
String Upload_Image_Url = Constants.main_url + Constants.driver_url
+ "/" + pPhoneNumber + Constants.driver_photo_upload_url
+ "/" + type;
Log.i("Upload_Image_Url", Upload_Image_Url);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Upload_Image_Url);
MultipartEntity entity = new MultipartEntity();
entity.addPart("type", new StringBody(type));
entity.addPart("phone", new StringBody(pPhoneNumber));
entity.addPart("image", new FileBody(new File(filePath),
"image/jpg"));
post.setEntity(entity);
Log.i("MimeTypeMap",MimeTypeMap.getFileExtensionFromUrl(filePath).toString());
HttpResponse response = client.execute(post);
//Log.i("car image response server",response.toString());
HttpEntity httpEntity = response.getEntity();
// receive response as inputStream
//InputStream inputStream = null;
InputStream inputStream = httpEntity.getContent();
TukxiDriverParsers.uploadPhotoResponse(pContext, inputStream);
} catch (Exception e) {
Constants.printLog("Upload Photo response ", "exception occured "
+ e.getMessage());
return e.getMessage();
}
return null;
}
=============================================== =============
public static String uploadPhotoResponse(Context pContext, InputStream lResponse) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(lResponse, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
lResponse.close();
String response = sb.toString();
Constants.printLog("uploadPhotoResponse ", ""+response);
return null;
}
解决: 解决了这个问题。我在响应中返回null。我修正了这个以获得正确的结果。
答案 0 :(得分:1)
您将从null
方法返回uploadCarImage
。
请参阅try-catch块之后的行。
此外,您的uploadPhotoResponse
始终返回null。
答案 1 :(得分:1)
将Getter / Setter添加到Content.java,
public class Content
{
@SerializedName("imagedata")
public String imagedata;
public string getImagedata(){
return imagedata;
}
public void setImagedata(String imageData){
imagedata = imageData;
}
}
在setDriverImage(String pValue)中,
String imageData = content.imagedata; //use content.getImagedata(); instead
imageData = imageData.substring(imageData.indexOf(",") + 1);
decodeString = Base64.decode(imageData);
供参考,请检查
http://www.mysamplecode.com/2012/05/java-convert-json-string-object.html http://examples.javacodegeeks.com/core-java/gson/convert-java-object-to-from-json-using-gson-example/ JSON GSON.fromJson Java Objects
希望它有效。快乐的编码:)