我正在研究android studio
。当我的设备处于Portrait
模式时,它可以完美运行,但是当我将屏幕旋转到landscape
模式时,应用程序崩溃时会出现以下错误
java.lang.RuntimeException: Unable to stop activity {com.example.accurat.myapp/com.example.accurat.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.api.GoogleApiClient.disconnect()' on a null object reference
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3845)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3899)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4103)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.api.GoogleApiClient.disconnect()' on a null object reference
at com.example.accurat.myapp.MainActivity.onStop(MainActivity.java:249)
at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1301)
at android.app.Activity.performStop(Activity.java:6414)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3840)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3899)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4103)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
并点击以下代码部分
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect(); // it hit here
}
获取坐标时调用以下方法。
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
if(mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
任何帮助都将受到高度赞赏。
答案 0 :(得分:3)
尝试使用此功能可能对您有所帮助!!
将其保留在清单文件中
<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>
答案 1 :(得分:2)
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// do nothing, just override
}
IN LAyout
<activity
android:name=".SplashScreen"
android:configChanges="orientation|keyboardHidden|screenSize" ... />
答案 2 :(得分:1)
更改此
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect(); // it hit here
}
到这个
protected void onStop() {
mGoogleApiClient.disconnect(); // it hit here
super.onStop();
}
答案 3 :(得分:0)
您应该在super.onStop()
实施中致电onStop()
作为最后一件事:
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
答案 4 :(得分:0)
您是否正在使用onConfigurationChange方法做什么?
如果您想避开横向模式,请执行此操作:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name">
来自文档
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
Advertencia:Android 3.2(el nivel de API 13),el“tamaño de pantalla“tambiéncambiacuando el dispositivo cambia entre la orientaciónverticaly horizontal。来自lo tanto,si deseas evitar los reinicios en tiempodeejecucióndebidoal cambiodeorientación cuando desarrollas para el nivel de API 13 oiveles superiores(según 债权人,债权人,债权人,债权人,债权人,债权人,债权人 包括el valor“screenSize”ademásdelvalor“orientation”。 ES decir,debes declarar android:configChanges =“orientation | screenSize”。 Sin embargo,situaplicacióntienecomo destino el nivel de API 12 o niveles superiores,tu actividad siempre maneja este cambio de configuraciónposímisma(este cambiodeconfiguraciónnoreinicia tu actividad,ni siquiera cuando se ejecuta en un dispositivo Android 3.2 o superior)。
正常使用:
在onCreate()
中构建Google api对象@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
连接onStart()
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
像这样断开连接
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}