0

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

此外,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;
}
    }

日志猫

`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:回答了我自己的问题。下面的解决方案

4

3 回答 3

0

我以前从未在任何地方使用过全息,但您的问题似乎出在这一行:

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;
于 2013-02-16T05:41:38.103 回答
0

看起来我明白了伙计们。我正在正确实施一切,我只是在我的 Main.activity 上设置了错误的列表适配器

编辑自:setListAdapter(new ImageAndTextAdapter(ctx, R.layout.lchs_item, items, icons));

到:this.setListAdapter(new ImageAndTextAdapter(this, R.layout.lchs_item, items, icons));

于 2013-02-16T06:36:27.043 回答
0

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

于 2013-02-17T02:52:46.720 回答