2014-02-06 134 views
7

當我按下按鈕(名爲「當前位置」)它應該顯示當前位置(經緯度&長)在textview(名爲「tvAddress」)。但它不是按照我的預期工作。它給我errors.Errors給出below.Can有人幫我PLZ顯示錯誤導致:java.lang.reflect.InvocationTargetException

錯誤是:

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 

    } 
} 

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> 

回答

5

您在設置視圖前打電話給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) 

把你的時間仔細閱讀堆棧跟蹤,尋找地方它指向給你寫的類,它始終是開始調查錯誤的好地方;)

+0

最後,作爲獎勵,這是一個很好的做法,檢查的谷歌Play服務的可用性,更多的信息在這裏:http://developer.android.com /training/location/retrieve-current.html – 2Dee

1

你的前兩行onCreate方法應該是這些的:

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

只是你錯誤地初始化了你的觀點。由於您只能在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);