2013-04-02 135 views
1

嗨,大家好我一直嘗試實施以下到我的應用程序。 這表明在谷歌地圖上的位置,並具有以下教程,但每次我跑我的應用程序的這部分時間崩潰幫助將是非常讚賞 一個頁面謝謝谷歌地圖APIv2實現

它總是崩潰時,我的設備我上運​​行嘗試了這麼多的東西,但唉,我是一個初學者在Android 地方部分,它只會告訴你你在地圖上的位置,你的協調似乎根本不適合我。

這裏是日誌貓。

登錄貓

04-02 03:45:19.025: W/dalvikvm(1102): threadid=1: thread exiting with uncaught exception  (group=0x40a71930) 
04-02 03:45:19.104: E/AndroidRuntime(1102): FATAL EXCEPTION: main 
04-02 03:45:19.104: E/AndroidRuntime(1102): java.lang.RuntimeException: Unable to start activityComponentInfo{com.hangoverhelper/com.hangoverhelper.Place}: java.lang.NullPointerException 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.os.Looper.loop(Looper.java:137) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at dalvik.system.NativeStart.main(Native Method) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  Caused by: java.lang.NullPointerException 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at com.hangoverhelper.Place.onCreate(Place.java:42) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.app.Activity.performCreate(Activity.java:5104) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
04-02 03:45:19.104: E/AndroidRuntime(1102):  ... 11 more 

Place.java

package com.hangoverhelper; 
import com.hangoverhelper.R; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.LocationSource; 
import com.google.android.gms.maps.MapFragment; 

import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.FragmentManager; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Place extends Activity 
     implements LocationSource, LocationListener{ 

    final int RQS_GooglePlayServices = 1; 
    private GoogleMap myMap; 
    TextView tvLocInfo; 

    LocationManager myLocationManager = null; 
    OnLocationChangedListener myLocationListener = null; 
    Criteria myCriteria; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_place); 
     tvLocInfo = (TextView)findViewById(R.id.locinfo); 

     FragmentManager myFragmentManager = getFragmentManager(); 
     MapFragment myMapFragment 
      = (MapFragment)myFragmentManager.findFragmentById(R.id.map); 
     myMap = myMapFragment.getMap(); 

     myMap.setMyLocationEnabled(true); 

     myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 

     myCriteria = new Criteria(); 
     myCriteria.setAccuracy(Criteria.ACCURACY_FINE); 
     myLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.place, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int itemId = item.getItemId(); 
     if (itemId == R.id.menu_legalnotices) { 
      String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
        getApplicationContext()); 
      AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(Place.this); 
      LicenseDialog.setTitle("Legal Notices"); 
      LicenseDialog.setMessage(LicenseInfo); 
      LicenseDialog.show(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 

     if (resultCode == ConnectionResult.SUCCESS){ 
      Toast.makeText(getApplicationContext(), 
        "isGooglePlayServicesAvailable SUCCESS", 
        Toast.LENGTH_LONG).show(); 

      //Register for location updates using a Criteria, and a callback on the specified looper thread. 
      myLocationManager.requestLocationUpdates(
        0L,    //minTime 
        0.0f,   //minDistance 
        myCriteria,  //criteria 
        this,   //listener 
        null);   //looper 

      //Replaces the location source of the my-location layer. 
      myMap.setLocationSource(this); 

     }else{ 
      GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);  
     } 

    } 

    @Override 
    protected void onPause() { 
     myMap.setLocationSource(null); 
     myLocationManager.removeUpdates(this); 

     super.onPause(); 
    } 

    @Override 
    public void activate(OnLocationChangedListener listener) { 
     myLocationListener = listener; 
    } 

    @Override 
    public void deactivate() { 
     myLocationListener = null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     if (myLocationListener != null) { 
      myLocationListener.onLocationChanged(location); 

      double lat = location.getLatitude(); 
      double lon = location.getLongitude(); 

      tvLocInfo.setText(
        "lat: " + lat + "\n" + 
        "lon: " + lon); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     // TODO Auto-generated method stub 

    } 

} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.hangoverhelper" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="17" /> 

    <permission 
     android:name="com.hangoverhelper.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"></permission> 
    <uses-permission 
     android:name="com.hangoverhelper.permission.MAPS_RECEIVE"/> 
    <uses-permission 
     android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <uses-permission 
     android:name="android.permission.INTERNET"/> 
    <uses-permission 
     android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission 
     android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission 
     android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:logo="@drawable/ic_launcher" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name="com.hangoverhelper.SplashActivity" 
      android:label="@string/title_activity_splash" 
      android:theme="@style/Theme.Splash" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.hangoverhelper.MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme" > 
     </activity> 
     <activity 
      android:name="com.hangoverhelper.Place" 
      android:configChanges="orientation|keyboardHidden|screenSize" 
      android:label="@string/local_title" 
      android:theme="@style/AppTheme" > 
     </activity> 
     <activity 
      android:name="com.hangoverhelper.Pill" 
      android:label="@string/pills_title" > 
     </activity> 
     <activity 
      android:name="com.hangoverhelper.Food" 
      android:label="@string/food_title" > 
     </activity> 
     <activity 
      android:name="com.hangoverhelper.Coffee" 
      android:label="@string/coffee_title" > 
     </activity> 
     <activity 
      android:name="com.hangoverhelper.Home" 
      android:label="@string/home_title" > 
     </activity> 
     <meta-data 
    android:name="com.google.android.maps.v2.API_KEY" 
    android:value="AIzaSyBkFR4cjuUf2rw8MfLBrWS8iaS2-Th5XA4"/> 
    </application> 

</manifest> 

activity_place.x毫升

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".Place" > 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.MapFragment" 
     tools:layout="@layout/custom_info_contents" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="#A0FFFFFF" > 
     <TextView 
      android:id="@+id/locinfo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/local" /> 

    </LinearLayout> 

</RelativeLayout> 
+1

崩潰更換public class Place extends Activity來自您的地方類 – VinceFR

+1

檢查母牛數量42在Place.java file.there行42 NP。 –

回答

0

您的地圖活動應擴大FragmentActivity,而不是活性的public class Place extends FragmentActivity