我想使用以下代码更改Fragment
中的TextView字体:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
TextView txt = (TextView) v.findViewById(R.id.Zipcode);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/customfont.ttf");
txt.setTypeface(font);
return v;
}
但它强制关闭此声明txt.setTypeface(font)
:
有谁知道解决这个问题?下面是logcat:
04-27 13:02:23.825: D/ViewRootImpl(11706): pckname = com.group.factor
04-27 13:02:23.875: D/dalvikvm(11706): GC_FOR_ALLOC freed 92K, 4% free 6645K/6851K, paused 24ms
04-27 13:02:23.885: I/dalvikvm-heap(11706): Grow heap (frag case) to 7.985MB for 1495024-byte allocation
04-27 13:02:23.935: D/dalvikvm(11706): GC_CONCURRENT freed 2K, 3% free 8103K/8327K, paused 2ms+18ms
04-27 13:02:23.985: D/AndroidRuntime(11706): Shutting down VM
04-27 13:02:23.985: W/dalvikvm(11706): threadid=1: thread exiting with uncaught exception (group=0x40a4f1f8)
04-27 13:02:23.985: E/AndroidRuntime(11706): FATAL EXCEPTION: main
04-27 13:02:23.985: E/AndroidRuntime(11706): java.lang.RuntimeException: native typeface cannot be made
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.graphics.Typeface.<init>(Typeface.java:147)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.graphics.Typeface.createFromAsset(Typeface.java:121)
04-27 13:02:23.985: E/AndroidRuntime(11706): at com.group.factor.MainActivity$DummySectionFragment.onCreateView(MainActivity.java:179)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.view.View.measure(View.java:12727)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.view.View.measure(View.java:12727)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.view.View.measure(View.java:12727)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-27 13:02:23.985: E/AndroidRuntime(11706): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.view.View.measure(View.java:12727)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1089)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2467)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.os.Looper.loop(Looper.java:137)
04-27 13:02:23.985: E/AndroidRuntime(11706): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-27 13:02:23.985: E/AndroidRuntime(11706): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 13:02:23.985: E/AndroidRuntime(11706): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 13:02:23.985: E/AndroidRuntime(11706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-27 13:02:23.985: E/AndroidRuntime(11706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-27 13:02:23.985: E/AndroidRuntime(11706): at dalvik.system.NativeStart.main(Native Method)
04-27 13:02:24.065: I/dalvikvm(11706): threadid=3: reacting to signal 3
04-27 13:02:24.065: I/dalvikvm(11706): Wrote stack traces to '/data/anr/traces.txt'
04-27 13:02:24.565: I/dalvikvm(11706): threadid=3: reacting to signal 3
04-27 13:02:24.565: I/dalvikvm(11706): Wrote stack traces to '/data/anr/traces.txt'
答案 0 :(得分:1)
从这里采取:source
Android OS的这个错误可能是您遇到问题的原因:
Typeface.createFromAsset泄漏资产流
此错误报告中的解决方法也在哪里:
这里有一个解决方法,因此该方法不会采用字体路径或格式。必须将字体资源的完整路径作为参数提交。我还在try-catch块中包含对createFromAsset()的调用,以便在找不到资产时get()方法将返回null。
public class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
另外,试试这个:
Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
"blanch_caps.ttf");
I changed to:
Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
"fonts/blanch_caps.ttf");
我的文件位于assets / fonts / blanch_caps.ttf。