如何在我的Android应用程序中放置自定义字体?

时间:2014-06-14 20:01:26

标签: java android eclipse fonts android-activity

我正在尝试在我的应用中添加自定义字体,但我犯了很多错误!有人请帮帮我?我是一名学生,没有多大意义。

这是我试图放入 MainActivity.java 的代码:

TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(this.getAssets(),
    "fonts/watermelon.ttf");
tx.setTypeface(custom_font);

这是 MainActivity 的代码:

package com.akzonobel.malote;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.os.Build;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.widget.Button;
import android.widget.TextView;

@SuppressWarnings("unused")
public class MainActivity extends ActionBarActivity{


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Typefaces.get(getApplication(), "fonts/watermelon.ttf");



TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(this.getAssets(),
    "fonts/watermelon.ttf");
tx.setTypeface(custom_font);


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();}
                    }

    public void opensecondactivity(View view) {
        Intent intent = new Intent(this , SecondActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
           } 

    @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) {
        Intent intent = new Intent(this , AboutActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
        // 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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
              }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView; 
            }
          }
       }

使用此代码,Eclipse不会声明任何错误,但是当他运行时,错误和应用程序的活动单独关闭。怎么办?

调试:

New_configuration [Android Application] 
    DalvikVM [localhost:8612]   
        Thread [<1> main] (Suspended (exception RuntimeException))  
            <VM does not provide monitor information>   
            ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2195    
            ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2245 
            ActivityThread.access$800(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 135    
            ActivityThread$H.handleMessage(Message) line: 1196  
            ActivityThread$H(Handler).dispatchMessage(Message) line: 102    
            Looper.loop() line: 136 
            ActivityThread.main(String[]) line: 5017    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 515  
            ZygoteInit$MethodAndArgsCaller.run() line: 779  
            ZygoteInit.main(String[]) line: 595 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<10> Binder_2] (Running)    
        Thread [<9> Binder_1] (Running) 
        Thread [<11> Timer-0] (Running) 

logcat的:

06-14 09:58:59.484: W/ActivityThread(1243): Application com.akzonobel.malote is waiting for the debugger on port 8100...
06-14 09:58:59.494: I/System.out(1243): Sending WAIT chunk
06-14 09:58:59.584: I/dalvikvm(1243): Debugger is active
06-14 09:58:59.694: I/System.out(1243): Debugger has connected
06-14 09:58:59.744: I/System.out(1243): waiting for debugger to settle...
06-14 09:58:59.954: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.154: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.354: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.564: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.764: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.994: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:01.194: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:01.394: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:01.594: I/System.out(1243): debugger has settled (1348)
06-14 09:59:02.424: D/(1243): HostConnection::get() New Host Connection established 0xb8f54e48, tid 1243
06-14 09:59:02.514: W/EGL_emulation(1243): eglSurfaceAttrib not implemented
06-14 09:59:02.524: D/OpenGLRenderer(1243): Enabling debug mode 0
06-14 09:59:03.054: W/EGL_emulation(1243): eglSurfaceAttrib not implemented
06-14 09:59:06.384: D/AndroidRuntime(1243): Shutting down VM
06-14 09:59:06.384: W/dalvikvm(1243): threadid=1: thread exiting with uncaught exception (group=0xb1aa8ba8)
06-14 09:59:06.484: E/AndroidRuntime(1243): FATAL EXCEPTION: main
06-14 09:59:06.484: E/AndroidRuntime(1243): Process: com.akzonobel.malote, PID: 1243
06-14 09:59:06.484: E/AndroidRuntime(1243): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akzonobel.malote/com.akzonobel.malote.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.os.Looper.loop(Looper.java:136)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at java.lang.reflect.Method.invoke(Method.java:515)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at dalvik.system.NativeStart.main(Native Method)
06-14 09:59:06.484: E/AndroidRuntime(1243): Caused by: java.lang.RuntimeException: native typeface cannot be made
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.graphics.Typeface.<init>(Typeface.java:175)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.graphics.Typeface.createFromAsset(Typeface.java:149)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at com.akzonobel.malote.MainActivity.onCreate(MainActivity.java:32)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.Activity.performCreate(Activity.java:5231)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-14 09:59:06.484: E/AndroidRuntime(1243):     ... 11 more
06-14 10:00:38.184: I/Process(1243): Sending signal. PID: 1243 SIG: 9

源位于右侧文件夹(资产/字体),字体名称及其扩展名均为小写。

请尽可能详细解释,因为我是一名高中生,不熟悉编程。

抱歉我的英语不好。

谢谢!

更新! - LOGCAT:

06-14 18:31:33.607: D/(2031): HostConnection::get() New Host Connection established 0xb837bfa8, tid 2031
06-14 18:31:33.677: W/EGL_emulation(2031): eglSurfaceAttrib not implemented
06-14 18:31:33.687: D/OpenGLRenderer(2031): Enabling debug mode 0
06-14 18:31:34.037: W/EGL_emulation(2031): eglSurfaceAttrib not implemented
06-14 18:31:37.197: E/Typefaces(2031): Could not get typeface 'fonts/watermelon.ttf' because native typeface cannot be made
06-14 18:31:49.457: D/AndroidRuntime(2031): Shutting down VM
06-14 18:31:49.467: W/dalvikvm(2031): threadid=1: thread exiting with uncaught exception (group=0xb1a42ba8)
06-14 18:31:49.557: E/AndroidRuntime(2031): FATAL EXCEPTION: main
06-14 18:31:49.557: E/AndroidRuntime(2031): Process: com.akzonobel.malote, PID: 2031
06-14 18:31:49.557: E/AndroidRuntime(2031): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akzonobel.malote/com.akzonobel.malote.MainActivity}: java.lang.NullPointerException
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.os.Looper.loop(Looper.java:136)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at java.lang.reflect.Method.invoke(Method.java:515)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at dalvik.system.NativeStart.main(Native Method)
06-14 18:31:49.557: E/AndroidRuntime(2031): Caused by: java.lang.NullPointerException
06-14 18:31:49.557: E/AndroidRuntime(2031):     at com.akzonobel.malote.MainActivity.onCreate(MainActivity.java:35)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.Activity.performCreate(Activity.java:5231)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-14 18:31:49.557: E/AndroidRuntime(2031):     ... 11 more

3 个答案:

答案 0 :(得分:1)

您是否尝试从this移除this.getAssets()

TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/watermelon.ttf");
tx.setTypeface(custom_font);

根据this答案应该解决它。

答案 1 :(得分:0)

OP解决方案。

解决方案是在public view onCreateView中实现代码。看看它是如何适合的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    TextView txt = (TextView) rootView.findViewById(R.id.TextView1);
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/laouib.TTF");
    txt.setTypeface(font); 

    return rootView;

答案 2 :(得分:0)

将您的字体添加到src / main / assets / fonts中 在你的应用程序类中添加: TypefaceUtils.overrideFont(getApplicationContext(),“SERIF”,“nameofyourefont.ttf”);

相关问题