編輯:關於Android編程
#1、錯誤描述
今天,想實現Activity不顯示標題欄的效果,在項目的AndroidManifest.xml文件,對相應的Activity添加屬性
android:theme="@android:style/Theme.NoTitleBar"
具體代碼如下:
<activity android:name=".module.view.activity.KuwoMusicPlayActivity" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
結果運行app的時候,報了如下錯誤:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
具體錯誤log如下:
01-01 01:33:41.950 6637-6637/com.xtc.kuwo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xtc.kuwo, PID: 6637
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xtc.kuwo/com.xtc.kuwo.module.view.activity.KuwoMusicPlayActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:340)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.xtc.kuwo.module.view.activity.KuwoMusicPlayActivity.onCreate(KuwoMusicPlayActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)?
at android.app.ActivityThread.access$800(ActivityThread.java:151)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)?
at android.os.Handler.dispatchMessage(Handler.java:102)?
at android.os.Looper.loop(Looper.java:135)?
at android.app.ActivityThread.main(ActivityThread.java:5254)?
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:955)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)?
如下圖所示:
原來我的Activity繼承自 android.support.v7.app.AppCompatActivity,而不是 android.app.Activity。
創建的activity時,如果不是那麼強烈需要繼承自AppCompatActivity,就直接繼承Activity。
如將activity繼承自AppCompatActivity:
public class MainActivity extends ActionBarActivity
改成activity繼承自Activity:
public class MainActivity extends Activity
還是想繼承自AppCompatActivity,那麼根據提示來使用AppCompat的theme,即將AndroidManifest.xml文件中關於Activity的主題配置改成:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
完整代碼如下:
<activity android:name=".module.view.activity.KuwoMusicPlayActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
問題解決!
參考Android仿騰訊QQ實現滑動刪除這篇文章進行學習,文章實現的功能是:在ListView的Item上從右向左滑時,出現刪除按鈕,點擊刪除按鈕把Item刪除,效果看過
IntentService簡介:IntentService是一個通過Context.startService(Intent)啟動可以處理異步請求的Service,使用時你
方案一:PreLollipopTransition首先在 build.gradle 配置文件添加這個庫依賴dependencies { compile
從5.0(API等級21)開始,android開始支持矢量圖了。關於什麼是矢量圖以及矢量圖有什麼優缺點不在本文的涉及范圍之內,具體可以參考矢量圖百科。不過這裡要提一下它的