2017-11-13 7 views
-3

私の場合はアプリが常にクラッシュする原因:java.lang.NullPointerException:仮想メソッド 'com.google.android.gms.maps.model.Polyline com.google.android.gmsを呼び出そうとしています.maps.GoogleMap.addPolylineと私はコードの下に書きますが、実行しているとき、私は閉じるエラーを強制的に表示しアンドロイド - GoogleMapのNullPointerException

ActivityMain

public class MainActivity extends FragmentActivity implements OnMapReadyCallback { 
    //Google Maps 
    public GoogleMap mMap; 

    // 
    SQLHelper dbHelper; 
    Cursor cursor; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //DB 
     dbHelper = new SQLHelper(this); 
     try { 
      dbHelper.createDataBase(); 
     } 
     catch (Exception ioe) { 
      Toast.makeText(getApplicationContext(), "Gagal", Toast.LENGTH_LONG).show(); 
     } 


     //BUAT MAP 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.peta); 
     mapFragment.getMapAsync(this); 



     int simpul_awal = 0; 
     int simpul_tujuan = 3; 

     //output algoritma 
     dijkstra algo = new dijkstra(); 
     algo.jalurTerpendek(this, simpul_awal, simpul_tujuan); 
     Toast.makeText(getBaseContext(), "Jalur terdekat : "+algo.jalur_terpendek1, Toast.LENGTH_LONG).show(); 
     String[] exp = algo.jalur_terpendek1.split("->"); 

     int start = 0; 
     //Query DB; Gambar Jalurnya! 

     dbHelper = new SQLHelper(this); 
     SQLiteDatabase db = dbHelper.getReadableDatabase(); 
     for(int i = 0; i < exp.length-1; i++){ 
      ArrayList<LatLng> lat_lng = new ArrayList<LatLng>(); 
      cursor = db.rawQuery("SELECT jalur FROM graph where simpul_awal ="+exp[start]+" and simpul_tujuan ="+exp[(++start)], null); 
      cursor.moveToFirst(); 
      int idx_koord = 0; 
      String[] exp_koord = cursor.getString(0).split(","); 
      for(int j = 0; j < exp_koord.length/2; j++){ 
       lat_lng.add(new LatLng(Double.parseDouble(exp_koord[idx_koord]), Double.parseDouble(exp_koord[++idx_koord]))); 
       ++idx_koord; 
      } 
      PolylineOptions jalurBiasa = new PolylineOptions(); 
      jalurBiasa.addAll(lat_lng).width(5).color(0xff4b9efa).geodesic(true); 
      **mMap**.addPolyline(jalurBiasa); 

     } 

     //zoom ke koordinat 
     cursor = db.rawQuery("SELECT jalur FROM graph where simpul_awal ="+exp[0]+" and simpul_tujuan ="+exp[1], null); 
     cursor.moveToFirst(); 
     String[] koord = cursor.getString(0).split(","); 
     **mMap**.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(koord[0]), Double.parseDouble(koord[1])), 12)); 



    }//oncreate 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="kokoro1.com.kokoro1"> 
    <!-- 
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
     Google Maps Android API v2, but you must specify either coarse or fine 
     location permissions for the 'MyLocation' functionality. 
    --> 

    <uses-permission android:name="kokoro1.com.kokoro1.MAPS_RECEIVE"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <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-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 


    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <!-- 
      The API key for Google Maps-based APIs is defined as a string resource. 
      (See the file "res/values/google_maps_api.xml"). 
      Note that the API key is linked to the encryption key used to sign the APK. 
      You need a different API key for each encryption key, including the release key that is used to 
      sign the APK for publishing. 
      You can define the keys for the debug and release targets in src/debug/ and src/release/. 
     --> 
     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="AIzaSyAIbPBUmnZ1TF0rHkGjLN-g1GNYHsnR5Uo" /> 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

MyLogcat

D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: kokoro1.com.kokoro1, PID: 26867 
        java.lang.RuntimeException: Unable to start activity ComponentInfo{kokoro1.com.kokoro1/kokoro1.com.kokoro1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Polyline com.google.android.gms.maps.GoogleMap.addPolyline(com.google.android.gms.maps.model.PolylineOptions)' on a null object reference 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
         at android.app.ActivityThread.access$800(ActivityThread.java:156) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:211) 
         at android.app.ActivityThread.main(ActivityThread.java:5389) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Polyline com.google.android.gms.maps.GoogleMap.addPolyline(com.google.android.gms.maps.model.PolylineOptions)' on a null object reference 
         at kokoro1.com.kokoro1.MainActivity.onCreate(MainActivity.java:80) 
         at android.app.Activity.performCreate(Activity.java:5990) 
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)  
         at android.app.ActivityThread.access$800(ActivityThread.java:156)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:211)  
         at android.app.ActivityThread.main(ActivityThread.java:5389)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at java.lang.reflect.Method.invoke(Method.java:372)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)  
Application terminated. 

答えて

1

onMapReadyが呼び出された後に:)

+0

これが正しいとあなたは "MMAP" 変数を使用する必要があります。 GoogleMapオブジェクトを初期化する前に、そのオブジェクトを使用しようとしています。コードがonCreate()で実行されているときに、マップが動作する準備ができているという保証はありません。 onMapReady()コールバックで 'mMap = googleMap; 'の後に呼び出すメソッドにコードを移動してみてください。 – DMP

関連する問題