日志文件:
08-18 20:27:09.762: E/AndroidRuntime(11726): FATAL EXCEPTION: AsyncTask #2
08-18 20:27:09.762: E/AndroidRuntime(11726): Process: com.example.pr, PID: 11726
08-18 20:27:09.762: E/AndroidRuntime(11726): java.lang.RuntimeException: An error occured while executing doInBackground()
08-18 20:27:09.762: E/AndroidRuntime(11726): at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-18 20:27:09.762: E/AndroidRuntime(11726): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-18 20:27:09.762: E/AndroidRuntime(11726): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-18 20:27:09.762: E/AndroidRuntime(11726): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-18 20:27:09.762: E/AndroidRuntime(11726): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-18 20:27:09.762: E/AndroidRuntime(11726): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-18 20:27:09.762: E/AndroidRuntime(11726): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-18 20:27:09.762: E/AndroidRuntime(11726): at java.lang.Thread.run(Thread.java:841)
08-18 20:27:09.762: E/AndroidRuntime(11726): Caused by: java.lang.NullPointerException
08-18 20:27:09.762: E/AndroidRuntime(11726): at com.example.pr.MainActivity$UploadImage.doInBackground(MainActivity.java:137)
08-18 20:27:09.762: E/AndroidRuntime(11726): at com.example.pr.MainActivity$UploadImage.doInBackground(MainActivity.java:1)
08-18 20:27:09.762: E/AndroidRuntime(11726): at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-18 20:27:09.762: E/AndroidRuntime(11726): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-18 20:27:09.762: E/AndroidRuntime(11726): ... 4 more
当我尝试点击上传按钮时,应用程序崩溃了。不知道发生了什么.Below是我的MainAcyivity.java
php文件中还有一个错误,显示file_put_contents
的警告。这与此有关吗?
在onClick(VIew v)
方法中,我创建了UploadImage
的构造函数为 - new UploadImage(image,text1.getText().toString()).execute()
public class MainActivity extends Activity implements View.OnClickListener{
private static final int RESULT_IMAGE = 1;
private static final String SERVER_ADDRESS = "http://swapnil1.host22.com/";
ImageView image1,image2;
EditText text1,text2;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);
text1 = (EditText)findViewById(R.id.text1);
text2 = (EditText)findViewById(R.id.text2);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
image1.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.image1:
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,RESULT_IMAGE);
Toast.makeText(getApplicationContext(),"Selecting Image",Toast.LENGTH_LONG).show();
break;
case R.id.button1:
Bitmap image = ((BitmapDrawable)image1.getDrawable()).getBitmap();
new UploadImage(image,text1.getText().toString());
UploadImage UI = new UploadImage();
UI.execute();
break;
case R.id.button2:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_IMAGE && resultCode == RESULT_OK && data != null)
{
Uri selectedImage = data.getData();
image1.setImageURI(selectedImage);
Toast.makeText(getApplicationContext(),"Image Selected",Toast.LENGTH_LONG).show();
}
}
private class UploadImage extends AsyncTask<Void, Void, Void>
{
Bitmap image;
String name;
public UploadImage(Bitmap image, String name)
{
this.image = image;
this.name = name;
}
public UploadImage() {
// TODO Auto-generated constructor stub
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();
dataToSend.add(new BasicNameValuePair("name",name));
dataToSend.add(new BasicNameValuePair("image",encodedImage));
HttpParams httpRequestParams = getHttpRequestParams();
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "upload.php");
try
{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
private HttpParams getHttpRequestParams()
{
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,1000 * 30);
HttpConnectionParams.setSoTimeout(httpRequestParams,1000 * 30);
return httpRequestParams;
}
}
答案 0 :(得分:0)
您正在使用不带参数的构造函数:
UploadImage UI=new UploadImage();
哪个没有设置
Bitmap image;
字段。
这就是你在线上获得NullPointerException的原因:
image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
答案 1 :(得分:0)
请更改
new UploadImage(image,text1.getText().toString());
UploadImage UI=new UploadImage();
UI.execute();
到
UploadImage UI=new UploadImage(image,text1.getText().toString());
UI.execute();