当我preSS按钮(名为当前位置),它应该显示当前位置(经纬度和放大器;长)上的TextView(名为tvAddress)。但它不工作,我期望的那样。它给了我errors.Errors给出below.Can有人帮我PLZ
错误是:
02-05 23:37:22.429:E / AndroidRuntime(20293):致命异常:主要02-05 23:37:22.429:E / AndroidRuntime(20293):java.lang.IllegalStateException:无法执行活动的方法02-05 23:37:22.429:E / AndroidRuntime(20293):在android.view.View $ 1.onClick(View.java:3680)02-05 23:37:22.429:E / AndroidRuntime(20293):在android.view.View.performClick(View.java:4191)02-05 23:37:22.429:E / AndroidRuntime(20293):在android.view.View $ PerformClick.run(View.java:17229)02-05 23:37:22.429:E / AndroidRuntime(20293):在android.os.Handler.handleCallback(Handler.java:615)02-05 23:37:22.429:E / AndroidRuntime(20293):在android.os.Handler.dispatchMessage(Handler.java:92)02-05 23:37:22.429:E / AndroidRuntime(20293):在android.os.Looper.loop(Looper.java:137)02-05 23:37:22.429:E / AndroidRuntime(20293):在android.app.ActivityThread.main(ActivityThread.java:4960)02-05 23:37:22.429:E / AndroidRuntime(20293):在java.lang.reflect.Method.invokeNative(本机方法)02-05 23:37:22.429:E / AndroidRuntime(20293):在java.lang.reflect.Method.invoke(Method.java:511)02-05 23:37:22.429:E / AndroidRuntime(20293):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038)02-05 23:37:22.429:E / AndroidRuntime(20293):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)02-05 23:37:22.429:E / AndroidRuntime(20293):在dalvik.system.NativeStart.main(本机方法)02-05 23:37:22.429:E / AndroidRuntime(20293):由:java.lang.reflect.InvocationTargetException02-05 23:37:22.429:E / AndroidRuntime(20293):在java.lang.reflect.Method.invokeNative(本机方法)02-05 23:37:22.429:E / AndroidRuntime(20293):在java.lang.reflect.Method.invoke(Method.java:511)02-05 23:37:22.429:E / AndroidRuntime(20293):在android.view.View $ 1.onClick(View.java:3675)02-05 23:37:22.429:E / AndroidRuntime(20293):... 11个02-05 23:37:22.429:E / AndroidRuntime(20293):致:显示java.lang.NullPointerException02-05 23:37:22.429:E / AndroidRuntime(20293):在com.mamun.tasktest.MapActivity.marker(MapActivity.java:129)02-05 23:37:22.429:E / AndroidRuntime(20293):... 14个MapActivity.java
包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.Geo codeR;进口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.gmsmon.ConnectionResult;进口com.google.android.gmsmon.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的{ 私人的LocationManager经理; 私人TextView的tvAddress; 私人按钮btncurrent; 私人LocationClient locationClient; 私人GoogleMap的GoogleMap的; 私人MapFragment mapFragment; @覆盖 保护无效的onCreate(捆绑savedInstanceState){ 经理=(的LocationManager)getSystemService(LOCATION_SERVICE); tvAddress =(的TextView)findViewById(R.id.tvaddress); btncurrent =(按钮)findViewById(R.id.btncurrent); super.onCreate(savedInstanceState); 的setContentView(R.layout.map); locationClient =新LocationClient(这,这,这); } 公共无效标志(视图V) { 位置currentLocation = locationClient.getLastLocation(); tvAddress.setText(currentLocation.getLatitude()+,+ currentLocation.getLongitude()); Toast.LENGTH_LONG).show(); } 地理codeR地理codeR =新的地缘codeR(本); 尝试{ ArrayList的<地址>地址=(ArrayList的<地址>)地理codeR .getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(),5); 地址地址= addresses.get(0); }赶上(IOException异常五){ // TODO自动生成catch块 e.printStackTrace(); } } @覆盖 保护无效onResume(){ // TODO自动生成方法存根 super.onResume(); locationClient.connect(); } @覆盖 保护无效的onPause(){ // TODO自动生成方法存根 super.onPause(); locationClient.disconnect(); } @覆盖 公共无效onLocationChanged(地点){ // TODO自动生成方法存根 } @覆盖 公共无效onConnectionFailed(ConnectionResult结果){ // TODO自动生成方法存根 } @覆盖 公共无效onConnected(捆绑connectionHint){ } @覆盖 公共无效onDisconnected(){ // TODO自动生成方法存根 }}map.xml
<?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=http://schemas.android/apk/res/android 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:方向=垂直> <的LinearLayout 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:方向=垂直> <的EditText 机器人:ID =@ + ID / etSearch 机器人:layout_width =match_parent 机器人:layout_height =WRAP_CONTENT 机器人:EMS =10 机器人:提示=您的位置> < requestFocus的/> < /&的EditText GT; <的TextView 机器人:ID =@ + ID / tvaddress 机器人:layout_width =match_parent 机器人:layout_height =WRAP_CONTENT 机器人:文字=/> <的LinearLayout 机器人:layout_width =match_parent 机器人:layout_height =WRAP_CONTENT 机器人:方向=横向> <按钮 机器人:ID =@ + ID / btncurrent 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 安卓的onClick =标记 机器人:文字=当前位置 机器人:layout_marginLeft =98dp/> <按钮 机器人:ID =@ + ID / btnsave 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:文本=保存/> < / LinearLayout中> < / LinearLayout中> <的FrameLayout 机器人:ID =@ + ID / map_container 机器人:layout_width =match_parent 机器人:layout_height =match_parent> <片段 机器人:ID =@ + ID /图 机器人:layout_width =match_parent 机器人:layout_height =match_parent 类=com.google.android.gms.maps.MapFragment/> < /&的FrameLayout GT;< / LinearLayout中>解决方案
您在呼唤 findViewById 设置查看过,所以当你调用 tvAddress .setText , tvAddress 为空。启动这个code来代替:
@覆盖保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.map); 经理=(的LocationManager)getSystemService(LOCATION_SERVICE); tvAddress =(的TextView)findViewById(R.id.tvaddress); btncurrent =(按钮)findViewById(R.id.btncurrent); locationClient =新LocationClient(这,这,这);}另外,你得到了错误错了,真正的原因是这样的:
产生的原因:显示java.lang.NullPointerException 在com.mamun.tasktest.MapActivity.marker(MapActivity.java:129)把你的时间仔细阅读堆栈跟踪,寻找它指向你写的类的地方,它始终是开始调查错误的好地方;)
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
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$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 moreMapActivity.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.gmsmon.ConnectionResult; import com.google.android.gmsmon.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()); Toast.LENGTH_LONG).show(); } 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="schemas.android/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>解决方案
You are calling findViewById before setting the View, so when you call tvAddress.setText, tvAddress is null. Start with this code instead :
@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 ;)
更多推荐
示值误差产生的原因:java.lang.reflect.InvocationTargetException
发布评论