java - 从抽屉导航打开 Activity

标签 java android android-activity navigation-drawer

每次我从 Navigation Drawer 打开我的 Activity 时,都会出现一个空白屏幕。我正在尝试从我的导航中打开一个 fragment :

public class MainActivity extends ActionBarActivity {

    private ListView mDrawerList;
    private DrawerLayout mDrawerLayout;
    private ArrayAdapter<String> mAdapter;
    private ActionBarDrawerToggle mDrawerToggle;
    private String mActivityTitle;

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

        mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();

        addDrawerItems();
        setupDrawer();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    private void addDrawerItems() {
        String[] osArray = {"Math", "Physics", "Chemistry"};
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
        mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                    switch(position) {
                        case 0:
                            Intent a = new Intent(MainActivity.this, MathActivity.class);
                            startActivity(a);
                            break;

                        case 1:
                            Intent b = new Intent(MainActivity.this, PhysicsActivity.class);
                            startActivity(b);
                            break;
                        case 2:
                            Intent c = new Intent(MainActivity.this, ChemistryActivity.class );
                            startActivity(c);
                            break;
                        default:
                    }

            }

        });


    }
    private void setupDrawer() {
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle("Navigation!");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mActivityTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    @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);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

像这样打开Activity:

public class MathActivity extends Activity{

protected void onCreate(Bundle bundle) {

    setContentView(R.layout.math_fragment_activity);
    super.onCreate(bundle);
}

}

每次我这样做时,都会出现一个空白屏幕。为什么会发生这种情况,谁能帮我解决这个问题?

更新: 这是我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >

<ListView
    android:id="@+id/listViewAnimals"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:dividerHeight="0.1dp"
    android:divider="#0000CC"/> </RelativeLayout>

最佳答案

尝试将 setContentiew 切换到 super.onCreate 调用之后。

关于java - 从抽屉导航打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30402967/

相关文章:

java - 正确使用附加类文件中的方法

java - 如何访问 rx 中先前发射的数据?

android - 重新启动应用程序以释放堆内存

java - 将 Activity 传递给服务

android-studio - 从片段更改 Activity 的按钮状态

java - hibernate : select for update in two request

java - 未填充 JPA/Hibernate 静态元模型属性——NullPointerException

Android:具有多个 UI Activity (多线程/处理程序)的蓝牙 SPP 应用程序的一般方向

android - 如何查找 Google 云端硬盘文件的资源 ID

java - android中 Intent 中对象的浅拷贝