Google地图搜索功能出错(Android工作室)

时间:2017-03-05 05:20:38

标签: java android android-layout android-studio android-fragments

好的,目前我正在做的是尝试添加一个简单的应用程序来报告和查找故障车辆。在第三个屏幕上,我让用户输入一个地址,单击搜索按钮,一旦找到,该按钮上的小地图将移动到该位置。如果用户满意,他可以单击“提交”按钮并进入下一个屏幕...(提交按钮尚未完成,我只是测试地图)

我已经按照这个youtuber here给出的步骤......我认为我已经弄明白了......但是当我测试我的应用程序时,我尝试输入一些东西像纽约一样简单,我得到一个致命的错误信息......

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.greyknightdante.roadsideassistance, PID: 7950
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.view.View$DeclaredOnClickListener.onClick(View.java:4698)
                  at android.view.View.performClick(View.java:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.view.View$DeclaredOnClickListener.onClick(View.java:4693)
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22265) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at com.example.greyknightdante.roadsideassistance.MapsActivity.onSearch(MapsActivity.java:45)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.view.View$DeclaredOnClickListener.onClick(View.java:4693) 
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22265) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

我还是Android工作室的新手,最重要的是,我现在也病了,不能直接思考这个令人头疼的问题(来自感冒和这个应用程序) .....我有一个星期的时间来完成这个应用程序,所以我现在匆忙....

我无法弄清楚导致错误的原因以及如何修复错误......我感觉它可能是我的搜索按钮的onClick,但我无法确定...可以谁来帮帮我?任何帮助将不胜感激!

这是我的Java活动代码

package com.example.greyknightdante.roadsideassistance;

import android.content.Intent;

import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{

private GoogleMap mMap;
public String problem;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //obtain previous data
    Intent intent = getIntent();
    problem = intent.getStringExtra(ReportActivity.EXTRA_MESSAGE);

}

public void onSearch(View view){
    EditText location_input = (EditText) view.findViewById(R.id.locationInputText);
    String location = location_input.getText().toString();
    List<Address> addressList = null;

    if(location != null || location.equals("")) {
        Geocoder geocoder = new Geocoder(this);
        try {
            addressList = geocoder.getFromLocationName(location, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(),address.getLongitude());
        mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    }

}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

并且继承了我的地图和搜索功能部分......

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/gpsLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="What is your location?"
        android:id="@+id/locationTextView"
        android:textSize="30dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPostalAddress"
        android:ems="10"
        android:id="@+id/locationInputText"
        android:text=""
        android:hint="Enter Location..."
        android:layout_below="@+id/locationTextView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:background="@color/white"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search"
        android:id="@+id/enterGPSButton"
        android:layout_below="@+id/locationInputText"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="onSearch"/>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:layout_below="@+id/locationInputText"
        android:layout_marginTop="55dp"
        android:id="@+id/mapLayout">
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.example.greyknightdante.roadsideassistance.MapsActivity"/>

    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

 EditText location_input = (EditText) view.findViewById(R.id.locationInputText);

此处Editext未附加到onSearch(View view)

所以改变:

     EditText location_input;  //declare globally

    @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);
    location_input = (EditText)findViewById(R.id.locationInputText); //initialize here
    }

现在使用onSearch()方法

public void onSearch(View view){
String location = location_input.getText().toString();
//rest of the code
}