1

I want to design an app in which there will be two buttons. And when we are pressing on each button one by one, it will show a list of some content.

For this, I am using the following code:

this.findViewById( R.id.UpdateList ).setOnClickListener( this );
this.findViewById( R.id.SettingsList ).setOnClickListener(buttonclicked); 

With this i am creating butttons. For on click, I am using:

ArrayList<String> smsList = new ArrayList<String>();
public void onClick( View v ) {
    //My list content for first button
}

private OnClickListener buttonclicked = new OnClickListener() {
    public void onClick( View v ) {
        //My list content for second button
    }
    ListView settingsListView = (ListView) findViewById( R.id.SETTINGSList );
    settingsListView.setAdapter( new ArrayAdapter<String>( (Context) buttonclicked  , android.R.layout.simple_list_item_1, settingsList) );

but the problem is my first list is working fine but when the second button is pressed,it stopped forcefully.

EDIT logcat:

05-13 19:44:46.988: E/AndroidRuntime(6806): FATAL EXCEPTION: main 
05-13 19:44:46.988: E/AndroidRuntime(6806): java.lang.ClassCastException: com.bitgriff.androidcalls.MainActivity$1 cannot be cast to android.content.Context 
05-13 19:44:46.988: E/AndroidRuntime(6806):atcom.bitgriff.androidcalls.MainActivity$1.onClick(MainAc??tivity.java:506) 
05-13 19:44:46.988: E/AndroidRuntime(6806):atandroid.view.View.performClick(View.java:3627) 
05-13 19:44:46.988: E/AndroidRuntime(6806):atandroid.view.View$PerformClick.run(View.java:14329) –
4

2 回答 2

3

您正在将您的对象 buttonclicked(这是一个 OnClickListener)投射到一个上下文中。

settingsListView.setAdapter( new ArrayAdapter<String>( (Context) buttonclicked  , android.R.layout.simple_list_item_1, settingsList) );

改为使用YourActivity.this

于 2013-05-13T14:20:26.307 回答
1

你需要改变这个:

settingsListView.setAdapter( new ArrayAdapter<String>( (Context) buttonclicked, android.R.layout.simple_list_item_1, settingsList) )

对此:

settingsListView.setAdapter( new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, settingsList) )
于 2013-05-13T14:19:48.753 回答