我是Android开发的新手,我正在尝试为我在iPad上运行的应用程序获取Roboto字体。目前它在logcat中给我一个错误,说这里有一个错误膨胀一个班级。
LogCat报告:
08-04 02:45:53.264: E/AndroidRuntime(2595): FATAL EXCEPTION: main
08-04 02:45:53.264: E/AndroidRuntime(2595): Process: com.example.sportsocialv2, PID: 2595
08-04 02:45:53.264: E/AndroidRuntime(2595): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sportsocialv2/sportsocialv2.activity.LoginActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class com.sportsocialv2.theme.TextView_Roboto
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.os.Handler.dispatchMessage(Handler.java:102)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.os.Looper.loop(Looper.java:136)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-04 02:45:53.264: E/AndroidRuntime(2595): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 02:45:53.264: E/AndroidRuntime(2595): at java.lang.reflect.Method.invoke(Method.java:515)
08-04 02:45:53.264: E/AndroidRuntime(2595): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-04 02:45:53.264: E/AndroidRuntime(2595): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-04 02:45:53.264: E/AndroidRuntime(2595): at dalvik.system.NativeStart.main(Native Method)
08-04 02:45:53.264: E/AndroidRuntime(2595): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class com.sportsocialv2.theme.TextView_Roboto
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.view.LayoutInflater.inflate(LayoutInflater.java:462)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-04 02:45:53.264: E/AndroidRuntime(2595): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.Activity.setContentView(Activity.java:1929)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
08-04 02:45:53.264: E/AndroidRuntime(2595): at sportsocialv2.activity.LoginActivity.onCreate(LoginActivity.java:78)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.Activity.performCreate(Activity.java:5231)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-04 02:45:53.264: E/AndroidRuntime(2595): ... 11 more
08-04 02:45:53.264: E/AndroidRuntime(2595): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sportsocialv2.theme.TextView_Roboto" on path: DexPathList[[zip file "/data/app/com.example.sportsocialv2-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.sportsocialv2-2, /vendor/lib, /system/lib]]
08-04 02:45:53.264: E/AndroidRuntime(2595): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
08-04 02:45:53.264: E/AndroidRuntime(2595): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
08-04 02:45:53.264: E/AndroidRuntime(2595): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
08-04 02:45:53.264: E/AndroidRuntime(2595): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
这是我想要使用Roboto fontstyle的活动:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="sportsocialv2.activity.LoginActivity"
>
<!-- Login progress -->
<LinearLayout
android:id="@+id/login_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="gone" >
<com.sportsocialv2.theme.TextView_Roboto
android:id="@+id/settings_cover_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginLeft="10dp"
/>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp" />
<TextView
android:id="@+id/login_status_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif-light"
android:text="@string/login_progress_signing_in"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<!-- Login form -->
<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
style="@style/LoginFormContainer"
android:orientation="vertical" >
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_username_prompt"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="16dp"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:text="@string/action_sign_in_register" />
</LinearLayout>
</ScrollView>
这是扩展TextView的相关Java类:
package sportsocialv2.theme;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class TextView_Roboto extends TextView
{
public TextView_Roboto(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
createFont();
}
public TextView_Roboto(Context context, AttributeSet attrs)
{
super(context, attrs);
createFont();
}
public TextView_Roboto(Context context)
{
super(context);
createFont();
}
public void createFont()
{
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Regular.ttf");
setTypeface(font);
}
}
我已经在SO和其他网站上搜索了有关设置Roboto的指南,但我似乎无法运行或实际显示字体。
感谢先进的任何建议。