首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google地图出现错误的搜索功能(Android Studios)

Google地图出现错误的搜索功能(Android Studios)
EN

Stack Overflow用户
提问于 2017-03-05 13:20:38
回答 1查看 107关注 0票数 0

好的,现在我正在做的是添加一个简单的应用程序来报告和定位故障的汽车。在第三个屏幕上,我让用户输入一个地址,单击搜索按钮,按钮上的一个小地图将在找到后移动到该位置。如果用户满意,他可以单击Submit按钮并进入下一个屏幕...( submit按钮还没有完成,我只是在测试地图)

我遵循了youtuber here...and给出的步骤当我测试我的应用程序时,我以为我已经想通了,我试着输入一些简单的东西,比如out...Yet out...Yet,我得到了一个致命的错误信息……

代码语言:javascript
运行
复制
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 Studios的新手,更糟糕的是,我现在也病了,不能直接思考我的头痛(来自寒冷和这个该死的应用程序).....I有一周的时间来完成这个应用程序,所以我现在很忙……

我不能完全弄清楚是什么导致了这个错误以及如何修复它……我有一种感觉,可能是我的搜索按钮的onClick,但是我不能是sure...Can,有人能帮我吗?任何帮助都将不胜感激!

下面是我的Java活动代码

代码语言:javascript
运行
复制
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));
}

这是我的地图和搜索功能的一部分。

代码语言:javascript
运行
复制
    <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-05 14:08:44

问题出在这一行:

代码语言:javascript
运行
复制
 EditText location_input = (EditText) view.findViewById(R.id.locationInputText);

此处Editext未连接到onSearch(View view)

因此,改变:

代码语言:javascript
运行
复制
     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()方法中

代码语言:javascript
运行
复制
public void onSearch(View view){
String location = location_input.getText().toString();
//rest of the code
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42604814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档