0

wordonlist.xml:

<TextView
        android:id="@+id/tvHiragana"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="Hiragana"
        android:textColor="@color/blackColor"
        android:textColorHint="@color/blackColor"
        android:textSize="18sp" />

主要活动:

setContentView(R.layout.activity_main);
    tvHiragana = (TextView) findViewById(R.id.tvHiragana);
    Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/JapaneseLetter.ttf");
    tvHiragana.setTypeface(tf,Typeface.BOLD);

结果:

tvHiragana.setTypeface(tf,Typeface.BOLD) 行中的 NullPointerException;

我认为这是因为 TextView 不在 activity_main.xml 中。如何获取 wordonlist.xml 中的 TextView 的参考?

4

4 回答 4

1

添加以下代码

LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 查看 customView= (View) mInflater.inflate(R.layout.wordonlist, null);

            TextView tvHiragana = (TextView) customView findViewById(R.id.tvHiragana);
              Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/JapaneseLetter.ttf");
              tvHiragana.setTypeface(tf,Typeface.BOLD);

如果您仍然遇到问题,请告诉我

于 2013-08-22T07:41:34.483 回答
1

试试这样:

LayoutInflater ltInflater = getLayoutInflater();
View view = ltInflater.inflate(R.layout.text, null, false);
于 2013-08-22T07:13:47.600 回答
0

您可以使用Layout inflator加载不在活动布局中的 xml 文件

LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE)
于 2013-08-22T07:12:26.380 回答
0

您应该使用 Infalter 添加不在 main.xml 中的任何布局

LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
       View textview= (View) mInflater.inflate(R.layout.wordonlist, null);

                TextView tvHiragana = (TextView) findViewById(R.id.tvHiragana);
                  Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/JapaneseLetter.ttf");
                  tvHiragana.setTypeface(tf,Typeface.BOLD);
于 2013-08-22T07:14:46.593 回答