- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的应用程序中添加自定义字体,但我遇到了很多错误!有人请帮助我吗?我是一名学生,没有太多的意义。
这是我试图放入 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 不会声明任何错误,但当他运行错误的 Activity 时,应用程序会单独关闭。怎么办?
调试:
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
源在正确的文件夹( Assets /字体)中,字体名称及其扩展名均为小写。
请尽可能详细地解释,因为我是一名高中生并且是编程新手。
抱歉我的英语不好。
谢谢!
更新! - 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
最佳答案
您是否尝试过从 this.getAssets()
中删除 this
:
TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/watermelon.ttf");
tx.setTypeface(custom_font);
根据 this回答应该修复它。
关于java - 如何在我的 android 应用程序中放置自定义字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24223783/
语句 1: [2,4,6,7,8].each do |i| (i % 2 == 0) || (puts "Not even" && break) puts i end 声明 2: [2
我有一张正在显示的卡片,上面有一些信息。我想将其包装在SingleChildScrollView中,因为我还有更多项目要添加到卡中,但是当我这样做时,屏幕只是空白吗?我曾尝试将其作为根(脚手架主体)放
我有一个带有窗体的 View ,该窗体显示ViewModel中ObservableCollection中对象的数据。 ObservableCollection使我可以浏览数据。 ObservableC
如何将时间戳附加文件名放在HDFS中? hadoop fs -put topic_2018-12-15%2016:31:15.csv /user/file_structure/ 最佳答案 您只是在运行
我正在寻求一些帮助,以找出为什么以下叠加函数的运行时间会随着每次连续运行而增加。 据我所知,如果缓冲区中的文本保持不变,则运行时间应该是相同的——即,仅向左/向右移动光标应该不会增加运行时间(但它确实
我有一个事件指示器,它显示在中间。如何将其放置在 View 的左上角? var activityIndicator = UIActivityIndicatorView() func show() {
首先,我想提前感谢所有回答这个问题的人。非常感谢您的帮助。这是我第一次在这里发帖,所以如果我发帖不礼貌,请原谅我。 我的问题是关于方法原型(prototype)的: void copySubtree(
我正在开发一个应该是通用的应用程序,一个适用于 iPad 和 iPhone 的应用程序。我想让他们的界面尽可能相似。在 iPhone 应用程序中,我使用的是选项卡栏 Controller ,其中一个选
我目前正在使用 JS 开发 REST API,但遇到以下问题:该代码有效,但如果我尝试删除、放置或修补不存在的条目,它不会返回错误,但会打印成功消息。这是为什么?获取路由完美运行。 app.route
.a{ width:500px; height:500px; background:yellow; border: 3px dashed black; }
首先,请引用下图: 这基本上是我对布局的想法。 我想要的是: 内容 div 成为“主要焦点”,例如当浏览器 调整大小,它应该留在中间; 当浏览器被调整大小时,我希望这两个图像基本上 位于内容 div
我的应用程序需要使用内存映射并发访问数据文件。我的目标是使其在共享内存系统中可扩展。研究了内存映射文件库实现的源码,想不通: 在多个线程中从 MappedByteBuffer 中读取是否合法? get
我有一个 JDesktopPane 并希望以网格样式显示 JInternalFrames 而无需覆盖框架。框架的尺寸会有所不同,因此应动态分配它们的位置。我可以存储最后放置的框架的坐标,但可以移动、最
根据https://isocpp.org/wiki/faq/dtors#placement-new传递给placement-new的地址必须正确对齐。但它给出的例子似乎与此相矛盾。 char memo
我最近一直在查看 Illumos 源代码,发现了一些奇怪的东西。 在他们的源代码中,函数类型是这样写的: static int outdec64(unsigned char *out, unsigne
您好,我目前正在尝试在我的一张图片旁边放置一个图例,但我在放置时遇到了问题。 我想将图例放在图像的左侧或右侧。这是我当前的代码: .my-legend .legend-title { text-a
根据文档, print 之间的唯一区别和 say 似乎是后者添加了 "\n"(并使用 .gist 进行字符串化)。然而, perl6 -e 'print "del\b\b"' 打印“d”,有效地应用转
所以我试图将我的图像标志放在背景上,但我的背景突然被裁剪,出现了一半的黑屏。如图: 我的 main.dart 代码: import 'package:flutter/material.dart'; i
我正在使用 Azure DevOps 构建 python 轮。我想让它尽可能通用,以便团队中的每个人都可以使用相同的管道来构建自己的 python 轮并将它们部署在一些 databricks 工作区中
在构建标准(非 WordPress)网页时,我通常会在正文末尾之前加载所有 javascript 文件,然后包含页面特定 js 代码的部分。 WorPress 建议使用 wp_enqueue_scri
我是一名优秀的程序员,十分优秀!