好吧,我一直在寻找答案但仍然没有结果,所以我必须回答这个问题。事实上,我正在将AppCompat设置为继承FloatingActionButton的自定义类的上下文,但仍然存在错误:
You need to use a Theme.AppCompat theme (or descendant) with the design library.
这是我正在创建浮动按钮的代码片段:
Heap properties = new Heap(part);
context.setTheme(R.style.AppTheme);
SocialNetwork socialBtn = new SocialNetwork(context);
socialBtn.setAlpha(0.8f);
socialBtn.setScaleX(0.85f);
socialBtn.setScaleY(0.85f);
socialBtn.setClickable(false);
socialBtn.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FAFAFA")));
InputStream is;
try {
is = context.getAssets().open("social_icons/" + properties.get("icon"));
Bitmap icon = BitmapFactory.decodeStream(is);
socialBtn.setImageBitmap(icon);
socialBtn.setScaleType(ImageView.ScaleType.CENTER);
} catch (IOException e) {
e.printStackTrace();
}
RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
socialBtnsContainer.addView(socialBtn, btnParams);
这是我的遗产类代码:
import android.content.Context;
import android.support.design.widget.FloatingActionButton;
public class SocialNetwork extends FloatingActionButton {
public SocialNetwork(Context context) {
super(context);
}
}
这是错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{co.cardseed.cardseed/co.cardseed.cardseed.activities.CardseedActivity}: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.access$700(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:159)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:153)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:149)
at co.cardseed.cardseed.components.type.SocialNetwork.<init>(SocialNetwork.java:16)
at co.cardseed.cardseed.components.type.Type$1.Run(Type.java:178)
at co.cardseed.cardseed.components.heap.Heap.execute(Heap.java:246)
at co.cardseed.cardseed.components.heap.Heap.forEach(Heap.java:238)
at co.cardseed.cardseed.components.type.Type.buildType(Type.java:173)
at co.cardseed.cardseed.app.configs.Card$1.Run(Card.java:164)
at co.cardseed.cardseed.components.heap.Heap.execute(Heap.java:246)
at co.cardseed.cardseed.components.heap.Heap.forEach(Heap.java:238)
at co.cardseed.cardseed.components.heap.Heap.forEach(Heap.java:239)
at co.cardseed.cardseed.components.heap.Heap.forEach(Heap.java:239)
at co.cardseed.cardseed.components.heap.Heap.forEach(Heap.java:239)
at co.cardseed.cardseed.components.heap.Heap.forEach(Heap.java:239)
at co.cardseed.cardseed.app.configs.Card.buildCard(Card.java:137)
at co.cardseed.cardseed.models.beans.User.<init>(User.java:55)
at co.cardseed.cardseed.app.App.setMainActivity(App.java:170)
at co.cardseed.cardseed.activities.CardseedActivity.onCreate(CardseedActivity.java:50)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.access$700(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
我的gradle配置:
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "co.cardseed.cardseed"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
compile 'com.android.support:appcompat-v7:25.3.1'