我正在尝试点击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
;正在收到错误。
logcat:
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>
答案 0 :(得分:0)
像这样使用,您无法直接从TextView
itemClicked
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int
position, long id) {
TextView textView = (TextView) itemClicked.findViewById(R.id.textView_List_Item);;
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();
}
}
});
}
答案 1 :(得分:0)
那是因为你点击的项目不属于TextView类;你的演员阵容在那一行是错误的,你需要从已被点击的项目视图中获取TextView,该视图将通过点击回调传递给你。
答案 2 :(得分:0)
为什么不让您从所选的ListView
获得排名,并从String
数据中检索ListViewAdapter
。
例如:
public void onItemClick(AdapterView<?> parent,
View itemClicked,
int position,
long id) {
String selectedItem = items[position];//you may need to make String[] items final for this, or make it a global variable to the class
//do stuff with String
}
通过这种方式,您不必担心摇滚类的演员阵容。希望这有帮助!
答案 3 :(得分:0)
1)更改自定义Adapter类getItem方法
// CustomAdapter
@Override
public Object getItem(int arg0) {
// return arg0;
return m_items[arg0];
}
2)更改您的项目点击逻辑;
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
CustomAdapter customAdapter = (CustomAdapter)parent.getAdapter();
String selectedText = (String)customAdapter.getItem(position);
// Use your selected text here
}
}
这将解决您的问题。