Htcsja
2021-05-13 23:42
采纳率: 100%
浏览 43

android点击menu菜单按钮后报错程序停止运行

错误如下:

点击菜单按钮

然后出现:

log报错信息如下:

optionsmenu.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/add"
        android:title="@string/dlmenuadd"
        />
    <item
        android:id="@+id/del"
        android:title="@string/dlmenudelete"
        />
</menu>

java文件如下:

package com.example.pc.wangxiaowen;


import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;

public class DiaryListActivity extends AppCompatActivity {
    //private String data[]={"标题1","标题2","标题3"};
    static ArrayList data = new ArrayList<String>(Arrays.asList("标题1", "标题2", "标题3"));
   // extends AppCompatActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary_list);
        Intent intent = this.getIntent();
        if (intent.getStringExtra("DiaryTitle") != null && intent.getStringExtra("DiaryTitle")!="") {
            String receive_str = intent.getStringExtra("DiaryTitle");
            data.add(receive_str);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(DiaryListActivity.this, android.R.layout.simple_list_item_1, data);
        ListView listView = (ListView)findViewById(R.id.listview);
        listView.setAdapter(adapter);
        this.registerForContextMenu(listView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.optionsmenu,menu);
        return true;


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
                Intent intent = new Intent(DiaryListActivity.this, DiaryDetailActivity.class);
                startActivity(intent);
                break;
            case R.id.del:
                AlertDialog dialog = new AlertDialog.Builder(this)
                       .setIcon(R.drawable.delete)
                       .setTitle("提示")
                       .setMessage("确认删除所选日志吗?")
                       .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                           @Override
                            public void onClick(DialogInterface dialog, int which) {
                               dialog.dismiss();
                           }
                        })
                       .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                           }
                       }).create();
                dialog.show();
                break;

        }
        return super.onOptionsItemSelected(item);
    }

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

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        switch (item.getItemId()){
            case R.id.plus:
                Toast.makeText(DiaryListActivity.this, "添加", Toast.LENGTH_SHORT).show();
                break;
            case R.id.look:
                Toast.makeText(DiaryListActivity.this, "查看", Toast.LENGTH_SHORT).show();
                break;
            case R.id.correct:
                Toast.makeText(DiaryListActivity.this, "修改", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete2:
                Toast.makeText(DiaryListActivity.this, "删除", Toast.LENGTH_SHORT).show();
                break;

        }
        return super.onContextItemSelected(item);
    }
}

安卓版本:Android studio 2.3.1

新手小白刚学安卓,求各位大佬看看这到底是是什么问题导致的啊!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • Htcsja 2021-05-14 00:09
    已采纳

    此问题已删除

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题