0

所以我搜索并发现我需要创建一个自定义数组适配器来更改列表视图活动的字体。我的列表活动由一个数组列表组成。所以我就这样做了

public class CustomArrayAdapter extends ArrayAdapter <String> {

private Typeface tf;
private LayoutInflater inflater;
private int resource;
private int textViewResourceId;
private ArrayList<String> objects;

public CustomArrayAdapter(Context context, int resource, int textViewResourceId,
        ArrayList<String> favorites, Typeface tf) {
    super(context, resource, textViewResourceId, favorites);
    this.tf = tf;
    this.resource = resource;
    this.textViewResourceId = textViewResourceId;
    this.objects = favorites;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = inflater.inflate(resource, parent, false);    
    }

    TextView text = (TextView) convertView.findViewById(textViewResourceId);
    text.setTypeface(tf);

    return convertView;
}

}

这是我的 Listactivity

public class Favorites extends ListActivity  {

public static ArrayList<String> favorites = new ArrayList<String>();
public static final String PREFS_NAME ="SAVEDATA";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



     Typeface cFont = Typeface.createFromAsset(getAssets(), "annoyingkettle.ttf");

     setListAdapter(new CustomArrayAdapter(this, android.R.layout.simple_list_item_1, R.id.ListView01, favorites, cFont));

这不起作用,我不知道为什么。当我尝试打开它时,我的 listactivity 崩溃了。我怎样才能解决这个问题?

这是我的xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ListView01" >

编辑:这是 LogCat

    '09-18 02:36:16.036: E/Trace(15562): error opening trace file: No such file or directory (2)
09-18 02:36:17.486: D/dalvikvm(15562): GC_FOR_ALLOC freed 73K, 7% free 2655K/2848K, paused 251ms, total 287ms
09-18 02:36:17.539: I/dalvikvm-heap(15562): Grow heap (frag case) to 3.781MB for 1127536-byte allocation
09-18 02:36:17.826: D/dalvikvm(15562): GC_FOR_ALLOC freed 2K, 5% free 3754K/3952K, paused 274ms, total 274ms
09-18 02:36:18.066: D/dalvikvm(15562): GC_CONCURRENT freed <1K, 5% free 3769K/3952K, paused 5ms+13ms, total 224ms
09-18 02:36:18.316: D/gralloc_goldfish(15562): Emulator without GPU emulation detected.
09-18 02:36:24.436: D/AndroidRuntime(15562): Shutting down VM
09-18 02:36:24.436: W/dalvikvm(15562): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-18 02:36:24.588: E/AndroidRuntime(15562): FATAL EXCEPTION: main
09-18 02:36:24.588: E/AndroidRuntime(15562): java.lang.NullPointerException
09-18 02:36:24.588: E/AndroidRuntime(15562):    at com.mkaya.ultimatejokescollection.CustomArrayAdapter.getView(CustomArrayAdapter.java:41)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.ListView.fillDown(ListView.java:674)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.ListView.fillFromTop(ListView.java:735)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.ListView.layoutChildren(ListView.java:1652)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.AbsListView.onLayout(AbsListView.java:1994)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.View.layout(View.java:14008)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.View.layout(View.java:14008)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.View.layout(View.java:14008)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.View.layout(View.java:14008)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.Choreographer.doFrame(Choreographer.java:532)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.os.Handler.handleCallback(Handler.java:725)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.os.Looper.loop(Looper.java:137)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at android.app.ActivityThread.main(ActivityThread.java:5041)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at java.lang.reflect.Method.invoke(Method.java:511)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-18 02:36:24.588: E/AndroidRuntime(15562):    at dalvik.system.NativeStart.main(Native Method)
09-18 02:36:27.846: I/Process(15562): Sending signal. PID: 15562 SIG: 9

'

4

2 回答 2

0

检查你TextView text = (TextView) convertView.findViewById(textViewResourceId);是否不为空。

尝试改变

setListAdapter(new CustomArrayAdapter(this, android.R.layout.simple_list_item_1, R.id.ListView01, favorites, cFont));

setListAdapter(new CustomArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, favorites, cFont));

于 2013-09-18T02:55:52.173 回答
0

这可能是因为您的字体可能为空。您必须将字体放在资产中的字体文件夹中,并在 Typeface.createFromAssets(getAssets(),"fonts/yourFontname"); 等活动中访问。

于 2013-09-18T03:53:43.470 回答