使用Android应用程序通过谷歌地图获取驾驶距离

时间:2017-04-02 07:15:45

标签: java android

我试图通过传递源和目的地的纬度和经度来获得两点之间的行车距离。

这是我的MainActivity.java

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText t1,t2,t3,t4;
    TextView v1,v2;
    Button b1;

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

        t1=(EditText)findViewById(R.id.t1);
        t2=(EditText)findViewById(R.id.t2);
        t3=(EditText)findViewById(R.id.t3);
        t4=(EditText)findViewById(R.id.t4);
        v1=(TextView)findViewById(R.id.v1);
        v2=(TextView)findViewById(R.id.v2);

        t1.getText().toString();
        t2.getText().toString();
        t3.getText().toString();
        t4.getText().toString();

        b1=(Button)findViewById(R.id.b1);

        b1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps/api/directions/xml?origin=" + t1 + "," + t2 + "&destination=" + t3 + "," + t4 + "&sensor=false&units=metric"));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addCategory(Intent.CATEGORY_LAUNCHER );
                intent.setClassName("com.google.android.apps.maps", "com.example.john.bt2");
                MainActivity.this.startActivity(intent);
            }
        });

    }
}

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.john.bt2.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_marginTop="16dp"
        android:id="@+id/t1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/t2"
        android:layout_below="@+id/t1"
        android:layout_alignStart="@+id/t1" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/t3"
        android:layout_below="@+id/t2"
        android:layout_alignStart="@+id/t2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/t4"
        android:layout_below="@+id/t3"
        android:layout_alignStart="@+id/t3" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b1"
        android:layout_below="@+id/t4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="11dp" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/v1"
        android:layout_below="@+id/b1"
        android:layout_toStartOf="@+id/b1"
        android:layout_marginTop="23dp" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:id="@+id/v2"
        android:layout_below="@+id/v1"
        android:layout_alignStart="@+id/v1" />
</RelativeLayout>

现在,当我点击按钮时,应用程序崩溃了。我知道我在代码中遗漏了一些东西。也许JSON部分...但是我不知道如何添加它。如果您解释一下您建议的代码,因为我是Android新手,这将非常有用。任何帮助都非常感谢。

这是我的logcat

04-02 07:53:34.320 24514-24514/? D/dalvikvm: Late-enabling CheckJNI
04-02 07:53:34.424 24514-24514/com.example.john.bt2 I/InstantRun: Instant Run Runtime started. Android package is com.example.john.bt2, real application class is null.
04-02 07:53:34.424 24514-24514/com.example.john.bt2 W/InstantRun: No instant run dex files added to classpath
04-02 07:53:34.448 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
04-02 07:53:34.456 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve interface method 16104: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
04-02 07:53:34.456 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
04-02 07:53:34.456 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
04-02 07:53:34.456 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
04-02 07:53:34.456 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve interface method 16106: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
04-02 07:53:34.460 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
04-02 07:53:34.460 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
04-02 07:53:34.460 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve interface method 16110: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
04-02 07:53:34.460 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
04-02 07:53:34.460 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
04-02 07:53:34.460 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve virtual method 690: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-02 07:53:34.460 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
04-02 07:53:34.464 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
04-02 07:53:34.464 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve virtual method 712: Landroid/content/res/TypedArray;.getType (I)I
04-02 07:53:34.464 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
04-02 07:53:34.628 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild
04-02 07:53:34.628 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve virtual method 16538: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
04-02 07:53:34.628 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
04-02 07:53:34.644 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList
04-02 07:53:34.644 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve virtual method 422: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
04-02 07:53:34.644 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
04-02 07:53:34.644 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
04-02 07:53:34.644 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve virtual method 653: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-02 07:53:34.644 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
04-02 07:53:34.644 24514-24514/com.example.john.bt2 I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
04-02 07:53:34.644 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve virtual method 655: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-02 07:53:34.644 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
04-02 07:53:34.652 24514-24514/com.example.john.bt2 E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
04-02 07:53:34.656 24514-24514/com.example.john.bt2 W/dalvikvm: VFY: unable to resolve instanceof 151 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
04-02 07:53:34.656 24514-24514/com.example.john.bt2 D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
04-02 07:53:35.080 24514-24514/com.example.john.bt2 D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so

                                                              [ 04-02 07:53:35.084 24514:24514 D/         ]
                                                              HostConnection::get() New Host Connection established 0xb82b9a28, tid 24514
04-02 07:53:35.124 24514-24514/com.example.john.bt2 D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so
04-02 07:53:35.124 24514-24514/com.example.john.bt2 D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so
04-02 07:53:35.180 24514-24514/com.example.john.bt2 D/dalvikvm: GC_FOR_ALLOC freed 294K, 11% free 3061K/3420K, paused 3ms, total 3ms
04-02 07:53:35.228 24514-24514/com.example.john.bt2 W/EGL_genymotion: eglSurfaceAttrib not implemented
04-02 07:53:35.228 24514-24514/com.example.john.bt2 E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
04-02 07:53:35.236 24514-24514/com.example.john.bt2 E/OpenGLRenderer: MAX_TEXTURE_SIZE: 8192
04-02 07:53:35.240 24514-24514/com.example.john.bt2 E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
04-02 07:53:35.240 24514-24514/com.example.john.bt2 E/OpenGLRenderer: MAX_TEXTURE_SIZE: 8192
04-02 07:53:35.240 24514-24514/com.example.john.bt2 D/OpenGLRenderer: Enabling debug mode 0
04-02 07:53:37.080 24514-24514/com.example.john.bt2 D/AndroidRuntime: Shutting down VM
04-02 07:53:37.080 24514-24514/com.example.john.bt2 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d43b20)
04-02 07:53:37.084 24514-24514/com.example.john.bt2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.john.bt2, PID: 24514
                                                                      android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.john.bt2/com.example.john.bt2}; have you declared this activity in your AndroidManifest.xml?
                                                                          at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
                                                                          at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
                                                                          at android.app.Activity.startActivityForResult(Activity.java:3424)
                                                                          at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                                                                          at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
                                                                          at android.app.Activity.startActivityForResult(Activity.java:3385)
                                                                          at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                          at android.app.Activity.startActivity(Activity.java:3627)
                                                                          at android.app.Activity.startActivity(Activity.java:3595)
                                                                          at com.example.john.bt2.MainActivity$1.onClick(MainActivity.java:46)
                                                                          at android.view.View.performClick(View.java:4438)
                                                                          at android.view.View$PerformClick.run(View.java:18422)
                                                                          at android.os.Handler.handleCallback(Handler.java:733)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                          at dalvik.system.NativeStart.main(Native Method)
04-02 07:53:38.960 24514-24514/com.example.john.bt2 I/Process: Sending signal. PID: 24514 SIG: 9

0 个答案:

没有答案
相关问题