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

これは、クリックしたアイテムが TextView クラスではないためです。その行でキャストが間違っています。クリックされたアイテムのビューから TextView を取得する必要があります。そのビューはクリックコールバックを介して渡されます。

于 2013-10-23T11:46:45.527 に答える
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();
                }

            }
        });

}
于 2013-10-23T11:44:16.813 に答える
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
}

このように、不安定なクラスのキャストについて心配する必要はありません。お役に立てれば!

于 2013-10-23T13:18:44.583 に答える