2012-02-14 83 views
0

我对我的活动有选项菜单。我使用下面的方法选项菜单中点击显示另一个活动:选项菜单上显示的空白屏幕点击

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    Intent intent = new Intent(); 

    switch (item.getItemId()) { 

    case R.id.some_menu: 
     intent.setComponent(new ComponentName(MyActivity.this,SomeActivity.class)); 
     startActivity(intent); 
     return true; 

    case R.id.someother_menu: 
     intent.setComponent(new ComponentName(MyActivity.this, SomeOtherActivity.class)); 
     startActivity(intent); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

一级菜单,some_menu,工作正常。它显示提到的活动。但是,当我点击第二个菜单someother_menu时,它会显示空白的黑屏。不知道发生了什么。

XML为SomeOtherActivity:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/follow" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="25sp" 
    /> 

<ListView 
    android:id="@+id/follow_list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
</ListView> 
</LinearLayout> 
+0

请发表您的'SomeOtherActivity'的代码。我想你忘记了调用'setContentView(R.layout.your_layout)'。 – WarrenFaith 2012-02-14 17:44:31

+0

我的setContentView在保护无效的onCreate(捆绑savedInstanceState){ \t \t super.onCreate(savedInstanceState)我的 “SomeOtherActivity”; \t \t setContentView(R.layout.follow); } – Reena 2012-02-14 17:57:09

+0

你确定布局确实显示了一些东西?请将xml添加到您的问题中,而不是作为评论。谢谢。 – WarrenFaith 2012-02-14 18:02:07

回答

0

您的布局是空的,所以你不会看到任何东西 - >空白黑色屏幕。

文本属性添加到您的TextView显示一些虚拟的文本:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:orientation="vertical" > 

<TextView 
    android:id="@+id/follow" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="25sp" 
    android:text="DUMMYTEXT" 
    /> 

<ListView 
    android:id="@+id/follow_list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
</ListView> 
</LinearLayout> 
+0

谢谢!现在我只需要检查我的列表为什么没有被填充。 – Reena 2012-02-14 18:29:51

0

我不知道,如果这是解决方案,但尝试:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    Intent intent = new Intent(); 

    switch (item.getItemId()) { 

    case R.id.some_menu: 
     intent.setComponent(new ComponentName(MyActivity.this,SomeActivity.class)); 
     startActivity(intent); 
     return true; 

    case R.id.someother_menu: 
     intent.setComponent(new ComponentName(MyActivity.this, SomeOtherActivity.class)); 
     startActivity(intent); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

没有得到你。它与我的代码相同。 – Reena 2012-02-14 17:59:21

+0

不,它不是。我把这个回报放在开关外面。 – ringkjob 2012-02-14 18:10:21

+0

试过。这是行不通的。 – Reena 2012-02-14 18:24:19