sinat_35340227
Beview_
采纳率100%
2017-07-21 07:29 阅读 1.2k

我在使用openstreetmap 开发APP的时候,实现输入地名确定位置的功能时出现错误

MainActivity.java文件:

    public class MainActivity extends Activity implements OnClickListener {


      private Button plan;
    private EditText name;  
    private Button find; 
    private Geocoder geocoder;  

    private static final double lat = 39.98773;  
    private static final double lng = 116.37742;

    public MapView mapView=null;

    private ItemizedOverlayWithBubble<ExtendedOverlayItem> poiMarkers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        geocoder = new Geocoder(this);  

        name = (EditText) findViewById(R.id.name);  
        find = (Button) findViewById(R.id.find);

        plan=(Button) findViewById(R.id.plan);
        plan.setOnClickListener(this);
        find.setOnClickListener(this);
        Intent i= getIntent();
        Bundle bundle = i.getExtras();

    //      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    //      StrictMode.setThreadPolicy(policy);

        mapView = (MapView) findViewById(R.id.MapView);
        initMapOverlays(mapView);
        routePlan();
        poiSearch();




    }

其中我目前出现问题的部分是点击按钮获取输入文本实现位置确定代码如下:

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId()==R.id.plan) {
            Intent intent=new Intent(MainActivity.this,PlanActivity.class);
            startActivity(intent);
        }
        if (v.getId()==R.id.find) {
            String keyword = name.getText().toString();  

            new UpdateSearch().execute(keyword);    


            } 

    }
        /*
     * 异步_地点查询
     */
    class UpdateSearch extends AsyncTask<String, Void, List<Address>>{



        @Override
        protected List<Address> doInBackground(String... params) {
            // TODO Auto-generated method stub

            GeocoderNominatim coderNominatim = new GeocoderNominatim(MainActivity.this);
            List<Address> geoResults = null;
            try {
                geoResults = coderNominatim.getFromLocationName(params[0], 3);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return geoResults;
        }

        @Override
        protected void onPostExecute(List<Address> result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (result.size() == 0) { 
                 Toast.makeText(MainActivity.this, "Not found.", Toast.LENGTH_SHORT).show();
         } else {
                Address address = result.get(0);
                Bundle extras = address.getExtras();
                BoundingBoxE6 bb = extras.getParcelable("boundingbox");
                mapView.zoomToBoundingBox(bb);
         } 
        }

    }

在我输入地名“故宫”点击按钮之后程序闪退,部分日至如下:

 07-21 15:23:02.415: W/System(3021): ClassLoader referenced unknown path: /system/framework/com.qualcomm.qti.GBAHttpAuthentication.jar
07-21 15:23:23.520: E/AndroidRuntime(3021): FATAL EXCEPTION: AsyncTask #5
07-21 15:23:23.520: E/AndroidRuntime(3021): Process: com.example.osmdroiddemo, PID: 3021
07-21 15:23:23.520: E/AndroidRuntime(3021): java.lang.RuntimeException: An error occurred while executing doInBackground()
07-21 15:23:23.520: E/AndroidRuntime(3021):     at android.os.AsyncTask$3.done(AsyncTask.java:309)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.lang.Thread.run(Thread.java:818)
07-21 15:23:23.520: E/AndroidRuntime(3021): Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/JsonParser;
07-21 15:23:23.520: E/AndroidRuntime(3021):     at org.osmdroid.bonuspack.location.GeocoderNominatim.getFromLocationName(GeocoderNominatim.java:230)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at org.osmdroid.bonuspack.location.GeocoderNominatim.getFromLocationName(GeocoderNominatim.java:271)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at com.example.osmdroiddemo.MainActivity$UpdateSearch.doInBackground(MainActivity.java:360)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at com.example.osmdroiddemo.MainActivity$UpdateSearch.doInBackground(MainActivity.java:1)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at android.os.AsyncTask$2.call(AsyncTask.java:295)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-21 15:23:23.520: E/AndroidRuntime(3021):     ... 4 more
07-21 15:23:23.520: E/AndroidRuntime(3021): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.gson.JsonParser" on path: DexPathList[[zip file "/data/app/com.example.osmdroiddemo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.osmdroiddemo-1/lib/arm, /vendor/lib, /system/lib]]
07-21 15:23:23.520: E/AndroidRuntime(3021):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
07-21 15:23:23.520: E/AndroidRuntime(3021):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
07-21 15:23:23.520: E/AndroidRuntime(3021):     ... 10 more
07-21 15:23:23.520: E/AndroidRuntime(3021):     Suppressed: java.lang.ClassNotFoundException: com.google.gson.JsonParser
07-21 15:23:23.520: E/AndroidRuntime(3021):         at java.lang.Class.classForName(Native Method)
07-21 15:23:23.520: E/AndroidRuntime(3021):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
07-21 15:23:23.520: E/AndroidRuntime(3021):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
07-21 15:23:23.520: E/AndroidRuntime(3021):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
07-21 15:23:23.520: E/AndroidRuntime(3021):         ... 11 more
07-21 15:23:23.520: E/AndroidRuntime(3021):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

日志里提到的什么Json什么的实在是不知道咋去解决,国内关于osm开发的实战很少,所以有没有大神帮忙解决一下,有什么还需要我提供的请留言,谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    sinat_35340227 Beview_ 2017-07-25 07:09

    问题我已经自己解决了,是因为我没有导入一个包叫做gson。然后我通过搜索找到一个合适的包,链接如下:
    http://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.0/
    如何导包大家可以自行百度。

    点赞 评论 复制链接分享

相关推荐