2013-02-16 42 views
0

我有這個代碼,但它不想工作。如何正確創建此自定義ArrayAdapter?

也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的建議無效。 編輯2:回答了我自己的問題。下面

+0

您認爲這段代碼足以識別您的問題嗎?發佈完整代碼 – Pragnani 2013-02-16 05:36:31

+0

這是一個非常好的開始,OP表示很高興添加更多,試圖不做代碼轉儲。我認爲這是一個有效的嘗試,我們應該只批評那些甚至沒有嘗試...只是我的意見 – codeMagic 2013-02-16 05:41:41

+0

在你的oncreate你foget' this' here. this.mViewResourceId = mViewResourceId' – 2013-02-16 06:45:22

回答

0

看起來像我得到它的傢伙。我是正確的執行了一切,我只是名單適配器設置錯誤在我Main.activity

從編輯它:setListAdapter(new ImageAndTextAdapter(ctx, R.layout.lchs_item, items, icons));

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

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; 
+0

import android.view.LayoutInflater rather比org.holoeverywhere.LayoutInflater似乎沒有做任何事情。 – TheWizKid95 2013-02-16 05:58:25

0

mInflater = LayoutInflater.from(ctx);
絕對是HoloEverywere的LayoutInflater。

相關問題