Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在我的android应用程序中设置自定义字体?_Java_Android_Eclipse_Fonts_Android Activity - Fatal编程技术网

Java 如何在我的android应用程序中设置自定义字体?

Java 如何在我的android应用程序中设置自定义字体?,java,android,eclipse,fonts,android-activity,Java,Android,Eclipse,Fonts,Android Activity,我正试图在我的应用程序中添加自定义字体,但我犯了很多错误!谁能帮帮我?我是一个学生,没有什么判断力 这是我试图放在MainActivity.java中的代码: 这是主活动的代码: 使用这段代码,Eclipse不会声明任何错误,但当他运行时,错误活动和应用程序将单独关闭。怎么办 调试: 您是否尝试从this.getAssets()中删除this: 根据答案,应该解决它。解决方案由OP 解决方案是在创建视图的公共视图中实现代码。看看它是否合适: @Override public View onCre

我正试图在我的应用程序中添加自定义字体,但我犯了很多错误!谁能帮帮我?我是一个学生,没有什么判断力

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

这是主活动的代码:

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

调试:


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

根据答案,应该解决它。

解决方案由OP

解决方案是在创建视图的
公共视图中实现代码。看看它是否合适:

@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;

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

@metacubed No的可能重复项,在我的情况下,我所做的任何工作都不是实现sourceWhy you's writing Typefaces.get()?我认为不需要它们。哦,这是我根据解决方案进行的测试,但没有结果。您尝试过删除它吗?在您的textView1布局xml中,您是否按照我检查过的多次添加了自定义字体,本教程中的所有内容也是如此。我也尝试过其他教程,但仍然不起作用,应用程序会自动关闭,您将字体文件放在哪个文件夹中?它应该放在assets/fonts/她在
assets/fonts
中,尝试用textview交换字体定义,您是否记得将其添加为xml中的ressource?
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; 
            }
          }
       }
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) 
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
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
TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/watermelon.ttf");
tx.setTypeface(custom_font);
@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;