如果 ListActivity 是从 ActivityGroup 内启动的,则当列表为空时,选项菜单不会从 ListActivity 打开

发布于 2024-11-29 16:55:27 字数 3850 浏览 2 评论 0原文

这是我上周发布的问题的后续(列表为空时,选项菜单无法从 ListActivity 打开。)我已经做了一些工作来缩小我遇到的问题的范围。选项菜单的问题相当复杂,不适合胆小的人。

我的项目使用带有选项菜单的 ListActivity。 ListActivity 从活动组内启动。 (我在活动组内使用它,以便“后退”按钮可以正常工作,以便在特定选项卡内的活动中向后移动。)

只要为列表提供了数据,选项菜单就可以完美工作。但是,如果没有数据,选项菜单按钮将不会触发选项菜单。

然而,选项菜单确实适用于具有空列表的 ListActivity,前提是 ListActivity 不是从 ActivityGroup 内启动的。

为了重现这个问题,我使用从 ActivityGroup 类 (TabsFirstGroup) 内部开始的 ListActivity 类 (MsgList) 创建了一个非常非常精简的示例。

如果我注释掉 MsgList 中的第 30 行(我在其中填充提供数据的数组 )列表,words.add(s);) 选项菜单将无法实例化。我从未到达 MsgList 内的 onCreateOptionsMenu() 方法,这是通过设置从未命中的调试断点推断出来的。

这是 TabsFirstGroup 类: 包 com.techmeridian.testframework;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class TabsFirstGroup extends ActivityGroup {

// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
public static TabsFirstGroup group;

    // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
    private ArrayList<View> history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          this.history = new ArrayList<View>();
          group = this;
          View view = getLocalActivityManager().startActivity("MsgListActivity", new
                                            Intent(this,MsgList.class)
                                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                            .getDecorView();
          replaceView(view);
       }

    public void replaceView(View v) {
        // Adds the old one to history
        history.add(v);
        // Changes this Groups View to the new View.
        setContentView(v);
    }

    public void back() {
        if(history.size() > 0) {
            history.remove(history.size()-1);
            setContentView(history.get(history.size()-1));
        }else {
            finish();
        }
    }

   @Override
    public void onBackPressed() {
        TabsFirstGroup.group.back();
        return;
    }

是 MsgList 类:

package com.techmeridian.testframework;

import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.ArrayAdapter;

public class MsgList extends ListActivity {
    private static final String[] items={"lorem", "ipsum", "dolor",
        "sit", "amet", "consectetuer", "adipiscing", "elit",
        "morbi", "vel", "ligula", "vitae", "arcu", "aliquet",
        "mollis", "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

    private ArrayList<String> words=null;   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        words=new ArrayList<String>();

        for (String s : items) {
            // IF I COMMENT THE NEXT LINE OUT, THEN THE OPTION MENU WILL NOT WORK!
            words.add(s);
        }

        setListAdapter(new ArrayAdapter<String>(this,
                                        android.R.layout.simple_list_item_1, words));

        registerForContextMenu(getListView());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }        
}

任何帮助将不胜感激。谢谢。

This is a follow up to a question I posted last week ( Option Menu not opening from ListActivity when list is empty .) I have done some work to narrow down the problem I have been having. The problem with the option menu is rather convoluted and not for the faint at heart.

My project uses as ListActivity with an Option Menu. The ListActivity is started from within an Activity Group. (I use it inside an activity group, so that the "back" button will work properly for moving backwards through activities within a particular tab.)

The Option Menu works perfectly, as long as there is data supplied for the list. However, if there is no data, the option menu button will not trigger the option menu.

The Option Menu does, however, work for a ListActivity with an empty list, provided the ListActivity is not started from within an ActivityGroup.

To recreate the problem, I have created a very very stripped down example using a ListActivity class (MsgList) started from within an ActivityGroup class (TabsFirstGroup.)

If I comment out line 30 in MsgList (where I populate the array that supplies the data for the list, words.add(s);) the options menu will fail to instantiate. I never reach the onCreateOptionsMenu() method inside MsgList, as is deduced through setting a debug break point that is never hit.

Here is the TabsFirstGroup class:
package com.techmeridian.testframework;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class TabsFirstGroup extends ActivityGroup {

// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
public static TabsFirstGroup group;

    // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
    private ArrayList<View> history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          this.history = new ArrayList<View>();
          group = this;
          View view = getLocalActivityManager().startActivity("MsgListActivity", new
                                            Intent(this,MsgList.class)
                                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                            .getDecorView();
          replaceView(view);
       }

    public void replaceView(View v) {
        // Adds the old one to history
        history.add(v);
        // Changes this Groups View to the new View.
        setContentView(v);
    }

    public void back() {
        if(history.size() > 0) {
            history.remove(history.size()-1);
            setContentView(history.get(history.size()-1));
        }else {
            finish();
        }
    }

   @Override
    public void onBackPressed() {
        TabsFirstGroup.group.back();
        return;
    }

}

Here is the MsgList class:

package com.techmeridian.testframework;

import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.ArrayAdapter;

public class MsgList extends ListActivity {
    private static final String[] items={"lorem", "ipsum", "dolor",
        "sit", "amet", "consectetuer", "adipiscing", "elit",
        "morbi", "vel", "ligula", "vitae", "arcu", "aliquet",
        "mollis", "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

    private ArrayList<String> words=null;   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        words=new ArrayList<String>();

        for (String s : items) {
            // IF I COMMENT THE NEXT LINE OUT, THEN THE OPTION MENU WILL NOT WORK!
            words.add(s);
        }

        setListAdapter(new ArrayAdapter<String>(this,
                                        android.R.layout.simple_list_item_1, words));

        registerForContextMenu(getListView());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }        
}

Any assistance would be greatly appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文