我正在尝试使ListView
元素可点击,但我得到了ClassCastException
. 这是代码:
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
exit();
}
});
TextView title_1 = (TextView) findViewById(R.id.textView_Menu_Title_1);
title_1.setText(decodeString(R.string.menu_title1));
title_1.setTypeface(getTitleTypeface());
TextView title_2 = (TextView) findViewById(R.id.textView_Menu_Title_2);
title_2.setText(decodeString(R.string.menu_title2));
title_2.setTypeface(getTitleTypeface());
ListView list = (ListView)findViewById(R.id.listView_Menu);
String[] items = { decodeString(R.string.menu_igraj),
decodeString(R.string.menu_rezultati),
decodeString(R.string.menu_postavke),
decodeString(R.string.menu_pomoc),
decodeString(R.string.menu_izlaz) };
CustomAdapter adapter = new CustomAdapter(items);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int
position, long id) {
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_igraj)))
{
startActivity(new Intent(MenuActivity.this, GameActivity.class));
} else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_pomoc))) {
startActivity(new Intent(MenuActivity.this, HelpActivity.class));
} else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_postavke))) {
startActivity(new Intent(MenuActivity.this, SettingsActivity.class));
} else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_rezultati))) {
startActivity(new Intent(MenuActivity.this, ScoresActivity.class));
} else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_izlaz))) {
exit();
}
}
});
}
private void exit() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
//startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
finish();
}
private Typeface getTitleTypeface() {
Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(),
decodeString(R.string.tf_angelic));
return tf;
}
private Typeface getListTypeface() {
Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(),
decodeString(R.string.tf_jennifer));
return tf;
}
private String decodeString(int id) {
String string = getResources().getString(id);
return string;
}
class CustomAdapter extends BaseAdapter {
String[] m_items;
LayoutInflater m_inflater;
public CustomAdapter(String items[]) {
m_items = items;
m_inflater = LayoutInflater.from(MenuActivity.this);
}
@Override
public int getCount() {
return m_items.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = m_inflater.inflate(R.layout.list_item, null);
holder.tv = (TextView) convertView.findViewById(R.id.textView_List_Item);
holder.tv.setTextColor(getResources().getColor(R.color.black_overlay));
holder.tv.setTypeface(getListTypeface());
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
holder.tv.setText(m_items[position]);
return convertView;
}
}
class ViewHolder {
TextView tv;
}
}
我也尝试过,ArrayAdapter
但它不起作用,我总是得到一个 runtimeException。当我调试它时,它显示这一行: TextView textView = (TextView) itemClicked
; 出现错误。
日志猫:
10-23 12:59:33.356: E/ActivityThread(638): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-23 12:59:33.356: E/ActivityThread(638): at java.lang.Thread.run(Thread.java:856)
10-23 12:59:33.387: E/StrictMode(638): null
10-23 12:59:33.387: E/StrictMode(638): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d50490 that was originally bound here
10-23 12:59:33.387: E/StrictMode(638): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
10-23 12:59:33.387: E/StrictMode(638): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
10-23 12:59:33.387: E/StrictMode(638): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
10-23 12:59:33.387: E/StrictMode(638): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
10-23 12:59:33.387: E/StrictMode(638): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
10-23 12:59:33.387: E/StrictMode(638): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
10-23 12:59:33.387: E/StrictMode(638): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
10-23 12:59:33.387: E/StrictMode(638): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
10-23 12:59:33.387: E/StrictMode(638): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
10-23 12:59:33.387: E/StrictMode(638): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
10-23 12:59:33.387: E/StrictMode(638): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
10-23 12:59:33.387: E/StrictMode(638): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
10-23 12:59:33.387: E/StrictMode(638): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-23 12:59:33.387: E/StrictMode(638): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-23 12:59:33.387: E/StrictMode(638): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-23 12:59:33.387: E/StrictMode(638): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-23 12:59:33.387: E/StrictMode(638): at java.lang.Thread.run(Thread.java:856)
10-23 12:59:33.387: W/ActivityManager(278): Unbind failed: could not find connection for android.os.BinderProxy@40fe0b80
10-23 12:59:33.406: E/ActivityThread(638): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d31c70 that was originally bound here
10-23 12:59:33.406: E/ActivityThread(638): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d31c70 that was originally bound here
10-23 12:59:33.406: E/ActivityThread(638): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
10-23 12:59:33.406: E/ActivityThread(638): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
10-23 12:59:33.406: E/ActivityThread(638): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
10-23 12:59:33.406: E/ActivityThread(638): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
10-23 12:59:33.406: E/ActivityThread(638): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
10-23 12:59:33.406: E/ActivityThread(638): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
10-23 12:59:33.406: E/ActivityThread(638): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
10-23 12:59:33.406: E/ActivityThread(638): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
10-23 12:59:33.406: E/ActivityThread(638): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
10-23 12:59:33.406: E/ActivityThread(638): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
10-23 12:59:33.406: E/ActivityThread(638): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
10-23 12:59:33.406: E/ActivityThread(638): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-23 12:59:33.406: E/ActivityThread(638): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-23 12:59:33.406: E/ActivityThread(638): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-23 12:59:33.406: E/ActivityThread(638): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-23 12:59:33.406: E/ActivityThread(638): at java.lang.Thread.run(Thread.java:856)
10-23 12:59:33.506: E/StrictMode(638): null
10-23 12:59:33.506: E/StrictMode(638): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d31c70 that was originally bound here
10-23 12:59:33.506: E/StrictMode(638): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
10-23 12:59:33.506: E/StrictMode(638): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
10-23 12:59:33.506: E/StrictMode(638): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
10-23 12:59:33.506: E/StrictMode(638): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
10-23 12:59:33.506: E/StrictMode(638): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
10-23 12:59:33.506: E/StrictMode(638): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
10-23 12:59:33.506: E/StrictMode(638): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
10-23 12:59:33.506: E/StrictMode(638): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
10-23 12:59:33.506: E/StrictMode(638): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
10-23 12:59:33.506: E/StrictMode(638): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
10-23 12:59:33.506: E/StrictMode(638): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
10-23 12:59:33.506: E/StrictMode(638): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-23 12:59:33.506: E/StrictMode(638): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-23 12:59:33.506: E/StrictMode(638): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-23 12:59:33.506: E/StrictMode(638): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-23 12:59:33.506: E/StrictMode(638): at java.lang.Thread.run(Thread.java:856)
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView_List_Item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="test"
android:textSize="@dimen/list_item_dim" />
</RelativeLayout>