2017-02-01 122 views
0

所以我有一個服務,我試圖顯示一個警告對話框。這似乎是不可能的,所以我決定在我的應用程序的半透明活動中展示它。不過,我收到此錯誤:在半透明的活動中顯示警告對話框

02-01 10:29:18.336 3806-3806/? E/AndroidRuntime: FATAL EXCEPTION: main 
              Process: com.application.sweetiean.dummylocationupdates, PID: 3806 
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.sweetiean.dummylocationupdates/com.application.sweetiean.dummylocationupdates.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
               at android.app.ActivityThread.access$800(ActivityThread.java:144) 
               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
               at android.os.Handler.dispatchMessage(Handler.java:102) 
               at android.os.Looper.loop(Looper.java:135) 
               at android.app.ActivityThread.main(ActivityThread.java:5221) 
               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:899) 
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
               Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
               at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:355) 
               at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:324) 
               at android.support.v7.app.AppCompatDelegateImplV9.onPostCreate(AppCompatDelegateImplV9.java:172) 
               at android.support.v7.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:101) 
               at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1186) 
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280) 
               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  
               at android.app.ActivityThread.access$800(ActivityThread.java:144)  
               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  
               at android.os.Handler.dispatchMessage(Handler.java:102)  
               at android.os.Looper.loop(Looper.java:135)  
               at android.app.ActivityThread.main(ActivityThread.java:5221)  
               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:899)  
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)  

這是我的主要活動類:

package com.application.sweetiean.dummylocationupdates; 

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.LocationManager; 
import android.provider.Settings; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.view.ContextThemeWrapper; 
import android.view.WindowManager; 
import android.widget.Toast; 

import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationServices; 

import static com.application.sweetiean.dummylocationupdates.R.style.dialog; 

public class MainActivity extends AppCompatActivity { 
    LocationManager locationManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_main); 

    Toast.makeText(this, "Background Services starting...", Toast.LENGTH_SHORT).show(); 
    // use this to start and trigger a service 
    Intent i= new Intent(this, LocationUpdateService.class); 
    /*this.startService(i); 
    finish();*/ 

    // potentially add data to the intent 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

    // check if enabled and if not send user to the GSP settings 
    if (!enabled) { 
     showSettingsAlert(); 
    } else { 
     this.startService(i); 
     finish(); 
    } 
    //finish(); 
} 

public void showSettingsAlert() { 
    //AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); 
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getApplication(), dialog)); 
    builder.setTitle("GPS Settings"); 
    builder.setMessage("GPS is not enabled. Do you want to enable it?"); 
    //AlertDialog alert = builder.create(); 

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent); 
     } 
    }); 

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
      //stopSelf(); 
     } 
    }); 

    AlertDialog alert = builder.create(); 

    alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 

    alert.show(); 

} 

}

我現在用的是融合位置提供API在我的服務,我要確保用戶在服務運行之前打開GPS。 我有這個在我的清單已經:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

不完全相關的問題但它會心花怒放得到答案 什麼是谷歌Play商店中的在運行位置服務整天應用的政策?由於我使用的是FusedLocationProvider API將在Play商店中拒絕我的應用程序,因爲需要GPS爲全日運行無論是在前臺或後臺或者,即使應用程序被殺害

回答

0

從日誌

無法啓動活動java.lang.IllegalStateException:您需要爲此活動使用Theme.AppCompat主題(或後代)。

嘗試Theme.AppCompat主題(或後代)

也是在這種情況下忌用融合的位置,而不是使用定位管理

的LocationManager經理=(的LocationManager)getSystemService(Context.LOCATION_SERVICE);

manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

+0

爲什麼你會建議我使用LocationManager,與FusedLocation相比有什麼優勢。我以前使用位置管理器,並切換到谷歌播放服務api。另外,我可以使用具有半透明活動的appcompat主題嗎?我只是開始得到這個錯誤,當我決定引入alertdialog –

1

做這樣的:

AlertDialog.Builder builder = new AlertDialog.Builder(
      MainActivity.this); 
    builder.setTitle("GPS Settings"); 
    builder.setMessage("GPS is not enabled. Do you want to enable it?"); 
    AlertDialog alertDialog = builder.create(); 
    alertDialog.show(); 
+0

我仍然得到相同的logcat錯誤與此。請求我使用AppCompat主題 –

+0

@SweetieAnang嘗試更新的答案。讓我知道這是否有效 –

+0

它沒有改變任何東西。我的MainActivity是一個半透明的活動 –

1

對於第一exception試試這個:

styles.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="Theme.Transparent" parent="@style/Theme.AppCompat"> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:backgroundDimEnabled">false</item> 
    </style> 

以此作爲創建這一主題:

<activity android:name=".MainActivity" android:theme="@style/Theme.Transparent"> 
</activity> 

對於第二個查詢我不認爲這會爲UR谷歌應用程序中發揮的問題..

,但它是最好的做法,停止所有後臺服務時,應用程序被破壞

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    mGoogleApiClient.disconnect(); 
} 
+0

使用Theme.AppCompat擊敗了我維護半透明主題的目的:(這就是爲什麼我想修復代碼以使alertDialog能夠顯示而忽略l​​ogcat建議。至於我的第二個查詢,感謝您的輸入。關於onDestroy,我應該斷開我的服務吧? –

+0

這裏是一個很好的例子:http://www.androidwarriors.com/2015/10/fused-location-provider-in-android.html – rafsanahmad007

+0

Hahahaha這是確切的教程I使用,因爲它是我第一次使用FusedLocationProvider API !!!這是一個偉大的,幫助我更好地理解它比谷歌文檔更好 –