4

ImageAdapter extends BaseAdapterを膨らませるために使用していgridviewます。Gridview には 2 つtextviewの があります。それらの1つにカスタムフォントを設定したい。Typeface font = Typeface.createFromAsset(getAssets(), "BABYCAKE.TTF");inを使用するとImageAdaptererror が返されますThe method getAssets() is undefined for the type ImageAdapter

ImageAdapterと定義されている

package com.amit.wozzle;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.LayoutInflater;

public class ImageAdapter extends BaseAdapter
{  
private ArrayList<String> listCountry;  
private ArrayList<String> scorestage;  
private Activity activity;  
Typeface font;

public ImageAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<String> scorestage) {  
    super();  
    this.listCountry = listCountry;  
    this.scorestage = scorestage;  
    this.activity = activity;  
    font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");
}  

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return listCountry.size();  
}  

@Override  
public String getItem(int position) {  
    // TODO Auto-generated method stub  
    return listCountry.get(position);  
}  

@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return 0;  
}  

public static class ViewHolder  
{  
    public ImageView imgViewFlag;  
    public TextView  txtViewTitle; 
    public TextView  txtViewTitle2; 
}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  
    ViewHolder view;  
    LayoutInflater inflator = activity.getLayoutInflater();  

    if(convertView==null)  
    {  
        view = new ViewHolder();  
        convertView = inflator.inflate(R.layout.grid_item, null);  
        view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);  
        view.txtViewTitle2.setTypeface(font);
        view.txtViewTitle2 = (TextView) convertView.findViewById(R.id.textView2);  

        // view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);  
        convertView.setTag(view);  
    }  
    else  
    {  
        view = (ViewHolder) convertView.getTag();  
    }  
    // view.txtViewTitle2.setBackgroundColor(Color.BLUE);
    view.txtViewTitle2.setText(listCountry.get(position)); 
    view.txtViewTitle.setText(scorestage.get(position)); 
    // view.imgViewFlag.setImageResource(scorestage.get(position));  

    return convertView;  
}  
}

助けてください。

4

3 に答える 3

7

試す

Typeface font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");
于 2012-08-06T09:28:57.067 に答える
1

初期化する前に textView にアクセスしようとしているため、view.txtViewTitle2 で null ポインター例外が発生します。以下の変更は正しく機能するはずです

view.txtViewTitle2 = (TextView) convertView.findViewById(R.id.textView2)
view.txtViewTitle2.setTypeface(font);
于 2012-08-06T11:37:52.200 に答える
1

Typeface内にオブジェクトを作成するのはなぜですかgetViewTypeface内部で作成される Typeface オブジェクトの数が原因で使用可能なメモリ スペースが少なくなるため、大量のメモリが必要になり、アプリの速度が低下しますgetView

代わりに、フォント ファイルは 1 回だけ作成し、必要に応じて再利用する必要があります。の外部にフォント ファイルを作成しますgetView。アダプター内でのみ使用し、アダプター コンストラクター内で初期化する場合は、アダプター内でインスタンス変数として宣言します。getView 内で毎回新しいインスタンスを作成する代わりに、その単一のインスタンスを使用してフォントを設定します。

エラーの場合は、アクティビティ インスタンス変数を使用して呼び出しますgetAssests()

TypeFace font = Typeface.createFromAsset(activity.getAssets(), "fonts/BABYCAKE.TTF");

編集 -このように使用してみてください-

class DemoFonts{
    private static TypeFace typeFace;   
    public static TypeFace getTypeFace(Context mContext){
        if(typeface==null){
            typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/BABYCAKE.TTF");
        }

        return typeface;
    }

}

上記のように使用してみてください。fontsassets フォルダー内にフォルダーがあると仮定します。

于 2012-08-06T09:33:26.143 に答える