我刚学习android编程来显示地图。 但是当我运行此代码时,会出现一些错误。谁能解释为什么会出现这种错误?我能做些什么来解决这个问题?非常感谢你。
堆栈跟踪:
12-01 02:58:27.324: E/AndroidRuntime(1360): FATAL EXCEPTION: main
12-01 02:58:27.324: E/AndroidRuntime(1360): java.lang.NullPointerException
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.app.Activity.startActivityForResult(Activity.java:3390)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.app.Activity.startActivityForResult(Activity.java:3351)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.app.Activity.startActivity(Activity.java:3587)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.app.Activity.startActivity(Activity.java:3555)
12-01 02:58:27.324: E/AndroidRuntime(1360): at com.google.android.gms.dynamic.a$5.onClick(Unknown Source)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.view.View.performClick(View.java:4240)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.view.View$PerformClick.run(View.java:17721)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.os.Handler.handleCallback(Handler.java:730)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.os.Looper.loop(Looper.java:137)
12-01 02:58:27.324: E/AndroidRuntime(1360): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-01 02:58:27.324: E/AndroidRuntime(1360): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 02:58:27.324: E/AndroidRuntime(1360): at java.lang.reflect.Method.invoke(Method.java:525)
12-01 02:58:27.324: E/AndroidRuntime(1360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-01 02:58:27.324: E/AndroidRuntime(1360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-01 02:58:27.324: E/AndroidRuntime(1360): at dalvik.system.NativeStart.main(Native Method)
12-01 02:58:29.088: I/Process(1360): Sending signal. PID: 1360 SIG: 9
MainActivity.java
package unai.skripsi.mymaps;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
static final LatLng RAGUNAN = new LatLng(-6.3039, 106.8267);
static final LatLng TAMANMINI = new LatLng(-6.29436, 106.8859);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map= ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (map!=null){
Marker ragunan = map.addMarker(new MarkerOptions().position(RAGUNAN)
.title("Ragunan"));
Marker tamanmini = map.addMarker(new MarkerOptions().position(TAMANMINI)
.title("Taman mini")
.snippet("Taman mini itu indah")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(RAGUNAN, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
布局
<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"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="unai.skripsi.mymaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="unai.skripsi.mymaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="unai.skripsi.mymaps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="unai.skripsi.mymaps.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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" />
</application>
</manifest>
答案 0 :(得分:4)
在你的xml文件中改变它
class="com.google.android.gms.maps.MapFragment"
到 android:name="com.google.android.gms.maps.SupportMapFragment"
将您的活动扩展到FragmentActivty并更改
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap(); to
map= ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
同时检查
if (map== null)
{
map= ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();}
希望这有帮助。
答案 1 :(得分:1)
确保以下步骤正确无误:
步骤:
android.library.reference.1=google-play-services_lib
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
好的,现在您已准备好使用适用于Android的Google Map API V2创建自己的Google地图应用。
如果使用min SDK = 8创建应用程序,请使用android支持库v4 + SupportMapFragment而不是MapFragment。
请参阅此处,只需使用清单文件中的密钥更改api密钥,然后按照以下步骤操作: 并且确保使用android清单文件中提到的包名称生成api密钥,并且google_play_services_lib项目应仅存在于项目的工作空间中。
清单文件:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.geeklabs.map.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="replace with your API key"/>
</application>
</manifest>
MainActivity.java:
package com.geeklabs.map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
得到这个后告诉我。
答案 2 :(得分:1)
如果您的模拟器或设备未安装Google Play服务,则可能会出现上述问题。我没有完美的解决方案,但我想出了一些解决方法来保存你的应用程序崩溃。在这种情况下,您必须遵循简单的步骤 1.覆盖startActivityForResult(intent,requestcode) 2.在startActivityForResult中,在try catch中添加超级调用super.startActivityForResult并捕获NullPointerException
轻松完成现在你可以在这里捕获nullpointerexception并在catch
中添加你的错误处理答案 3 :(得分:0)
如果您在设备中测试此程序,您的程序将适用于17级或更高级别,从2.3.3使用尝试将您的代码设置为Siddharth Vyas 建议:切勿向每个人显示您的地图密钥。