3

I'm rewriting a project of mine to incorporate fragments for use on tablets. I also decided to start using Android Studio with the conversion. Pretty straight forward but I've run into an issue on the pretty simple task of setting a custom font.

I started with these: Custom fonts in android and List of files in assets folder and its subfolders and this Set custom font for Android fragments

In Eclipse my class extended Activity and I did the following in the OnCreate with no problems.(Having a directory and file "assets/FUT_R.ttf")

TextView tv=(TextView)findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getAssets(), "FUT_R.ttf"); 
tv.setTypeface(font);

now trying to convert this Activity to an ActionBarActivity (fragment with V7 support library) and I've changed the above code to this. (where view is the inflated layout with one Framelayout for phones)

TextView tv=(TextView) view.findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "FUT_R.ttf"); 
tv.setTypeface(font);

and it crashes at run time with the lovely java.lang.RuntimeException: native typeface cannot be madeeven though i created and 'assets' directory on my new project with FUT_R.ttf in its root.

To confirm the asset manager I tried this code:

String[] f = null;
                try {
                    f = getActivity().getAssets().list("");
                }
                catch (IOException e){
                    e.printStackTrace();
                }
                for(String f1:f){
                    Log.i("names",f1);
                }

And got the following output:

07-26 07:40:40.134    2114-2114/com.myapp I/names: images
07-26 07:40:40.134    2114-2114/com.myapp I/names: sounds
07-26 07:40:40.134    2114-2114/com.myapp I/names: webkit

I'm confused because no where in my 'assets' directory or project do I have these files and/or directories. Obviously the error is the system can't find my font file. WHAT AM I DOING WRONG? Any direction would be greatly appreciated, I've wasted far too much time on this stupid problem.

Thanks!

4

1 回答 1

13

我终于偶然发现了这个:Load a simple text file in Android Studio

这指出 Android Studio 中的资产文件夹显然已更改。它需要在 yourapp/src/main/assets 下创建,而不是它在根应用程序文件夹中的正常位置(与 libs、src 等相同的级别)。希望这对其他人有帮助。我在上面浪费了很多时间!

于 2013-07-26T22:57:14.560 回答