显示错误引起:java.lang.reflect.InvocationTargetException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21598165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing error Caused by: java.lang.reflect.InvocationTargetException
提问by user3076959
When I press the button(named "current location") it should shows current location(lat & long) on the textview(named "tvAddress") .But it's not working as I expect. It's giving me errors.Errors are given below.Can anyone help me plz
当我按下按钮(名为“当前位置”)时,它应该在文本视图(名为“tvAddress”)上显示当前位置(纬度和经度)。但它没有按我预期的那样工作。它给了我错误。下面给出了错误。谁能帮我请
Error is :
错误是:
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.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.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
}
}
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>
采纳答案by 2Dee
You are calling findViewById
before setting the View, so when you call tvAddress.setText
, tvAddress
is null. Start with this code instead :
您findViewById
在设置 View 之前调用,因此当您调用 时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);
}
Also, you got the error wrong, the real cause is this :
另外,你弄错了,真正的原因是:
Caused by: java.lang.NullPointerException
at com.mamun.tasktest.MapActivity.marker(MapActivity.java:129)
Take your time to read the stack trace carefully and look for places where it points to classes you wrote, it is always a good place to start for investigating bugs ;)
花点时间仔细阅读堆栈跟踪并查找它指向您编写的类的位置,这始终是调查错误的好起点;)
回答by Gheo
Your first two lines in onCreate method should be these ones:
您在 onCreate 方法中的前两行应该是这些:
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
回答by GrIsHu
Just you have wrongly initialized your views. As you can only access the views after setContentView()
method and you have done that totally reverse that is why its throwing error.
只是您错误地初始化了您的视图。由于您只能在setContentView()
方法之后访问视图并且您已经完成了完全相反的操作,这就是它抛出错误的原因。
Just change your onCreate()
as below:
只需更改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);