导航抽屉未打开,具有扩展的BaseActivity

时间:2014-04-11 06:39:07

标签: android

我遇到的问题是我的导航抽屉无法打开,而对于我的生活,我无法弄清楚导致它的原因。有人可以看看,可能会看到我失踪的东西吗?

public class BaseActivity extends Activity
{
    public DrawerLayout drawerLayout;
    public ListView drawerList;
    public String[] layers;
    private ActionBarDrawerToggle drawerToggle;
    Intent twitch = new Intent(this, TwitchActivity.class);
    Intent community = new Intent(this, CommunityActivity.class);
    Intent esports = new Intent(this, ESportsActivity.class);
    Intent home = new Intent(this, MainActivity.class);

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_layout);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        final ActionBar actionBar = getActionBar();

        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_launcher, 0, 0)
        {
            public void onDrawerClosed(View view)
            {
                actionBar.setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView)
            {
                actionBar.setTitle(R.string.menu);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);


        layers = getResources().getStringArray(R.array.layers);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
                layers));


        drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                String selected = arg0.getItemAtPosition(pos).toString();
                if(selected.equals("Twitch"))
                    startActivity(twitch);
                if(selected.equals("Community"))
                    startActivity(community);
                if(selected.equals("ESports"))
                    startActivity(esports);
                if(selected.equals("Home"))
                    startActivity(home);
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }
}

我的主要活动

public class MainActivity extends BaseActivity {

private Spinner spinner;
public static String region;
public static String name;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
    //..........

我的drawer_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Nav Drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111111"/>
</android.support.v4.widget.DrawerLayout>

0 个答案:

没有答案