我在自定义textview类上获得了膨胀异常,该类正在正确地处理其他项目,但在新项目中有膨胀异常。
{ FATAL EXCEPTION: main.lang.RuntimeException: Unable to start activity ComponentInfo{com.westchester.ride.driver/com.westchester.ride.driver.ActivityLogin}: android.view.InflateException: Binary XML file line #23: Error inflating class com.sgtaxi.driver.widgets.FontTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.sgtaxi.driver.widgets.FontTextView
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
at android.app.Activity.setContentView(Activity.java:1916)
at com.westchester.ride.driver.ActivityLogin.onCreate(ActivityLogin.java:77)
at android.app.Activity.performCreate(Activity.java:5191)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
at android.app.Activity.setContentView(Activity.java:1916)
at com.westchester.ride.driver.ActivityLogin.onCreate(ActivityLogin.java:77)
at android.app.Activity.performCreate(Activity.java:5191)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.westchester.ride.driver.R$styleable
at com.sgtaxi.driver.widgets.FontTextView.applyAttributes(FontTextView.java:31)
at com.sgtaxi.driver.widgets.FontTextView.<init>(FontTextView.java:22)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
at android.app.Activity.setContentView(Activity.java:1916)
at com.westchester.ride.driver.ActivityLogin.onCreate(ActivityLogin.java:77)
at android.app.Activity.performCreate(Activity.java:5191)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
}
我的fonttextview类代码
{package com.sgtaxi.driver.widgets;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import com.westchester.ride.driver.R;
import com.sgtaxi.driver.utils.FontUtils;
import com.sgtaxi.driver.utils.Fonts;
public class FontTextView extends TextView {
public FontTextView(Context context) {
super(context);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyAttributes(context, attrs);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyAttributes(context, attrs);
}
private void applyAttributes(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FontTextView);
String fontName = Fonts.values() [a.getInt(R.styleable.FontTextView_fontName, 0)].getName();
a.recycle();
if (!isInEditMode() && !TextUtils.isEmpty(fontName)) {
setTypeface(FontUtils.getFonts(getContext(), fontName));
}
}
}
My Activity Oncreate方法,其中我使用setContentView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acitivity_login);
initializeViews();
setlistners();
}
这是我的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rlMainLogin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical">
<include
android:id="@+id/ivHeaderLogin"
layout="@layout/top_bar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:background="@drawable/button_login_bg"
android:orientation="vertical"
android:padding="16dp">
<com.sgtaxi.driver.widgets.FontTextView
android:id="@+id/tvEnterYourLoginAndPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/login_main_heading"
android:textColor="@color/Black"
android:textSize="18sp"
app:fontName="roboto_light.ttf" />
<com.sgtaxi.driver.widgets.FontEditText
android:id="@+id/etUserNameLogin"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:layout_below="@+id/tvEnterYourLoginAndPassword"
android:background="@drawable/field_box"
android:drawableLeft="@drawable/ic_usermail"
android:hint="@string/email"
android:drawablePadding="16dp"
android:inputType="textEmailAddress"
android:paddingLeft="16dp"
android:textSize="16sp"
app:fontName="roboto_light.ttf" />
</LinearLayout>
</LinearLayout>
这是我的属性文件
<?xml version="1.0" encoding="utf-8"?><!-- auto generated shit -->
<resources>
<declare-styleable name="FontTextView">
<attr name="fontName" />
</declare-styleable>
<attr name="fontName" format="enum">
<enum name="open_sans_regular.ttf" value="0" />
<enum name="roboto_italic.ttf" value="1" />
<enum name="roboto_light.ttf" value="2" />
<enum name="roboto_light_italic.ttf" value="3" />
<enum name="roboto_medium.ttf" value="4" />
<enum name="roboto_medium_italic.ttf" value="5" />
<enum name="roboto_regular.ttf" value="6" />
<enum name="roboto_thin.ttf" value="7" />
<enum name="roboto_thin_italic.ttf" value="8" />
<enum name="signika_regular.ttf" value="9" />
</attr>
</resources>
答案 0 :(得分:0)
有一个类关联错误android studio没有识别我的小部件。 我只是删除我的
com.sgtaxi.driver.widgets.FontTextView
在xml中再次输入以建立关联..并且它有效。