【问题标题】:AsyncTask exception RuntimeExceptionAsyncTask 异常 RuntimeException
【发布时间】:2012-12-12 14:00:45
【问题描述】:

我正在尝试在 android 中使用 AsyncTask 连接到 mysql,但我遇到了并发症。我正在使用接口从 AsyncTask 返回数据,但是当我运行代码时,出现以下错误:异常 RuntimeException。非常感谢大家。

【问题讨论】:

  • 请发布完整的异常消息和堆栈跟踪
  • 在onPostExecute中注释掉//listener.onTaskCompleted(listaNegocios);是否会出现错误?
  • 我检查了日志,问题出在解析中,就在 int lat = (Integer.parseInt(latitud)*1000000),但我不知道如何解决
  • 如果添加Log.e("lat: ", lat); 和Log.e("lon: ", lon);,lat 和 lon 的值是多少
  • Log.e 我不能使用它,因为它必须是 Log.e (String, String) 的形式

标签: php android mysql android-asynctask


【解决方案1】:

在您的 JSON 解析 try/catch 中添加一个通用异常块并发布捕获的错误:

//parse json data
 try{
     JSONArray jarray = new JSONArray(result);
        for(int i=0 ; i<jarray.length() ; i++) {
        JSONObject ob = jarray.getJSONObject(i);
        String titulo = ob.getString("title");
        String cate = ob.getString("catid");
        int cat = Integer.parseInt(cate);
        String coord = ob.getString("extra_fields_search");

        String latitud = coord.substring(0, 9);
        Float lat_f = Float.parseFloat(latitud);
        int lat = lat_f *1000000;

        String longitud = coord.substring(10, 19);
        Float lon_f = Float.parseFloat(longitud);
        int lon = lon_f*1000000;

        GeoPoint point = new GeoPoint(lat, lon);
        negocioNuevo = new Negocio(titulo, cat, point);
        listaNegocios.add(negocioNuevo);
        }  
 }catch(JSONException e){
     Log.e("miError de JSON", "Error parsing JSON "+e.toString());
 }catch(Exception e){
     Log.e("miError de General", "Error parsing data "+e.toString());
 }

【讨论】:

  • 我得到这个错误:E / MyError General (1205): Error parsing data java.lang.NumberFormatException: Invalid int: "37.770783"。除了以这种方式运行并没有离开RuntimeException,但程序并没有像它应该的那样工作。
  • 很明显你的字符串有一个小数,所以试试 parseFloat(),正如我在上面更新的那样。将其乘以 1000000 应该可以将其存储为 int
  • parseFloat() 不起作用。我不知道为什么当我将它乘以 1000000 时不起作用。我好吗?
  • 还是不行,我认为问题在于将一种数据类型转换为另一种数据类型
【解决方案2】:

在连接类中尝试替换

public Conexion(OnTaskCompleted listener){
    this.listener=listener;
}

与

public void setOnResultsListener(OnTaskCompleted listener) {
        this.listener = listener;
    }

然后在MainActivity中替换:

Conexion conexion = new Conexion(this);
conexion.execute("tipo");

与:

Conexion conexion = new Conexion();
conexion.setOnResultsListener(this);
conexion.execute("tipo");

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 2023-03-23
    • 2013-09-03
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    相关资源
    最近更新 更多