显示由以下原因引起的错误:java.lang.reflect.InvocationTargetException

显示由以下原因引起的错误:java.lang.reflect.InvocationTargetException,java,android,Java,Android,当我按下按钮(名为“current location”)时,它应该会在文本视图(名为“tvAddress”)上显示当前位置(lat&long)。但它没有像我预期的那样工作。它给了我错误。错误如下。有人能帮我吗 错误为: 02-05 23:37:22.429: E/AndroidRuntime(20293): FATAL EXCEPTION: main 02-05 23:37:22.429: E/AndroidRuntime(20293): java.lang.IllegalStateExcept

当我按下按钮(名为“current location”)时,它应该会在文本视图(名为“tvAddress”)上显示当前位置(lat&long)。但它没有像我预期的那样工作。它给了我错误。错误如下。有人能帮我吗

错误为:

02-05 23:37:22.429: E/AndroidRuntime(20293): FATAL EXCEPTION: main
02-05 23:37:22.429: E/AndroidRuntime(20293): java.lang.IllegalStateException: Could not execute method of the activity
02-05 23:37:22.429: E/AndroidRuntime(20293):    at android.view.View$1.onClick(View.java:3680)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at android.view.View.performClick(View.java:4191)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at android.view.View$PerformClick.run(View.java:17229)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at android.os.Handler.handleCallback(Handler.java:615)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at android.os.Looper.loop(Looper.java:137)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at android.app.ActivityThread.main(ActivityThread.java:4960)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at java.lang.reflect.Method.invokeNative(Native Method)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at java.lang.reflect.Method.invoke(Method.java:511)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at dalvik.system.NativeStart.main(Native Method)
02-05 23:37:22.429: E/AndroidRuntime(20293): Caused by: java.lang.reflect.InvocationTargetException
02-05 23:37:22.429: E/AndroidRuntime(20293):    at java.lang.reflect.Method.invokeNative(Native Method)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at java.lang.reflect.Method.invoke(Method.java:511)
02-05 23:37:22.429: E/AndroidRuntime(20293):    at android.view.View$1.onClick(View.java:3675)
02-05 23:37:22.429: E/AndroidRuntime(20293):    ... 11 more
02-05 23:37:22.429: E/AndroidRuntime(20293): Caused by: java.lang.NullPointerException
02-05 23:37:22.429: E/AndroidRuntime(20293):    at com.mamun.tasktest.MapActivity.marker(MapActivity.java:129)
02-05 23:37:22.429: E/AndroidRuntime(20293):    ... 14 more
MapActivity.java

package com.mamun.tasktest;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    private LocationManager manager;
    private TextView tvAddress;
    private Button btncurrent;
    private LocationClient locationClient;
    private GoogleMap googleMap;
    private MapFragment mapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        manager = (LocationManager) getSystemService(LOCATION_SERVICE);
        tvAddress = (TextView) findViewById(R.id.tvaddress);
        btncurrent= (Button)findViewById(R.id.btncurrent
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        locationClient = new LocationClient(this, this, this);
    }

    public void marker(View v) {

        Location currentLocation = locationClient.getLastLocation();
        tvAddress.setText(currentLocation.getLatitude()+" , "+ currentLocation.getLongitude());
        Geocoder geocoder = new Geocoder(this);
        try {
            ArrayList<Address> addresses = (ArrayList<Address>) geocoder.getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 5);
            Address addr = addresses.get(0);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        locationClient.connect();
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        locationClient.disconnect();
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle connectionHint) {

    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }
}
package com.mamun.tasktest;
导入java.io.IOException;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.provider.Settings;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.GooglePlayServicesClient;
导入com.google.android.gms.location.LocationClient;
导入com.google.android.gms.location.LocationListener;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.MapFragment;
导入com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
导入com.google.android.gms.maps.model.BitmapDescriptorFactory;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;
公共类MapActivity扩展活动实现GooglePlayServicesClient.ConnectionCallbacks、GooglePlayServicesClient.OnConnectionFailedListener、LocationListener{
私人场所经理;
私有文本视图地址;
专用按钮BTN电流;
私人场所客户场所客户;
私人谷歌地图谷歌地图;
私有MapFragment-MapFragment;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
manager=(位置管理器)getSystemService(位置服务);
tvAddress=(TextView)findViewById(R.id.tvAddress);
btncurrent=(按钮)findViewById(R.id.btncurrent
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
locationClient=新locationClient(这个,这个,这个);
}
公共空白标记(视图v){
Location currentLocation=locationClient.getLastLocation();
tvAddress.setText(currentLocation.getLatitude()+”,“+currentLocation.getLatitude());
Geocoder Geocoder=新的Geocoder(本);
试一试{
ArrayList地址=(ArrayList)geocoder.getFromLocation(currentLocation.getLatitude(),currentLocation.getLatitude(),5);
地址addr=addresses.get(0);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
@凌驾
受保护的void onResume(){
//TODO自动生成的方法存根
super.onResume();
locationClient.connect();
}
@凌驾
受保护的void onPause(){
//TODO自动生成的方法存根
super.onPause();
locationClient.disconnect();
}
@凌驾
已更改位置上的公共无效(位置){
//TODO自动生成的方法存根
}
@凌驾
连接失败的公共void(连接结果){
//TODO自动生成的方法存根
}
@凌驾
未连接的公共无效(捆绑连接提示){
}
@凌驾
公共空间已断开连接(){
//TODO自动生成的方法存根
}
}
map.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 <LinearLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:orientation="vertical" >

    <EditText
        android:id="@+id/etSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Search your location" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tvaddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btncurrent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="marker"
            android:text="Current Location"
              android:layout_marginLeft="98dp" />

        <Button
            android:id="@+id/btnsave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save" />
    </LinearLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
         <fragment
            android:id="@+id/maps"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment" />

    </FrameLayout>

</LinearLayout>

在设置视图之前,您正在调用
findViewById
,因此当您调用
tvAddress.setText
时,
tvAddress
为空。请改为从以下代码开始:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.map);

    manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    tvAddress = (TextView) findViewById(R.id.tvaddress);
    btncurrent= (Button)findViewById(R.id.btncurrent);

    locationClient = new LocationClient(this, this, this);


}
此外,您的错误,真正的原因是:

Caused by: java.lang.NullPointerException
    at com.mamun.tasktest.MapActivity.marker(MapActivity.java:129)

花点时间仔细阅读堆栈跟踪,并查找它指向您编写的类的位置,它始终是调查bug的良好起点;)

onCreate方法中的前两行应该是:

super.onCreate(savedInstanceState);
setContentView(R.layout.map);

只是您错误地初始化了视图。因为您只能在
setContentView()方法之后访问视图,而您的操作完全相反,这就是抛出错误的原因

只需更改您的
onCreate()
,如下所示:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     setContentView(R.layout.map);
   manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    tvAddress = (TextView) findViewById(R.id.tvaddress);
    btncurrent= (Button)findViewById(R.id.btncurrent);

最后,作为奖励,检查Google Play服务的可用性是一个很好的做法,更多信息如下: