如何正确创建此自定义ArrayAdapter?

时间:2013-02-16 05:24:22

标签: android android-arrayadapter android-holo-everywhere

我有这段代码,但它不想工作。

另外,Main.java第76行在我的onCreate中包含了这个:

setListAdapter(new ImageAndTextAdapter(ctx, R.layout.lchs_item,
    items, icons));

imageandtextadapter.java:

public class ImageAndTextAdapter extends ArrayAdapter<String> {

private LayoutInflater mInflater;

private String[] mStrings;
private TypedArray mIcons;

private int mViewResourceId;

public ImageAndTextAdapter(Context ctx, int viewResourceId,
        String[] strings, TypedArray icons) {
    super(ctx, viewResourceId, strings);

    mInflater = (LayoutInflater)ctx.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    mStrings = strings;
    mIcons = icons;

    mViewResourceId = viewResourceId;
}

@Override
public int getCount() {
    return mStrings.length;
}

@Override
public String getItem(int position) {
    return mStrings[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = mInflater.inflate(mViewResourceId, null);

    ImageView iv = (ImageView)convertView.findViewById(R.id.item_icon);
    iv.setImageDrawable(mIcons.getDrawable(position));

    TextView tv = (TextView)convertView.findViewById(R.id.item_text);
    tv.setText(mStrings[position]);

    return convertView;
}
    }

logcat的

`02-16 00:19:36.867: E/InputManager-JNI(379): An exception was thrown by callback 'interceptKeyBeforeQueueing'.
02-16 00:19:36.867: E/AndroidRuntime(27101): FATAL EXCEPTION: main
02-16 00:19:36.867: E/AndroidRuntime(27101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twk95.lchs.browser/com.twk95.lchs.browser.Main}: java.lang.ClassCastException: com.android.internal.policy.impl.PhoneLayoutInflater cannot be cast to org.holoeverywhere.LayoutInflater
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.os.Looper.loop(Looper.java:137)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.app.ActivityThread.main(ActivityThread.java:5195)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at java.lang.reflect.Method.invokeNative(Native Method)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at java.lang.reflect.Method.invoke(Method.java:511)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at dalvik.system.NativeStart.main(Native Method)
02-16 00:19:36.867: E/AndroidRuntime(27101): Caused by: java.lang.ClassCastException: com.android.internal.policy.impl.PhoneLayoutInflater cannot be cast to org.holoeverywhere.LayoutInflater
02-16 00:19:36.867: E/AndroidRuntime(27101):    at com.twk95.lchs.browser.ImageAndTextAdapter.<init>(ImageAndTextAdapter.java:27)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at com.twk95.lchs.browser.Main.onCreate(Main.java:76)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.app.Activity.performCreate(Activity.java:5104)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-16 00:19:36.867: E/AndroidRuntime(27101):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
02-16 00:19:36.867: E/AndroidRuntime(27101):    ... 11 more
02-16 00:19:36.875: E/InputManager-JNI(379): java.lang.NullPointerException
02-16 00:19:36.875: E/InputManager-JNI(379):    at com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeQueueing(PhoneWindowManager.java:3832)
02-16 00:19:36.875: E/InputManager-JNI(379):    at com.android.server.wm.InputMonitor.interceptKeyBeforeQueueing(InputMonitor.java:337)
02-16 00:19:36.875: E/InputManager-JNI(379):    at com.android.server.input.InputManagerService.interceptKeyBeforeQueueing(InputManagerService.java:1317)
02-16 00:19:36.875: E/InputManager-JNI(379):    at dalvik.system.NativeStart.run(Native Method)`

如果你想要更多代码,请问。 谢谢你的帮助!

编辑:我添加了更多代码。此外,system32和ebarrenchea的建议不起作用。 EDIT2:回答了我自己的问题。

下面的解决方案

3 个答案:

答案 0 :(得分:0)

我之前从未使用过holo,但似乎你的问题就在于这一行:

02-16 00:19:36.867: E/AndroidRuntime(27101): Caused by: java.lang.ClassCastException: com.android.internal.policy.impl.PhoneLayoutInflater cannot be cast to org.holoeverywhere.LayoutInflater
02-16 00:19:36.867: E/AndroidRuntime(27101):    at com.twk95.lchs.browser.ImageAndTextAdapter.<init>(ImageAndTextAdapter.java:27)

你的mLayoutInflater应该是android.view.LayoutInflater类型,可以用简单的方法解决:

import android.view.LayoutInflater;

答案 1 :(得分:0)

看起来我得到了它们。我正在实现一切正确,我刚刚在Main.activity上设置了错误列表适配器

setListAdapter(new ImageAndTextAdapter(ctx, R.layout.lchs_item, items, icons));

编辑

收件人:this.setListAdapter(new ImageAndTextAdapter(this, R.layout.lchs_item, items, icons));

答案 2 :(得分:0)

mInflater = LayoutInflater.from(ctx);
绝对是HoloEverywere的LayoutInflater。