简体   繁体   English

单击选项菜单上显示的空白屏幕

[英]Blank screen displayed on option menu click

I have option menu on my activity. 我的活动上有选项菜单。 I am displaying another activity on click of options menu using below method: 我使用以下方法单击选项菜单时显示了另一个活动:

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

First menu, some_menu , is working fine. 第一个菜单some_menu运行正常。 It displays mentioned Activity. 它显示提到的活动。 However, when I click second menu, someother_menu , it displays blank black screen. 但是,当我单击第二个菜单someother_menu ,它将显示黑屏。 No idea what is happening. 不知道发生了什么。

XML for SomeOtherActivity: 用于SomeOtherActivity的XML:

<?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>

I'm not sure if this is the solution, but try: 我不确定这是否是解决方案,但是请尝试:

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

Your layout is empty, so you will not see anything -> blank black screen. 您的布局为空,因此您将看不到任何内容->黑屏。

Add the text attribute to your TextView to show some dummy text: 将text属性添加到您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM