-2
我是Android新手,这是我的第一个项目。我有调查页面,可以选择上传照片。 当我从厨房中选择照片时,出现错误。错误似乎在753行:String [] strArray = str.split(“,”);并在554上:String [] strArray = str.split(“,”); 我不得不删除大量张贴限制
PublicSurvay.java尝试在空对象引用上调用虚拟方法'java.lang.String [] java.lang.String.split(java.lang.String)'
public class PublicSurvay extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, LocationListener {
CheckBox c1, c2, c3, c4_sign;
String URLDATA, URLNEWDATA;
Location location;
LocationManager manager;
double l1 = 0, l2 = 0, l3 = 0, l4 = 0, l5 = 0, l6 = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn_upload1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(PublicSurvay.this).setTitle("Select picture").setMessage("Choose Any one")
.setPositiveButton("Choose From Gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i1) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 100);
}
}).setNegativeButton("Choose From Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 101);
}
}).show();
}
});
btn_upload2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(PublicSurvay.this).setTitle("Select picture").setMessage("Choose Any one")
.setPositiveButton("Choose From Gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i1) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 200);
}
}).setNegativeButton("Choose From Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 102);
}
}).show();
}
});
btn_upload3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(PublicSurvay.this).setTitle("Select picture").setMessage("Choose Any one")
.setPositiveButton("Choose From Gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i1) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 300);
}
}).setNegativeButton("Choose From Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 103);
}
}).show();
}
});
try {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location == null) {
location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
l1 = location.getLatitude();
l2 = location.getLongitude();
} else {
l1 = location.getLatitude();
l2 = location.getLongitude();
}
} catch (Exception e) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog = ProgressDialog.show(PublicSurvay.this, "", "Sending data...", true);
URLDATA = "http://www.avj.890m.com/publicservay.php?uname=" + uname + "&email=" + email + "&address=" + address + "&tro_milk=" + tropical1 + "&c1=" + c_1 + "&c2=" + c_2 + "&c3=" + c_3 + "&plumeria=" + palumaria + "&terms=" + c_4 + "&lat=" + l1 + "&log1=" + l2 + "&l3=" + l3 + "&l4=" + l4 + "&l5=" + l5 + "&l6=" + l6;
URLDATA = URLDATA.replaceAll(" ", "%20");
Log.e("URLDATA", URLDATA);
new Thread(new Runnable() {
public void run() {
uploadFile1(uname, email, address, tropical1, c_1, c_2, c_3, palumaria, c_4, l1, l2, l3, l4, l5, l6);
}
}).start();
} else {
dialog = ProgressDialog.show(PublicSurvay.this, "", "Sending data...", true);
URLDATA = "http://www.avj.890m.com/publicservay.php?uname=" + uname + "&email=" + email + "&address=" + address + "&tro_milk=" + tropical1 + "&img_tro_milk=" + img1 + "&c1=" + c_1 + "&c2=" + c_2 + "&c3=" + c_3 + "&img_milk_spec=" + img2 + "&plumeria=" + palumaria + "&img_pulmeria=" + img3 + "&terms=" + c_4 + "&lat=" + l1 + "&log1=" + l2 + "&l3=" + l3 + "&l4=" + l4 + "&l5=" + l5 + "&l6=" + l6;
URLDATA = URLDATA.replaceAll(" ", "%20");
Log.e("URLDATA", URLDATA);
new Thread(new Runnable() {
public void run() {
uploadFile(uname, email, address, tropical1, img1, c_1, c_2, c_3, img2, palumaria, img3, c_4, l1, l2, l3, l4, l5, l6);
}
}).start();
}
}
});
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton == c1) {
if (!b) {
} else {
c_1 = "true";
}
}
if (compoundButton == c2) {
if (!b) {
} else {
c_2 = "true";
}
}
if (compoundButton == c3) {
if (!b) {
} else {
c_3 = "true";
}
}
if (compoundButton == c4_sign) {
if (!b) {
} else {
c_4 = "true";
}
}
}
@Override
public void onLocationChanged(Location location) {
l1 = location.getLatitude();
l2 = location.getLongitude();
}
public int uploadFile(String uname, String email, String addressdata, String tropiw, String sourceFileUri, String
c1, String c2, String c3, String img11, String palumaria, String img22, String terms, double lat, double log1, double l3, double l4, double l5, double l6) {
try {
String fileName = sourceFileUri;
String fileName1 = img11;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize, bytesAvailable1, bufferSize1, bytesRead1;
byte[] buffer;
byte[] buffer1;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
dialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
/// messageText.setText("Source File not exist :"+ imagepath);
}
});
return 0;
} else {
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(URLDATA);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
conn.setRequestProperty("uname", uname);
conn.setRequestProperty("email", email);
conn.setRequestProperty("address", addressdata);
conn.setRequestProperty("tro_milk", tropiw);
conn.setRequestProperty("uploaded_file", fileName);
conn.setRequestProperty("c1", c1);
conn.setRequestProperty("c2", c2);
conn.setRequestProperty("c3", c3);
conn.setRequestProperty("img_milk_spec", img11);
conn.setRequestProperty("plumeria", palumaria);
conn.setRequestProperty("img_pulmeria", img22);
conn.setRequestProperty("terms", terms);
conn.setRequestProperty("lat", String.valueOf(lat));
conn.setRequestProperty("log1", String.valueOf(log1));
conn.setRequestProperty("l3", String.valueOf(l3));
conn.setRequestProperty("l4", String.valueOf(l4));
conn.setRequestProperty("l5", String.valueOf(l5));
conn.setRequestProperty("l6", String.valueOf(l6));
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
if (serverResponseCode == 200) {
runOnUiThread(new Runnable() {
public void run() {
String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
+ " F:/wamp/wamp/www/uploads";
Toast.makeText(PublicSurvay.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
dialog.dismiss();
ex.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(PublicSurvay.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
dialog.dismiss();
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(PublicSurvay.this, "Got Exception : see logcat ", Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
}
dialog.dismiss();
} // End else block
} catch (Exception e) {
Toast.makeText(this, "Image Upload Missing", Toast.LENGTH_SHORT).show();
}
return serverResponseCode;
}
public int uploadFile1(String uname, String email, String addressdata, String tropiw, String c1, String c2, String c3, String palumaria, String terms, double lat, double log1, double l3, double l4, double l5, double l6) {
try {
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize, bytesAvailable1, bufferSize1, bytesRead1;
byte[] buffer;
byte[] buffer1;
int maxBufferSize = 1 * 1024 * 1024;
try {
// FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(URLDATA);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uname", uname);
conn.setRequestProperty("email", email);
conn.setRequestProperty("address", addressdata);
conn.setRequestProperty("tro_milk", tropiw);
conn.setRequestProperty("c1", c1);
conn.setRequestProperty("c2", c2);
conn.setRequestProperty("c3", c3);
conn.setRequestProperty("plumeria", palumaria);
conn.setRequestProperty("terms", terms);
conn.setRequestProperty("l3", String.valueOf(l3));
conn.setRequestProperty("l4", String.valueOf(l4));
conn.setRequestProperty("l5", String.valueOf(l5));
conn.setRequestProperty("l6", String.valueOf(l6));
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
} catch (MalformedURLException ex) {
dialog.dismiss();
ex.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(PublicSurvay.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
dialog.dismiss();
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
// messageText.setText("Got Exception : see logcat ");
Toast.makeText(PublicSurvay.this, "Got Exception : see logcat ", Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
}
dialog.dismiss();
} catch (Exception e) {
}
return serverResponseCode;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
img1 = cursor.getString(cursor.getColumnIndex(filePath[0]));
ExifInterface exif = null;
try {
exif = new ExifInterface(img1);
String lat = ExifInterface.TAG_GPS_LATITUDE;
String lat_data = exif.getAttribute(lat);
String log = ExifInterface.TAG_GPS_LONGITUDE;
String log_data = exif.getAttribute(log);
cursor.close();
l1 = CountData(lat_data);
l2 = CountLongitude(log_data);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
cursor.close();
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
cursor.close();
}
if (requestCode == 200 && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
img2 = cursor.getString(cursor.getColumnIndex(filePath[0]));
ExifInterface exif = null;
try {
exif = new ExifInterface(img2);
String lat = ExifInterface.TAG_GPS_LATITUDE;
String lat_data = exif.getAttribute(lat);
String log = ExifInterface.TAG_GPS_LONGITUDE;
String log_data = exif.getAttribute(log);
cursor.close();
l3 = CountData(lat_data);
l4 = CountLongitude(log_data);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
cursor.close();
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
cursor.close();
}
if (requestCode == 300 && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
img3 = cursor.getString(cursor.getColumnIndex(filePath[0]));
ExifInterface exif = null;
try {
exif = new ExifInterface(img3);
String lat = ExifInterface.TAG_GPS_LATITUDE;
String lat_data = exif.getAttribute(lat);
String log = ExifInterface.TAG_GPS_LONGITUDE;
String log_data = exif.getAttribute(log);
cursor.close();
l5 = CountData(lat_data);
l6 = CountLongitude(log_data);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
cursor.close();
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
cursor.close();
}
if (requestCode == 101 && resultCode == RESULT_OK && data != null) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();
img1 = cursor.getString(column_index_data);
ExifInterface exif = null;
try {
exif = new ExifInterface(img1);
String lat = ExifInterface.TAG_GPS_LATITUDE;
String lat_data = exif.getAttribute(lat);
String log = ExifInterface.TAG_GPS_LONGITUDE;
String log_data = exif.getAttribute(log);
cursor.close();
l1 = CountData(lat_data);
l2 = CountLongitude(log_data);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
cursor.close();
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
if (requestCode == 102 && resultCode == RESULT_OK && data != null) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();
img2 = cursor.getString(column_index_data);
ExifInterface exif = null;
try {
exif = new ExifInterface(img2);
String lat = ExifInterface.TAG_GPS_LATITUDE;
String lat_data = exif.getAttribute(lat);
String log = ExifInterface.TAG_GPS_LONGITUDE;
String log_data = exif.getAttribute(log);
cursor.close();
l3 = CountData(lat_data);
l4 = CountLongitude(log_data);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
cursor.close();
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
if (requestCode == 103 && resultCode == RESULT_OK && data != null) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();
img3 = cursor.getString(column_index_data);
ExifInterface exif = null;
try {
exif = new ExifInterface(img3);
String lat = ExifInterface.TAG_GPS_LATITUDE;
String lat_data = exif.getAttribute(lat);
String log = ExifInterface.TAG_GPS_LONGITUDE;
String log_data = exif.getAttribute(log);
cursor.close();
l5 = CountData(lat_data);
l6 = CountLongitude(log_data);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
cursor.close();
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
}
public double CountData(String str) {
String[] strArray = str.split(",");
String str1 = strArray[0];
String[] s1 = str1.split("/");
String a1 = s1[0];
String a2 = s1[1];
int a11 = Integer.parseInt(a1);
int a22 = Integer.parseInt(a2);
System.out.println(a11 + "" + a22);
}
你在调用'CountData()'在某处有一个空参数。您需要进行调试以确定地点和原因。你还应该考虑如何创建[mcve]。当然不是所有的代码都会导致你的问题。 –
看起来这个消息很明显:你试图在一个空字符串对象上分割,一些方法。由于代码示例中没有行号,因此出现的唯一一个str.split()调用在函数CountData中。你将不得不添加一些调试,看看你如何获得空字符串。 –