处理我正在尝试使用Fragments的小应用程序。该应用程序从URI中提取一些JSON数据。然后它创建然后在列表中显示该数据。如果不涉及片段,所有这一切都可以正常工作。
我正在尝试将Google地图显示在我的布局中该列表下方的片段中,这就是我遇到麻烦的地方。
我的API密钥很好。我已经浏览了StackTrace,但无法弄清楚发生了什么。所以,我要求你们帮忙。此外,在建议之前,我已经多次在Android Studio中清理并重建了应用程序。
这是我的代码,我将尝试仅显示相关位。如果您需要更多,请告诉我。
CODE
MainActivity.java
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<CycloneData>> {
...
私人CycloneMap cycloneMapFrag;
private void initMapFragment() {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
cycloneMapFrag = new CycloneMap();
SupportMapFragment supportMapFragment = cycloneMapFrag;
fragmentTransaction.add(google_map, supportMapFragment);
fragmentTransaction.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cyclone_list);
...
initMapFragment();
}
}
CycloneMap.java(片段我试图添加)
public class CycloneMap extends SupportMapFragment {
//Defining Google Map objects variables
private GoogleMap googleMapView;
boolean mapReady = false;
static final CameraPosition START_POINT = CameraPosition.builder()
.target(new LatLng(38.1254, -101.1703))
.zoom(3)
.bearing(359)
.tilt(5)
.build();
private void initGoogleMap() {
getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
//Setting mapReady to true
mapReady = true;
//Loading local instance map from Callback
googleMapView = googleMap;
//Set map type to Satellite view
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
//Set camera at starting point, high over the middle of the U.S of A.
initialCameraPosition(START_POINT);
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.cyclone_list, container, false);
}
//@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initGoogleMap();
}
private void initialCameraPosition(CameraPosition target) {
//Setting position to the target created above
googleMapView.moveCamera(CameraUpdateFactory.newCameraPosition(target));
}
}
XML
cyclone_list.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Layout for a list of cyclones -->
<ListView
android:id="@+id/cyclone_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:orientation="vertical"
android:layout_weight="1"/>
<!-- Layout for Google Maps Fragment -->
<FrameLayout
android:id="@+id/google_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
...
</RelativeLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.palarran.cycloops">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_cycloops"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".MenuSettingsActivity"
android:label="@string/settings_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.palarran.cycloops.MenuSettingsActivity"/>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AHlaSyCIse5CB1QqYj9IqKzd7jRwGtIdjI48Mto"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
</manifest>
栈跟踪
CatLog说:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.palarran.cycloops, PID: 31964
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.palarran.cycloops/com.palarran.cycloops.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.impl.bq.v()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.impl.bq.v()' on a null object reference
at com.google.maps.api.android.lib6.impl.co.i(:com.google.android.gms.DynamiteModulesB:178)
at com.google.android.gms.maps.internal.w.onTransact(:com.google.android.gms.DynamiteModulesB:209)
at android.os.Binder.transact(Binder.java:499)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onStart(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zza.onStart(Unknown Source)
at com.google.android.gms.dynamic.zza$6.zzb(Unknown Source)
at com.google.android.gms.dynamic.zza.zza(Unknown Source)
at com.google.android.gms.dynamic.zza.onStart(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onStart(Unknown Source)
at android.support.v4.app.Fragment.performStart(Fragment.java:2215)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1340)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:2893)
at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:212)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:613)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6696)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
StackTrace的原因
引起:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'void com.google.maps.api.android.lib6.impl.bq.v()'
我已经浏览了StackTrace并阅读了有关NullPointerExceptions的建议文章。没有帮助。我无法找到或弄清楚NullPointer所指的是什么或如何纠正它。
修改/更新
我可能遇到了Google API错误。如果我在模拟器上运行它,它不会像以前一样崩溃。我发现错误报告和一些StackOverFlow帖子说由于USB连接到我的手机而发生问题,或者如果没有SD卡,它会发生在某些手机上。我手机上没有SD卡(Nexus 5X),没有其他手机可供测试。 :(
答案 0 :(得分:0)
我建议您设置Fragment交易,这样您就可以持有经理的实例而不是交易。
// Get an instance of your custom map fragment class
// or of "new SupportMapFragment();"
SupportMapFragment mapFragment = new MyMapFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
// Add the Map Fragment to your FrameLayout container
.replace(R.id.fragment_container, mapFragment, "MAP_FRAGMENT")
.commit();
您可能想尝试将initGoogleMap()
放入地图片段onResume()
中。