contextMenu dosent工作和播放时崩溃

时间:2017-06-13 20:07:04

标签: android

我的代码在第registerForContextMenu(lv);

中崩溃了

你能看到问题吗?我被困了几个小时。

我正在尝试连接列表视图 我知道它的东西很小,但我找不到它 请需要一些帮助 代码:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boss);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        datasource = new TableInfoDataSource(this);
        datasource.open();
        userPreferences = null;
        userPreferences = datasource.getAllPublications();
        lv = (ListView) findViewById(android.R.id.list);
        registerForContextMenu(lv); // for context menu!
        adapter = new CustomAdapter(Boss.this, R.layout.simple_list_item_2, userPreferences);
        lv.setAdapter(adapter);
        datasource.close();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
                Intent intent_employee = new Intent(Boss.this, BossAddJob.class);
                startActivity(intent_employee);
            }
        });


    }

    class CustomAdapter extends ArrayAdapter<TableInfo> {

        Context context;
        int layoutResourceId;
        ArrayList<TableInfo> data = null;
        private LayoutInflater mInflater;


        public CustomAdapter(Context context, int layoutResourceId, ArrayList<TableInfo> data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
            this.mInflater = LayoutInflater.from(context);
        }

 @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        //final SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        switch (item.getItemId()) {
            case R.id.ViewInContextMenu:

                return true;

            case R.id.DeleteInContextMenu:

                return true;
            default:
                return super.onContextItemSelected(item);
        }

1 个答案:

答案 0 :(得分:0)

问题在于ListView初始化:

lv = (ListView) findViewById(android.R.id.list);

...因为你只使用&#34; android&#34; SDK随附的标准Android功能的关键字。那说你只需要使用&#34; R.id.list&#34;相反,这意味着你在res目录中引用了一个资源文件(在这种情况下是布局XML文件),假设你在你的布局中正确配置了ListView,R.layout.activity_boss。