这是我的代码:
package com.greenwichdancestudio.greenwichdance;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;
List<DrawerItem> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// Add Drawer Item to Data List
dataList.add(new DrawerItem("Message", R.drawable.ic_action_email));
dataList.add(new DrawerItem("Likes", R.drawable.ic_action_good));
dataList.add(new DrawerItem("Games", R.drawable.ic_action_gamepad));
dataList.add(new DrawerItem("Lables", R.drawable.ic_action_labels));
dataList.add(new DrawerItem("Search", R.drawable.ic_action_search));
dataList.add(new DrawerItem("Cloud", R.drawable.ic_action_cloud));
dataList.add(new DrawerItem("Camara", R.drawable.ic_action_camera));
dataList.add(new DrawerItem("Video", R.drawable.ic_action_video));
dataList.add(new DrawerItem("Groups", R.drawable.ic_action_group));
dataList.add(new DrawerItem("Import & Export",
R.drawable.ic_action_import_export));
dataList.add(new DrawerItem("About", R.drawable.ic_action_about));
dataList.add(new DrawerItem("Settings", R.drawable.ic_action_settings));
dataList.add(new DrawerItem("Help", R.drawable.ic_action_help));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item, dataList);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close)
{
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
SelectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void SelectItem(int position) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (position) {
case 0:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 1:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 2:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 3:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 4:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 5:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 6:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 7:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 8:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 9:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 10:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 11:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
case 12:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(position)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(position)
.getImgResID());
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getSupportFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(dataList.get(position).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
SelectItem(position);
}
}
}
无论哪里有getSupportActionBar()调用,我都会收到以下错误:“无法解析方法'getSupportActionBar()'”。
我已经尝试导入v7和v4版本,但无济于事。也许我做错了什么?
有什么建议么?这将不胜感激!!!谢谢!
最佳答案
你需要改变
public class MainActivity extends FragmentActivity {..}
至
public class MainActivity extends ActionBarActivity {..}