Android 与ContextMenuResource有关的问题

Android 与ContextMenuResource有关的问题,android,Android,我无法获取ContextMenuResource以返回我正在查找的正确信息 有时它会返回我想要的东西,其他时候则不是这样 public class montypython extends Activity implements OnClickListener { private static final int SAVE_SOUND = 0; // Tag to easily locate errors in the log file. private static

我无法获取
ContextMenuResource
以返回我正在查找的正确信息

有时它会返回我想要的东西,其他时候则不是这样

 public class montypython extends Activity implements OnClickListener {
    private static final int SAVE_SOUND = 0;

    // Tag to easily locate errors in the log file.

    private static final String TAG = "Monty";

    // Declares the buttons.

    Button button0;
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    Button button6;
    Button button7;
    Button button8;
    Button button9;
    Button button10;
    Button button11;
    Button button12;
    Button button13;
    Button button14;
    Button button15;
    Button button16;
    Button button17;
    Button button18;
    Button button19;
    Button button20;
    Button button21;
    Button button22;
    Button button23;
    Button button24;
    Button button25;

    // Makes contextMenuResource available for use in other methods.

    public int contextMenuResource;

    // Makes MediaPlayer (mp) null and ready to go.

    MediaPlayer mp=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    // Set Views and Listeners and Tags for buttons.

    button0=(Button)findViewById(R.id.button0);
    button0.setOnClickListener(this);
    button0.setTag(R.raw.a);
    button1=(Button)findViewById(R.id.button1);
    button1.setOnClickListener(this);
    button1.setTag(R.raw.b);
    button2=(Button)findViewById(R.id.button2);
    button2.setOnClickListener(this);
    button2.setTag(R.raw.c);
    button3=(Button)findViewById(R.id.button3);
    button3.setOnClickListener(this);
    button3.setTag(R.raw.d);
    button4=(Button)findViewById(R.id.button4);
    button4.setOnClickListener(this);
    button4.setTag(R.raw.e);
    button5=(Button)findViewById(R.id.button5);
    button5.setOnClickListener(this);
    button5.setTag(R.raw.f);
    button6=(Button)findViewById(R.id.button6);
    button6.setOnClickListener(this);
    button6.setTag(R.raw.g);
    button7=(Button)findViewById(R.id.button7);
    button7.setOnClickListener(this);
    button7.setTag(R.raw.h);
    button8=(Button)findViewById(R.id.button8);
    button8.setOnClickListener(this);
    button8.setTag(R.raw.i);
    button9=(Button)findViewById(R.id.button9);
    button9.setOnClickListener(this);
    button9.setTag(R.raw.j);
    button10=(Button)findViewById(R.id.button10);
    button10.setOnClickListener(this);
    button10.setTag(R.raw.k);
    button11=(Button)findViewById(R.id.button11);
    button11.setOnClickListener(this);
    button11.setTag(R.raw.l);
    button12=(Button)findViewById(R.id.button12);
    button12.setOnClickListener(this);
    button12.setTag(R.raw.m);
    button13=(Button)findViewById(R.id.button13);
    button13.setOnClickListener(this);
    button13.setTag(R.raw.n);
    button14=(Button)findViewById(R.id.button14);
    button14.setOnClickListener(this);
    button14.setTag(R.raw.o);
    button15=(Button)findViewById(R.id.button15);
    button15.setOnClickListener(this);
    button15.setTag(R.raw.p);
    button16=(Button)findViewById(R.id.button16);
    button16.setOnClickListener(this);
    button16.setTag(R.raw.q);
    button17=(Button)findViewById(R.id.button17);
    button17.setOnClickListener(this);
    button17.setTag(R.raw.r);
    button18=(Button)findViewById(R.id.button18);
    button18.setOnClickListener(this);
    button18.setTag(R.raw.s);
    button19=(Button)findViewById(R.id.button19);
    button19.setOnClickListener(this);
    button19.setTag(R.raw.t);
    button20=(Button)findViewById(R.id.button20);
    button20.setOnClickListener(this);
    button20.setTag(R.raw.u);
    button21=(Button)findViewById(R.id.button21);
    button21.setOnClickListener(this);
    button21.setTag(R.raw.v);
    button22=(Button)findViewById(R.id.button22);
    button22.setOnClickListener(this);
    button22.setTag(R.raw.w);
    button23=(Button)findViewById(R.id.button23);
    button23.setOnClickListener(this);
    button23.setTag(R.raw.x);
    button24=(Button)findViewById(R.id.button24);
    button24.setOnClickListener(this);
    button24.setTag(R.raw.y);
    button25=(Button)findViewById(R.id.button25);
    button25.setOnClickListener(this);
    button25.setTag(R.raw.z);

    // Registering ContextMenu's for long clicks.

    registerForContextMenu(button0);
    registerForContextMenu(button1);
    registerForContextMenu(button2);
    registerForContextMenu(button3);
    registerForContextMenu(button4);
    registerForContextMenu(button5);
    registerForContextMenu(button6);
    registerForContextMenu(button7);
    registerForContextMenu(button8);
    registerForContextMenu(button9);
    registerForContextMenu(button10);
    registerForContextMenu(button11);
    registerForContextMenu(button12);
    registerForContextMenu(button13);
    registerForContextMenu(button14);
    registerForContextMenu(button15);
    registerForContextMenu(button16);
    registerForContextMenu(button17);
    registerForContextMenu(button18);
    registerForContextMenu(button19);
    registerForContextMenu(button20);
    registerForContextMenu(button21);
    registerForContextMenu(button22);
    registerForContextMenu(button23);
    registerForContextMenu(button24);
    registerForContextMenu(button25);

}

    // Using the Tag to allow for simple media playing coding.

    @Override
    public void onClick(View vv) {
        if (mp !=null) {mp.stop();}
        mp=MediaPlayer.create(this, ((Integer)vv.getTag()).intValue());
        mp.start();
    }

    // Create ContextMenu's.

    @Override
    public void onCreateContextMenu(ContextMenu menu, View vv, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, vv, menuInfo);
        contextMenuResource = ((((Integer) (vv.getTag())).intValue()));
        menu.setHeaderTitle(((Button)vv).getText());

        // contextMenuResource used here just to see what value it contains
        // when called it shows /res/raw/*.mp3 depending on which button

        menu.add(0, SAVE_SOUND, 0, contextMenuResource);
    }

    public boolean onContextItemSelected(MenuItem item) {

        switch(item.getItemId()) {
            case SAVE_SOUND:

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        // contextMenuResource used here just to see what value it contains
        // when called it shows at least a seven digit integer (which makes sense to me)

        dialogBuilder.setMessage("You selected " + contextMenuResource);
        dialogBuilder.setCancelable(true);
        dialogBuilder.create().show();

        // Creates a directory on the SDCard 

            File sddir = new File("/sdcard/NetApex/Ringtones");
            if (!sddir.mkdirs()) {
                Log.e(TAG, "Create dir on sdcard failed");  
                return false;
                }   

        // The rest is commented out since I can't get the data I would like in order
        // to copy the file to the SDCard

        /*  InputStream fIn = getBaseContext().getResources().openRawResource(contextMenuResource);
            int size = 0;
            size = fIn.available();
            byte[] buffer = new byte[size];
            fIn.read(buffer);
            fIn.close();

            File rtNew = new File("/sdcard/NetApex/Ringtones"
            + contextMenuResource
            +".mp3");

            FileOutputStream rtFOS = null;
            rtFOS = new FileOutputStream(rtNew);
            rtFOS.write(buffer);
            rtFOS.flush();
            rtFOS.close();  */

            return true;  
        }
        return false;      
    }

    // Options menu created just to see it created

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);

        return true;
    }

    // This makes the selection work

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {

        case R.id.quit:
            finish();
            return true;
        }
        return false;
    }
}

在我看来,这在很多方面都是错误的。。。你看过这个吗?你为什么要注册这些按钮?你的UI看起来怎么样?您已经打开了dilog-只需将布局膨胀到其中,即可显示所需内容并执行操作。

contextMenuResource
是原始资源ID的值。您不应尝试将其用作上下文菜单选项的标题,因为您在这里:

menu.add(0, SAVE_SOUND, 0, contextMenuResource);

您似乎没有在其他任何地方使用
contextMenuResource
,而且由于您没有解释您的实际症状,我不得不猜测。

用户界面几乎是一系列按钮。典型的音板。不过,我会查看建议页面,看看是否有更好的方法。但问题仍然是根据按下的按钮下载文件。我在那里使用它只是为了查看它报告的值,在这种情况下,结果是/res/raw/*.mp3,这取决于长时间按下的按钮。(这正是我希望的值)当我以:dialogBuilder.setMessage(“您选择了”+contextMenuResource)的身份调用时;它给出了完全不同的回答(例如2167480)。这就是我的困惑所在。我要再说一遍:你不应该试图用它作为上下文菜单选项的标题。它不是一个字符串资源ID——它是一个原始资源ID,两者不能互换。你做了一些你不应该做的事情,却得到了奇怪的结果,正是因为它会给你奇怪的结果。明白了,现在就改变吧。双手交叉,手持一棵四叶苜蓿。你真是个天才。它完全按照预期的方式工作(这是我的问题)。现在,让手机认识到它可以使用这些文件进行铃声或通知是我最后的障碍。我错误地找到了一种方法,让手机扫描卡上的媒体文件,所以现在它可以在普通播放列表中播放这些文件。唉,总有东西!“你是一个纯粹的天才。”两方面都没有,但谢谢你的感悟!实际上,我并没有为设置或添加铃声而烦恼,所以在这方面我没有特别的建议。