2012-02-14 81 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