Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.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 textView onCreate更改_Java_Android_Textview - Fatal编程技术网

Java Android textView onCreate更改

Java Android textView onCreate更改,java,android,textview,Java,Android,Textview,我已经使用main.xml上的布局编辑器创建了一个名为textView1的textView。我想使用自定义字体,所以我在onCreate中设置了一行字体代码,但它似乎无法识别名称textView1 package com.mystraldesign.memorable; import android.app.Activity; import android.graphics.Typeface; import android.os.Bundle; public class MemorableA

我已经使用main.xml上的布局编辑器创建了一个名为textView1的textView。我想使用自定义字体,所以我在onCreate中设置了一行字体代码,但它似乎无法识别名称textView1

package com.mystraldesign.memorable;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;

public class MemorableActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Typeface type = Typeface.createFromAsset(getAssets(),"fonts/optima.ttf"); 
        textView1.setTypeface(type);
    }
}

我肯定我错过了一些简单的东西,但这是我第一次编写Android,所以我仍然在摸索。请在代码中初始化TextView

TextView textView1= (TextView) findViewById(R.id.text_info);
Typeface type= Typeface.createFromAsset(getAssets(),"fonts/optima.ttf");
textView1.setTypeface(type);
试试这个:-

 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      // Font path
      String fontPath = "fonts/optima.ttf";
      // text view label
      TextView textView1= (TextView) findViewById(R.id.name);
     // Loading Font Face
      Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
     // Applying font
      textView1.setTypeface(tf);
  }

请在代码中初始化TextView

试试这个:-

 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      // Font path
      String fontPath = "fonts/optima.ttf";
      // text view label
      TextView textView1= (TextView) findViewById(R.id.name);
     // Loading Font Face
      Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
     // Applying font
      textView1.setTypeface(tf);
  }

imran khan,我试过了,但是得到了一个错误,text_info无法解析或者不是一个字段,它提供了迁移代码的修复。OK,你在main.xml布局中有id为text_info的textview。如果没有,那么首先在main.xml布局中添加一个textview,你想为它设置自定义字体是愚蠢的。文本信息应该是文本视图1。我太不习惯了Java@JamesKraw:james只需将text_info替换为您要设置字体的textview id Imran khan,我试过了,但是得到了一个错误,text_info无法解析或者不是一个字段,它提供了迁移代码的修复。确定您在main.xml布局中有id为text_info的textview。如果没有,那么首先在main.xml布局中添加一个textview,您想为其设置自定义字体是愚蠢的。文本信息应该是文本视图1。我太不习惯了Java@JamesKraw:james只需将text_info替换为您要设置字体的textview id以下解决方案是否解决了您的问题?以下解决方案是否解决了您的问题?此。ADK为您做了很多事情,但它仍然无法神奇地创建您尚未声明的Java对象。ADK为您做了很多事情,但它仍然无法神奇地创建您尚未声明的Java对象。