سؤال

I want to implement an ActionBar in a listview. As given in developer.android.com I tried using extends ActionBar activity in my code. My code runs fine when I give extends Activity, But I won't get an ActionBar. The code crashes when I use extends ActionBarActivity with java.lang.exceptionininitializererror.

I have imported support library. The screenshot is attached. The imported libraries are android-support-v4.jar android-support-v7-appcompat.jar.

Below is my code. Please guide me on this.

Also please let me know if I can do it using any other way.

public class ImageTextListViewActivity extends Activity implements OnItemClickListener {

public static final String[] titles = new String[] { "Football",
        "Basketball" };

public static final Integer[] images = { R.drawable.football,
        R.drawable.basketball };

ListView listView;
List<RowItem> rowItems;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //ActionBar actionBar = getSupportActionBar();
   // actionBar.setDisplayShowTitleEnabled(false);
    //android.graphics.drawable.ColorDrawable.setColor(0xff9ACC00);
    //actionBar.setBackgroundDrawable(colorDrawable);
    //actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < titles.length; i++) {
        RowItem item = new RowItem(images[i], titles[i]);
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.list);
    CustomListViewAdapter adapter = new CustomListViewAdapter(this,
            R.layout.list_item, rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

    //Navigation to next page

    Intent myIntent = new Intent(view.getContext(), Football.class);
    myIntent.putExtra("Game",listView.getItemAtPosition(position).toString());
    startActivityForResult(myIntent, 0);

 //Navigation ends

}
}
هل كانت مفيدة؟

المحلول

You have to add appcompat library as a library project not just import the jar.

Import appcompat into your IDE from android_sdk_dir/extras/android/support/v7/appcompat.

Add it to your project as a library project. (Project -> Properties -> Android -> Library -> Add.. and select appcompat).

Don't forget to change the application theme to Theme.AppCompat.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top