TypeFace不起作用并给出一些例外

时间:2014-12-17 11:13:45

标签: java android

当我运行我的代码时,由于TypeFace,它会产生一些例外 我有一个微调器,我希望它在我点击其中任何一个时更改字体系列 但它不起作用。

这是我的.java代码

public class Graduation extends ActionBarActivity {
    ImageView imageView ;
    Spinner spinner;
    int fontSizeInt;
    String[]items = { "12", "16", "18", "20", "24", "28" , "30" };
    Spinner spinner1;
    int fontSizeFamily;

    String[]f_items = { "Times New Roman ", "Arial", "sans-serif", "20", "24", "28" , "30" };
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graduation);

        spinner = (Spinner)findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this, android.R.layout.simple_spinner_item, items);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(
            new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    int position = spinner.getSelectedItemPosition();

                    try
                    {
                        fontSizeInt = Integer.parseInt(items[position]);
                    }
                    catch (NumberFormatException e)
                    {
                        fontSizeInt = 12; // Default size.
                    }
                    et.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) fontSizeInt);
                    // TODO Auto-generated method stub
                }
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
                }
            }
        );

        spinner1 = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
        this, android.R.layout.simple_spinner_item, f_items);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(
            new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                          int arg2, long arg3) {
                    int position = spinner1.getSelectedItemPosition();

                    try
                    {
                        if (position == 0 )
                        { 
                            Typeface f = Typeface.createFromAsset(getBaseContext().getAssets(),"font/TimesNewRoman.ttf");
                            et.setTypeface(f);
                        }
                        if (position == 1 )
                        {  
                            Typeface f = Typeface.createFromAsset(getBaseContext().getAssets(),"font/arial.ttf");
                            et.setTypeface(f);
                        }
                    }
                    catch (NumberFormatException e)
                    {
                        // 
                    }

                    // TODO Auto-generated method stub
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
                }
            }
        );
        addListenerOnImageg1() ;
        et = (EditText) findViewById(R.id.et);
    }

    public void addListenerOnImageg1() {
        final Context context = this;
        imageView = (ImageView) findViewById(R.id.g1);
        imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, g1.class);
            startActivity(intent);
            Intent intent1 = new Intent(context , g1.class); 
            intent1.putExtra("fn" , et.getText().toString());
            intent1.putExtra("font_size", fontSizeInt);
            startActivity(intent1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的logcat

 12-09 22:04:27.670: E/AndroidRuntime(1858): FATAL EXCEPTION: main
 12-09 22:04:27.670: E/AndroidRuntime(1858): java.lang.RuntimeException: native typeface cannot be made
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.graphics.Typeface.<init>(Typeface.java:147)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.graphics.Typeface.createFromAsset(Typeface.java:121)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at com.example.invitationcard.Graduation$2.onItemSelected(Graduation.java:92)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:882)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.widget.AdapterView.access$200(AdapterView.java:48)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:848)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.os.Handler.handleCallback(Handler.java:605)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.os.Handler.dispatchMessage(Handler.java:92)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.os.Looper.loop(Looper.java:137)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at android.app.ActivityThread.main(ActivityThread.java:4424)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at java.lang.reflect.Method.invokeNative(Native Method)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at java.lang.reflect.Method.invoke(Method.java:511)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 12-09 22:04:27.670: E/AndroidRuntime(1858):    at dalvik.system.NativeStart.main(Native Method)

我不知道我的问题在哪里 我也有xml文件,没有错误..有spinner和et以及.java文件中定义的所有内容......

我也改变了我的陈述:

Typeface f = Typeface.createFromAsset(Graduation.this.getAssets(),"font/TimesNewRoman.ttf");

并且它给出了同样的错误..我该怎么办?

我的.ttf文件存在于bin / assets / font文件夹中。

我的文件扩展名是正确的。

5 个答案:

答案 0 :(得分:0)

您可以使用View的getContext()方法获取当前的Context,然后使用它来获取资源:

Typeface font = Typeface.createFromAsset(getContext().getAssets(), "font/arial.ttf");

答案 1 :(得分:0)

试试这个 -

Typeface f = Typeface.createFromAsset(getApplicationContext().getAssets(),"font/Blue Highway.ttf");

使用getApplicationContext()。这是在我的地方工作。如果它不在您的项目中,那么您的ttf文件中存在问题

答案 2 :(得分:0)

您的代码是正确的。我建议检查你的字体文件,它可能不支持android或损坏。 您可以尝试使用任何其他字体文件或只是在Android文件系统中打开.ttf文件来检查它的兼容性。

答案 3 :(得分:0)

创建给定的两个字体对象下面的setContentView方法并在spinner

中使用它

字体tf1 = Typeface.createFromAsset(getAssets(),“font / TimesNewRoman.ttf”);

字体tf2 = Typeface.createFromAsset(getAssets(),“font / arial.ttf”);

答案 4 :(得分:0)

我猜这个字体本身有问题。当操作系统中的本机代码尝试加载字体时会触发该错误,因为丢失的文件存在不同的消息,因此我认为它是找到文件但不喜欢它的原因。

尝试使用其他一些ttf文件可能会从其他目的地下载它,如果可能的话尝试使用otf扩展字体。