运行字体时更改字体以更改字体系列

时间:2014-12-09 20:09:37

标签: 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文件中定义的所有内容......

0 个答案:

没有答案