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" />
MainActivity:
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参考?
答案 0 :(得分:1)
试试这样:
LayoutInflater ltInflater = getLayoutInflater();
View view = ltInflater.inflate(R.layout.text, null, false);
答案 1 :(得分:1)
添加以下代码
LayoutInflater mInflater =(LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 查看customView =(查看)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);
如果您仍然遇到问题,请告诉我
答案 2 :(得分:0)
您可以使用Layout inflator加载不在活动布局中的xml文件
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE)
答案 3 :(得分: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);