Android致命异常AsyncTask#3

问题描述:

当我尝试向Web服务器发送一些数据时,它遇到了一个致命异常AsyncTask#3问题。Android致命异常AsyncTask#3

如果我发送的数据没有文件,错误显示并杀死应用程序,但如果我发送数据与文件(jpg,mp4)一切都很好。

这是日志:

10-25 00:31:59.274: E/AndroidRuntime(24895): FATAL EXCEPTION: AsyncTask #3 
10-25 00:31:59.274: E/AndroidRuntime(24895): java.lang.RuntimeException: An error occured while executing doInBackground() 
10-25 00:31:59.274: E/AndroidRuntime(24895): at android.os.AsyncTask$3.done(AsyncTask.java:278) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.lang.Thread.run(Thread.java:856) 
10-25 00:31:59.274: E/AndroidRuntime(24895): Caused by: java.lang.NullPointerException 
10-25 00:31:59.274: E/AndroidRuntime(24895): at com.videotrafico.ReporteActivity$asyncreport.doInBackground(ReporteActivity.java:260) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at com.videotrafico.ReporteActivity$asyncreport.doInBackground(ReporteActivity.java:1) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
10-25 00:31:59.274: E/AndroidRuntime(24895): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
10-25 00:31:59.274: E/AndroidRuntime(24895): ... 5 more 

它给出了错误的路线,这是一个:

file = new File(fileUri.getPath()); 

我如何可以覆盖应用到死,如果文件没有设置好的吗?

谢谢!

的AsyncTask:

class asyncreport extends AsyncTask< String, String, String > { 

      String rep; 
      String user; 
      protected void onPreExecute() { 
       //para el progress dialog 
       pDialog = new ProgressDialog(ReporteActivity.this); 
       pDialog.setMessage("Enviando reporte"); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 

      protected String doInBackground(String... params) { 
       //obtnemos usr y pass 
       rep=params[0]; 
       user=params[1]; 
       File file = new File(fileUri.getPath()); 
       //enviamos y recibimos y analizamos los datos en segundo plano. 
       if (loginstatus(rep, user, file)==true){       
        return "ok"; //login valido 
       }else{   
        return "err"; //login invalido      
       } 

      } 


      /*Una vez terminado doInBackground segun lo que halla ocurrido 
      pasamos a la sig. activity 
      o mostramos error*/ 
      protected void onPostExecute(String result) { 

       pDialog.dismiss();//ocultamos progess dialog. 
       Log.e("onPostExecute=",""+result); 

       if (result.equals("ok")){ 
        Intent i=new Intent(getApplicationContext(),MainActivity.class); 
        startActivity(i); 
       }else{ 
        Intent i=new Intent(getApplicationContext(),MainActivity.class); 
         startActivity(i); 
       } 

                 } 

      } 

好是在public Uri fileUri;现在设置好的问题,以public Uri fileUri = Uri.parse("");

但应用程序无论如何都不会发布的数据!

这是代码POR后:

try { 
        MultipartEntity entity = new MultipartEntity(); 
       Log.e("enviando", fileUri.getPath()); 
        entity.addPart("reporte", new StringBody(reporte)); 
        entity.addPart("usuarioID", new StringBody(user)); 
        entity.addPart("archivo", new FileBody(file)); 
        httppost.setEntity(entity); 
        HttpResponse response = httpclient.execute(httppost); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
+1

SO为什么不使用代码来检查null –

+0

没有看到你的AsyncTask,这很难调试......但是在你的doInBackground中有一个java.lang.NullPointerException,所以你可能缺少一个if/null检查。 –

+0

你的fileUri对象是空的,检查它是否被初始化。 –

您还没有初始化fileUri为一个值,你可以通过构造函数传递的URI的AsyncTask做到这一点。

 public class asyncReport extends AsyncTask<String,String,String> 
     { 
      String user,rep 
      String fileUri; 

      public asyncReport(String fileUri) 
      { 
      this.fileUri=fileUri; 
      } 

     ... 
     } 

此外,当你说<input,progress,output><String,String,String>,你必须发布使用String.I你的操作进度建议您使用<String,Integer,String> instead.In为了发布进度使用:

 //In your doInBackground 
     publishProgress(progress);//create and maintain a variable to do this 

     //in your AsyncTask 
     onProgressUpdate(int progress)  
     { 
     setProgressPrecent(progress); 
     } 

你可以还将fileUri作为参数之一传递给doInBackground。

你可以简单地做一个空检查,并与您的代码进行,我想这应该这样做。

if(fileUri!=null) 
{ 
    file = new File(fileUri.getPath()); 
    // do all relevant part here. 
} 
+0

感谢您的答案,但如果我这样做,该应用程序无论如何死于该行,如果它看起来问题是与fileUri,我怎么能设置一些东西呢?只是为了覆盖应用程序kepp死亡? –

我怀疑这是你怎么想打电话给你的异步任务:

HttpTraitement task = new HttpTraitement(this); 
task.execute("http://namedomain/test.php"); 
In order to do that, you must change your async task the following way: 

public class HttpTraitement extends AsyncTask<String, Integer, HttpResponse> { 

    private Context context; 

    public HttpTraitement (Context context) 
    { 
     this.context = context;  
    } 

    @Override 
    protected HttpResponse doInBackground(String... params) { 
     HttpResponse response = null; 
     HttpPost httppost = new HttpPost(params[0]); 

     try { 
      HttpClient client = new DefaultHttpClient(); 
      response = client.execute(httppost); 

     } catch (ClientProtocolException e) { 
      Toast.makeText(this.context, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show(); 
     } catch (IOException e) { 
      Toast.makeText(this.context, "Caught IOException", Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
      Toast.makeText(this.context, "Exception", Toast.LENGTH_SHORT).show();     
     } 

     return response; 

    } 

} 
Please note that the method execute() must be called only once for each async task object. Thus if you want to call it for more than one URL, you must do this: 

HttpTraitement task1 = new HttpTraitement(this); 
task1.execute("http://namedomain/test1.php"); 
HttpTraitement task2 = new HttpTraitement(this); 
task2.execute("http://namedomain/test2.php"); 
+0

我的天啊......你好像是未来的探测器...... –

+0

他不是在问asyncTask的例子.. –