如何从单个菜单选项开始新活动

发布于 2024-12-16 13:02:33 字数 848 浏览 1 评论 0原文

您好,我正在尝试使用单个菜单选项(R.id.waypointEditId)打开一个新活动(GPSWptsEdit.class),但我对代码没有太多运气。每次我在 Eclipse 中以调试模式运行 GPSTrialActivity 并单击菜单选项时,当我单击“是”时,eclispe 中会弹出一个名为“确认透视切换”的窗口,默认检测选项卡显示“未找到源 - JAR 文件 C” :\Program Files (x86)\Android\android-sdk\platfoms\android-8\android.jar 没有附加源

如果有人可以提供一些建议,我们将不胜感激。是我一直在玩的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.waypointEditId:
        waypointEdit();
        return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
public void waypointEdit(){
     startActivity(new Intent(GPSTrialActivity.this, GPSWptsEdit.class));
}

}

Hi i am trying to open a new activity (GPSWptsEdit.class) with a single menu option (R.id.waypointEditId) but I am not having much luck with the code. Each time I run the GPSTrialActivity in debug mode in eclipse and click on the menu option I get a window pop up in eclispe called "Confirm Perspective Switch" when I click yes, the defualt instrumentation tab says "Source not found - The JAR file C:\Program Files (x86)\Android\android-sdk\platfoms\android-8\android.jar has no source attached.

If someone could assist with some suggestions, it would be much apreciated. Here is the code I have been playing with:

@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.waypointEditId:
        waypointEdit();
        return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
public void waypointEdit(){
     startActivity(new Intent(GPSTrialActivity.this, GPSWptsEdit.class));
}

}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放肆 2024-12-23 13:02:33

我在我的项目中使用了同样的东西并且效果很好。

我用过:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.custom_menu, menu);
        return true;
}

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

         case R.id.add_new:
                        openDialog();
                        return true;

         case R.id.delete_all:
                          // some code
                          return true;
         default:
                          return super.onOptionsItemSelected(item);
    }
}

public void openDialog() 
{
        Intent intent=new Intent(ThisActivity.this,NewActivity.class);          
        startActivity(intent);
        finish();               
}   

你遇到了什么问题?

i have used the same thing in my project and it was working well.

i used:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.custom_menu, menu);
        return true;
}

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

         case R.id.add_new:
                        openDialog();
                        return true;

         case R.id.delete_all:
                          // some code
                          return true;
         default:
                          return super.onOptionsItemSelected(item);
    }
}

public void openDialog() 
{
        Intent intent=new Intent(ThisActivity.this,NewActivity.class);          
        startActivity(intent);
        finish();               
}   

What problem do you get?

孤蝉 2024-12-23 13:02:33

在menifest中添加活动GPSWptsEdit。

add Activity GPSWptsEdit in menifest .

撧情箌佬 2024-12-23 13:02:33

问题可能是您在 switch-case 构造中缺少 break;尝试使用这段代码

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

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.waypointEditId:
        waypointEdit();
        break;
    default:
        return super.onOptionsItemSelected(item);
        break;
    }
}

public void waypointEdit(){
    Intent i = new Intent(GPSTrialActivity.this, GPSWptsEdit.class);
    startActivity(i);
    }
}

但是你能说出问题是什么吗?您遇到哪些错误? logcat 说什么?

The problem might be that you're missing the break in your switch-case construction; try with this code

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

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.waypointEditId:
        waypointEdit();
        break;
    default:
        return super.onOptionsItemSelected(item);
        break;
    }
}

public void waypointEdit(){
    Intent i = new Intent(GPSTrialActivity.this, GPSWptsEdit.class);
    startActivity(i);
    }
}

But could you tell what is the problem? Which errors you get? What does the logcat say?

尐籹人 2024-12-23 13:02:33

在阅读了提供的评论后,我最终解决了这个问题。
结果我忘记修改了:

setContentView(R.layout.wptedit);

I did end up resolving the issue, after reading the comments provided.
Turns out that, I forgot to amend this:

setContentView(R.layout.wptedit);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文