2017-03-02 46 views
-1

我正在致力于android studio。虽然我的设备处于Portrait模式下,它可以完美运行,但是当我旋转我的屏幕landscape模式,然后应用程序崩溃而得到下述错误无法在android中停止活动

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(); 
    } 
} 

任何帮助将不胜感激。

回答

3

尝试使用这可能会帮助你!

保持这个清单文件

<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/> 
+0

宾果它的作品感谢兄弟 – faisal1208

+0

Pleasure Bro !!! –

+0

然后,您不能使用横向模式 – MarcGV

0

你应该叫super.onStop()的最后一件事你onStop()实现:

protected void onStop() { 
    mGoogleApiClient.disconnect(); 

    super.onStop(); 
} 
+0

仍然有同样的问题 – faisal1208

1

改变这种与onConfigurationChange方法

protected void onStop() { 
super.onStop(); 
mGoogleApiClient.disconnect(); // it hit here 

} 

这个

protected void onStop() { 
mGoogleApiClient.disconnect(); // it hit here 
super.onStop(); 


} 
+0

还是同样的问题,在'mGoogleApiClient.disconnect()再次命中;' – faisal1208

0

你在做什么?

<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:

,如果你想避免横向模式做这partir德的Android 3.2(EL NIVEL德API 13) ,el“tamaño de pantalla”tambiéncambia cuando el dispositivo cambia entre la orientaciónvertical y horizo​​ntal。 POR LO短刀,SI deseas evitar洛杉矶 reinicios恩时代报德ejecucióndebido人坎比奥去方向报 宽多desarrollas第下午NIVEL德API 13 O niveles superiores(托里奥拉 SE declara连接洛杉矶atributos的minSdkVersionŸtargetSdkVersion),debes incluir EL勇气“ screenSize“ademásdel valor”orientation“。 Es decir,debes declarar android:configChanges =“orientation | screenSize”。 仙禁运,司徒aplicacióntiene科莫DESTINO EL NIVEL德API 12个 niveles superiores,TU ACTIVIDAD SIEMPRE maneja埃斯特坎比奥德 configuración陈健波对misma(埃斯特坎比奥德configuración没有reinicia 恩ACTIVIDAD,NI siquiera宽多本身ejecuta EN联合国dispositivo Android 3.2 o superior)。

正常使用:

创建谷歌API对象中的onCreate()

@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(); 
} 
+0

不能做到这一点,因为我必须得到坐标和按钮上点击api被调用。如果我移出'onCreate'方法,它会自动获得'lat'和'long',这个我不想要 – faisal1208

+0

如果你启动应用程序并返回而不点击按钮,崩溃不会发生?它似乎代码将尝试断开mGoogleApiClient,这还没有初始化 – MarcGV

+1

是的,它确实崩溃,但我不能简单地把代码从按钮点击 – faisal1208

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" ... />