前提・実現したいこと
getResources() on a null object referenceを解決したい
bottom navigation barを押すと、このエラーが出、アプリが落ちる。
アプリでbottom navigation barを押しても落ちないようにしたい。
発生している問題・エラーメッセージ
2019-04-26 12:23:25.187 8956-8956/com.websarva.wings.android.mapsecond E/AndroidRuntime: FATAL EXCEPTION: main Process: com.websarva.wings.android.mapsecond, PID: 8956 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at com.google.android.gms.maps.model.MapStyleOptions.loadRawResourceStyle(Unknown Source) at com.websarva.wings.android.mapsecond.MapsActivity.onMapReady(MapsActivity.java:107) at com.google.android.gms.maps.zzak.zza(Unknown Source) at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source) at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source) at android.os.Binder.transact(Binder.java:499) at fv.b(:com.google.android.gms.dynamite_mapsdynamite@16089051@16.0.89 (040408-239467275):14) at com.google.android.gms.maps.internal.bd.a(:com.google.android.gms.dynamite_mapsdynamite@16089051@16.0.89 (040408-239467275):4) at com.google.maps.api.android.lib6.impl.bk.run(:com.google.android.gms.dynamite_mapsdynamite@16089051@16.0.89 (040408-239467275):4) at android.os.Handler.handleCallback(Handler.java:761) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6617) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
該当のソースコード
java
1//mapsActivity.java 2@Override 3 public void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 6 7 // Obtain the SupportMapFragment and get notified when the map is ready to be used. 8 SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map); 9 10 if (mapFragment != null) { 11 mapFragment.getMapAsync(this); 12 } 13 14 //This class automatically initializes the maps system and the view. 15 } 16 17 18 19 20 21 @Override 22 public View onCreateView(LayoutInflater inflater, ViewGroup container, 23 Bundle savedInstanceState){ 24 25 26 View view; 27 view = inflater.inflate(R.layout.activity_maps, container, 28 false); 29 30 31 32 33 return view; 34 } 35 36 @Override 37 public void onActivityCreated(Bundle savedInstanceState){ 38 super.onActivityCreated(savedInstanceState); 39 40 SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager() 41 .findFragmentById(R.id.map); 42 43 mapFragment.getMapAsync(this); 44 } 45 46 47 /** 48 * Manipulates the map once available. 49 * This callback is triggered when the map is ready to be used. 50 * This is where we can add markers or lines, add listeners or move the camera. In this case, 51 * we just add a marker near Sydney, Australia. 52 * If Google Play services is not installed on the device, the user will be prompted to install 53 * it inside the SupportMapFragment. This method will only be triggered once the user has 54 * installed Google Play services and returned to the app. 55 */ 56 @Override 57 public void onMapReady(GoogleMap googleMap) { 58 mMap = googleMap; 59 try { 60 // Customise the styling of the base map using a JSON object defined 61 // in a raw resource file. 62 boolean success = googleMap.setMapStyle( 63 MapStyleOptions.loadRawResourceStyle( 64 _parentActivity, R.raw.style_json));//ここがMapsActivity.java:107です 65 if (!success) { 66 Log.e(TAG, "Style parsing failed."); 67 } 68 } catch (Resources.NotFoundException e) { 69 Log.e(TAG, "Can't find style. Error: ", e); 70 } 71 72 73 Log.wtf(TAG, "onMapReady: is started"); 74 75 checkPermission_or_request(); 76 77 78 79 80 81 82 }
java
1public class MainActivity extends AppCompatActivity { 2 3 final Fragment fragment1 = new MapsActivity(); 4 final Fragment fragment2 = null; 5 final Fragment fragment3 = null; 6 final FragmentManager fm = getSupportFragmentManager(); 7 Fragment active = fragment1; 8 9 @Override 10 protected void onCreate(final Bundle savedInstanceState) { 11 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation); 15 16 17 BottomNavigationView.OnNavigationItemSelectedListener nav_handler = new BottomNavigationView.OnNavigationItemSelectedListener() { 18 19 int saved_state = 0; 20 @Override 21 public boolean onNavigationItemSelected(@NonNull MenuItem item) { 22 23 switch (item.getItemId()) { 24 case R.id.action_recents: 25 Toast.makeText(MainActivity.this, "Recent", Toast.LENGTH_SHORT).show(); 26 if (saved_state == 0) { 27 active = fragment1; 28 } 29 break; 30 31 case R.id.action_favorites: 32 if (saved_state == 0) { 33 active = fragment2; 34 } 35 Toast.makeText(MainActivity.this, "Favorites", Toast.LENGTH_SHORT).show(); 36 break; 37 38 case R.id.action_nearby: 39 if (saved_state == 0) { 40 active = fragment3; 41 } 42 Toast.makeText(MainActivity.this, "Nearby", Toast.LENGTH_SHORT).show(); 43 break; 44 } 45 46 47 if (savedInstanceState == null) { 48 49 saved_state = 1; 50 51 52 FragmentManager manager = getSupportFragmentManager(); 53 FragmentTransaction transaction = manager.beginTransaction(); 54 transaction.replace(R.id.frame_layout, active); 55 56 transaction.commit(); 57 58 59 60 } 61 62 return true; 63 64 } 65 }; 66 67 bottomNavigationView.setOnNavigationItemSelectedListener(nav_handler); 68 } 69 70 71} 72
xml
1<?xml version="1.0" encoding="utf-8"?> 2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MapsActivity"> 7 8 <fragment xmlns:android="http://schemas.android.com/apk/res/android" 9 xmlns:tools="http://schemas.android.com/tools" 10 android:id="@+id/map" 11 android:name="com.google.android.gms.maps.SupportMapFragment" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 tools:context=".MapsActivity" /> 15 16</FrameLayout> 17
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 8 <FrameLayout 9 android:id="@+id/frame_layout" 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:layout_above="@+id/bottom_navigation" 13 android:animateLayoutChanges="true"> 14 15 </FrameLayout> 16 17 18 19 <android.support.design.widget.BottomNavigationView 20 android:id="@+id/bottom_navigation" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:layout_alignParentBottom="true" 24 app:menu="@menu/bottom_nav_menu" 25 app:itemBackground="@color/colorPrimary" 26 app:itemIconTint="@android:color/white" 27 app:itemTextColor="@android:color/white" /> 28</RelativeLayout> 29
試したこと
getMapAsyncをonActivityCreatedに移したり、savedInstanceState==nullのときだけFragmentをセットするようにしたりしましたが、うまくいきませんでした。
補足情報(FW/ツールのバージョンなど)
回答1件
あなたの回答
tips
プレビュー