我的 android studio 项目在棒棒糖上运行良好,但在 kitkat 设备上测试时崩溃
Posted
技术标签:
【中文标题】我的 android studio 项目在棒棒糖上运行良好,但在 kitkat 设备上测试时崩溃【英文标题】:My android studio project works finely on lollipop but crashes while testing on kitkat devices 【发布时间】:2016-06-19 12:53:33 【问题描述】:。它有一个材料视图分页器,在打开具有材料视图分页器的活动时,该应用程序只是强制关闭 Kit kat 设备。
E/androidRuntime: FATAL EXCEPTION: main
Process: com.production.iedc.helo, PID: 12711
java.lang.RuntimeException: Unable to start activity ComponentInfocom.production.iedc.helo/com.production.iedc.helo.MainActivity: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
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:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.production.iedc.helo.MainActivity.onCreate(MainActivity.java:80)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Build.gradle
apply plugin: 'com.android.application'
android
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig
applicationId "com.production.iedc.helo"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
dependencies
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile ('com.github.florent37:materialviewpager:1.0.8@aar')
transitive=true
compile 'com.squareup.picasso:picasso:2.3.3'
compile 'com.github.paolorotolo:appintro:3.4.0'
compile 'com.android.support:multidex:1.0.0'
Main activity
package com.production.iedc.helo;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.media.Image;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toolbar;
import com.github.florent37.materialviewpager.MaterialViewPager;
import com.squareup.picasso.Picasso;
public class MainActivity extends ActionBarActivity
MaterialViewPager materialViewPager;
View headerLogo;
ImageView headerLogoContent;
DrawerLayout mDrawer;
ActionBarDrawerToggle mDrawerToggle;
android.support.v7.widget.Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState)
// Declare a new thread to do a preference check
Thread t = new Thread(new Runnable()
@Override
public void run()
// Initialize SharedPreferences
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
// Create a new boolean and preference and set it to true
boolean isFirstStart = getPrefs.getBoolean("firstStart", true);
// If the activity has never started before...
if (isFirstStart)
// Launch app intro
Intent i = new Intent(MainActivity.this, DefaultIntro.class);
startActivity(i);
// Make a new preferences editor
SharedPreferences.Editor e = getPrefs.edit();
// Edit preference to make it false because we don't want this to run again
e.putBoolean("firstStart", false);
// Apply changes
e.apply();
);
// Start the thread
t.start();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//4 onglets
final int tabCount = 4;
//les vues définies dans @layout/header_logo
headerLogo = findViewById(R.id.headerLogo);
headerLogoContent = (ImageView) findViewById(R.id.headerLogoContent);
//le MaterialViewPager
this.materialViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager);
toolbar = materialViewPager.getToolbar();
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (toolbar != null)
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, 0, 0);
mDrawer.setDrawerListener(mDrawerToggle);
materialViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager())
@Override
public Fragment getItem(int position)
//je créé pour chaque onglet un RecyclerViewFragment
return RecyclerViewFragment.newInstance();
@Override
public int getCount()
return 4;
//le titre à afficher pour chaque page
@Override
public CharSequence getPageTitle(int position)
switch (position % 4)
case 0:
return "ACADEMICS";
case 1:
return "NEWS FEEDS";
case 2:
return "EVENTS";
case 3:
return "MISC";
default:
return "Page " + position;
int oldItemPosition = -1;
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
super.setPrimaryItem(container, position, object);
//seulement si la page est différente
if (oldItemPosition != position)
oldItemPosition = position;
//définir la nouvelle couleur et les nouvelles images
Drawable imageUrl = null;
int color = Color.BLACK;
Drawable newDrawable = null;
switch (position)
case 0:
imageUrl = getResources().getDrawable(R.drawable.academics);
color = getResources().getColor(R.color.cyan);
newDrawable = getResources().getDrawable(R.drawable.earth);
break;
case 1:
imageUrl = getResources().getDrawable(R.drawable.newsfeeds);
color = getResources().getColor(R.color.red);
newDrawable = getResources().getDrawable(R.drawable.tennis);
break;
case 2:
imageUrl = getResources().getDrawable(R.drawable.events);
color = getResources().getColor(R.color.green);
newDrawable = getResources().getDrawable(R.drawable.evennt);
break;
case 3:
imageUrl = getResources().getDrawable(R.drawable.misc);
color = getResources().getColor(R.color.lime);
newDrawable = getResources().getDrawable(R.drawable.light);
break;
//puis modifier les images/couleurs
int fadeDuration = 400;
materialViewPager.setColor(color, fadeDuration);
materialViewPager.setImageDrawable(imageUrl, fadeDuration);
toggleLogo(newDrawable,color,fadeDuration);
);
//permet au viewPager de garder 4 pages en mémoire (à ne pas utiliser sur plus de 4 pages !)
this.materialViewPager.getViewPager().setOffscreenPageLimit(tabCount);
//relie les tabs au viewpager
this.materialViewPager.getPagerTitleStrip().setViewPager(this.materialViewPager.getViewPager());
private void toggleLogo(final Drawable newLogo, final int newColor, int duration)
//animation de disparition
final AnimatorSet animatorSetDisappear = new AnimatorSet();
animatorSetDisappear.setDuration(duration);
animatorSetDisappear.playTogether(
ObjectAnimator.ofFloat(headerLogo, "scaleX", 0),
ObjectAnimator.ofFloat(headerLogo, "scaleY", 0)
);
//animation d'apparition
final AnimatorSet animatorSetAppear = new AnimatorSet();
animatorSetAppear.setDuration(duration);
animatorSetAppear.playTogether(
ObjectAnimator.ofFloat(headerLogo, "scaleX", 1),
ObjectAnimator.ofFloat(headerLogo, "scaleY", 1)
);
//après la disparition
animatorSetDisappear.addListener(new AnimatorListenerAdapter()
@Override
public void onAnimationEnd(Animator animation)
super.onAnimationEnd(animation);
//modifie la couleur du cercle
((GradientDrawable) headerLogo.getBackground()).setColor(newColor);
//modifie l'image contenue dans le cercle
headerLogoContent.setImageDrawable(newLogo);
//démarre l'animation d'apparition
animatorSetAppear.start();
);
//démarre l'animation de disparition
animatorSetDisappear.start();
@Override
protected void onPostCreate(Bundle savedInstanceState)
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
@Override
public boolean onOptionsItemSelected(MenuItem item)
return mDrawerToggle.onOptionsItemSelected(item) ||
super.onOptionsItemSelected(item);
Activitylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_
android:layout_>
<com.github.florent37.materialviewpager.MaterialViewPager
android:id="@+id/materialViewPager"
android:layout_
android:layout_
app:viewpager_logo="@layout/header_logo"
app:viewpager_pagerTitleStrip="@layout/material_view_pager_pagertitlestrip_newstand"
app:viewpager_logoMarginTop="100dp"
app:viewpager_color="@color/colorPrimary"
app:viewpager_headerHeight="200dp"
app:viewpager_headerAlpha="0.6"
app:viewpager_hideLogoWithFade="false"
app:viewpager_hideToolbarAndTitle="true"
app:viewpager_enableToolbarElevation="true"
app:viewpager_parallaxHeaderFactor="1.5"
app:viewpager_headerAdditionalHeight="20dp"
app:viewpager_displayToolbarWhenSwipe="true"
app:viewpager_transparentToolbar="true"
app:viewpager_animatedHeaderImage="true"
/>
<include layout="@layout/drawer" />
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
我用那个主题作为基础主题
我是新手 .. 现在卡在这个问题上你能帮帮我吗
整个项目都在这里goo.gl/Ytmpw8
【参考方案1】:
你应该改变java使用的继承
ActionBarActivity
>到
活动
并保持清单中的对话框主题不变。我想你不会有问题的。
【讨论】:
if (toolbar != null) setSupportActionBar(toolbar);最终的 ActionBar actionBar = getSupportActionBar();这些代码无法正常工作以上是关于我的 android studio 项目在棒棒糖上运行良好,但在 kitkat 设备上测试时崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Android 视频解码器仅在棒棒糖上不绘制到 gles 表面