我试图在我的Android应用程序上实现街景,并遵循文档和Google API说明...它符合罚款并且没有问题但是当我尝试在我的Android应用程序上运行它时由于无法运行而崩溃执行这一行
streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
我的Google Play服务,权限和API密钥都是正确的,因为我事先制作了一张地图,只是评论出来尝试这一点,但它发送似乎有效并且无法弄清楚为什么
public class Maps extends Activity implements OnStreetViewPanoramaReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
/*GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapping)).getMap();
LatLngBounds bounds = new LatLngBounds(
new LatLng(54.001615, -2.794561), // South west corner
new LatLng(54.001615, -2.782674)); // North east corner
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(54.00908, -2.787565), 14));
map.addMarker(new MarkerOptions()
.title("Lancaster Campus")
.snippet("LUCM")
.position(new LatLng(40.716216, -74.213393)));
CameraPosition INIT =
new CameraPosition.Builder()
.target(new LatLng(54.00908, -2.787565))
.zoom(25.5F)
.bearing(300F) // orientation
.tilt(50F) // viewing angle
.build();
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setTrafficEnabled(true);
map.setBuildingsEnabled(true);*/
StreetViewPanoramaFragment streetViewPanoramaFragment =
((StreetViewPanoramaFragment) getFragmentManager()
.findFragmentById(R.id.streetviewpanorama));
streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
}
public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
panorama.setPosition(new LatLng(-33.87365, 151.20689));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_maps, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
片段
<fragment
android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"
android:id="@+id/streetviewpanorama"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
活动xml链接到类
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapping"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
清单元数据
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="*my api key*" />
错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.StreetViewPanoramaFragment.getStreetViewPanoramaAsync(com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback)' on a null object reference
at harman.com.lufr.Maps.onCreate(Maps.java:68)
答案 0 :(得分:0)
streetViewPanoramaFragment
为空。
您确定已将片段添加到xml并且您的ID与R.id.streetviewpanorama
匹配吗?
<fragment
android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"
android:id="@+id/streetviewpanorama"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
修改强>
您的活动xml中缺少 StreetViewPanoramaFragment
。
答案 1 :(得分:0)
这对我有用。
streetViewPanoramaFragment.getStreetViewPanoramaAsync(
new OnStreetViewPanoramaReadyCallback() {
@Override
public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
// Do what you want to do here.
}
}
);