空对象引用上的android.os.Looper android.content.Context.getMainLooper()
我正在学习使用谷歌地图,但我不断地出错。 这是本教程的链接: 注意:在真实设备上进行测试。华为TIT-U02安卓5.1 API 22 这就是我得到的错误空对象引用上的android.os.Looper android.content.Context.getMainLooper(),android,google-maps,nullpointerexception,Android,Google Maps,Nullpointerexception,我正在学习使用谷歌地图,但我不断地出错。 这是本教程的链接: 注意:在真实设备上进行测试。华为TIT-U02安卓5.1 API 22 这就是我得到的错误 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.tw_location, PID: 24183 java.lang.RuntimeException: Unable to instantiate ac
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.tw_location, PID: 24183
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.tw_location/com.example.android.tw_location.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.access$700(ActivityThread.java:183)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1485)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5668)
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:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
at android.content.ContextWrapper.getMainLooper(ContextWrapper.java:101)
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at com.example.android.tw_location.MapsActivity.<init>(MapsActivity.java:46)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.access$700(ActivityThread.java:183)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1485)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5668)
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:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
这是清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.tw_location">
<!--
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="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
GoogleAppClient初始化应该在
onCreate
方法中:
GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */,
this /* OnConnectionFailedListener */)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.setAccountName("aymanshaltut@gmail.com")
.build();
}
尝试在空对象引用上调用虚拟方法android.os.Looper
android.content.Context.getMainLooper()
是由于传递的上下文mGoogleApiClient=new GoogleApiClient.Builder(getcontext())
为空,它正试图通过上下文调用getMainLooper()
检查GoogleAppClient构造函数内部
解决方案:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (getContext() == null) {
return;
}
}
无论何时使用符号mGoogleApiClient,我都无法解析它,我猜它超出了范围?谢谢..它成功了。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (getContext() == null) {
return;
}
}