1

我正在尝试使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>
4

4 回答 4

0

像这样使用,不能直接TextViewitemClicked

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();
                }

            }
        });

}
于 2013-10-23T11:44:16.813 回答
0

那是因为您单击的项目不是 TextView 类;您的演员在那一行是错误的,您需要从已点击的项目视图中获取 TextView,该视图正在通过点击回调传递给您。

于 2013-10-23T11:46:45.527 回答
0

为什么不直接从ListView所选位置获取位置并StringListViewAdapter数据中检索。

例如:

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
}

这样,您就不必担心不稳定的类铸件。希望这可以帮助!

于 2013-10-23T13:18:44.583 回答
0

1) Change your customAdapter class's getItem method

// CustomAdapter 
@Override
public Object getItem(int arg0) {
    // return arg0;
    return m_items[arg0];
}

2) Change your onItemClicked logic;

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
     }
}

This will solve your problem.

于 2013-10-23T13:32:07.460 回答