如何从选项菜单startActivity [英] How to startActivity from Options Menu

查看:90
本文介绍了如何从选项菜单startActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个菜单项开始的活动但是它的每一次失败。我敢肯定,我俯瞰简单的东西。

P.S。

的我在做什么错,而不是刚刚更新code的解释是大大AP preciated!

  • 阿曼尼斯旺

来源:

 进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;

公共类AppActivity延伸活动{
私人按钮按钮1,按钮2,按钮3;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
按钮1 =(按钮)findViewById(R.id.button1);
按钮2 =(按钮)findViewById(R.id.button2);
按钮3 =(按钮)findViewById(R.id.button3);

}
公共无效openDialog(视图v){
AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(AppActivity.this)
.setTitle(设置菜单)
.setMessage(删除编辑或链接?)
.setCancelable(假)
.setNeutralButton(编辑,新DialogInterface.OnClickListener(){
     公共无效的onClick(DialogInterface对话框,INT ID){
         意图int​​entApp2Activity =新的意图(AppActivity.this,User1Settings.class);
         startActivity(intentApp2Activity);
     }
})
.setPositiveButton(通,新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface对话框,INT ID){
        意图int​​entApp2Activity =新的意图(AppActivity.this,User1.class);
        startActivity(intentApp2Activity);
    }
})
.setNegativeButton(删除,新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface对话框,INT ID){

        dialog.cancel();
    }
 });

AlertDialog alertDialog = alertDialogBu​​ilder.create();

alertDialog.show();
}

公共布尔onCreateOptionsMenu(功能菜单){
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.main,菜单);
返回true;
}
公共布尔onOptionsItemSelected(菜单项项){
意图int​​entApp2Activity =新的意图(AppActivity.this,User1.class);
startActivity(intentApp2Activity);
}
}
 


的logcat

 一月三号日至14号:51:12.781:D / dalvikvm(1982年):GC_FOR_ALLOC释放36K,7%免费2467K / 2648K,暂停51ms,总56MS
1月三号至14日:51:12.831:I / dalvikvm堆(1982):成长堆(破片的情况下),以6.061MB为3686416字节分配
1月三号至14日:51:12.951:D / dalvikvm(1982年):GC_FOR_ALLOC释放2K,3%的自由6065K / 6252K,暂停119ms,总119ms
1月三号至14日:51:13.131:D / dalvikvm(1982年):GC_CONCURRENT释放< 1K,3%的自由6065K / 6252K,暂停7毫秒+ 6ms的,总178ms
1月三号至14日:51:14.391:D / libEGL(1982年):加载/system/lib/egl/libEGL_emulation.so
1月三号至14日:51:14.487:D /(1982年):HostConnection ::得到()新主机连接建立0x2a152778,工业贸易署1982年
1月三号至14日:51:14.711:D / libEGL(1982年):加载/system/lib/egl/libGLESv1_CM_emulation.so
1月三号至14日:51:14.731:D / libEGL(1982年):加载/system/lib/egl/libGLESv2_emulation.so
1月三号至14日:51:14.981:W / EGL_emulation(1982年):eglSurfaceAttrib未实现
1月三号至14日:51:15.000:D / OpenGLRenderer(1982年):启用调试模式0
1月三号至14日:51:16.871:D / dalvikvm(1982年):GC_FOR_ALLOC释放12K,3%的自由6125K / 6280K,暂停53ms,共60ms的
1月三号至14日:51:16.941:I / dalvikvm堆(1982):成长堆(破片的情况下),以9.634MB为3686416字节分配
1月三号至14日:51:17.161:D / dalvikvm(1982年):GC_CONCURRENT释放2K,2%自由9722K / 9884K,暂停81ms + 17MS,总227ms
1月三号至14日:51:19.020:W / EGL_emulation(1982年):eglSurfaceAttrib未实现
1月三号至14日:51:42.030:D / AndroidRuntime(1982年):关闭虚拟机
1月三号至14日:51:42.040:W / dalvikvm(1982年):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40a71930)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):致命异常:主要
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):java.lang.Error:未解决编制问题:
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):此方法必须返回boolean类型的结果
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.example.linkingmanager.AppActivity.onOptionsItemSelected(AppActivity.java:84)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在android.app.Activity.onMenuItemSelected(Activity.java:2548)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在android.view.View.performClick(View.java:4204)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在android.view.View $ PerformClick.run(View.java:17355)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在android.os.Handler.handleCallback(Handler.java:725)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在android.os.Handler.dispatchMessage(Handler.java:92)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在android.os.Looper.loop(Looper.java:137)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在android.app.ActivityThread.main(ActivityThread.java:5041)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在java.lang.reflect.Method.invokeNative(本机方法)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在java.lang.reflect.Method.invoke(Method.java:511)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
1月三号至14日:51:42.160:E / AndroidRuntime(1982年):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

 公共布尔onOptionsItemSelected(菜单项项){
意图int​​entApp2Activity =新的意图(AppActivity.this,User1.class);
startActivity(intentApp2Activity);
}
 

必须是

 公共布尔onOptionsItemSelected(菜单项项){
意图int​​entApp2Activity =新的意图(AppActivity.this,User1.class);
startActivity(intentApp2Activity);
返回true;
}
 

您方法不返回布尔,但需要这样做,因为你的方法的签名

 公共布尔onOptionsItemSelected(菜单项项){
 

没有布尔返回结果中的未解决的编译错误,你看到的。

也可以使用<一个href="http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why">@Override注释时,possile ,以减少非覆盖的方法时,应overidden。

I'm attempting to start an activity from a MenuItem however it's failing every time. I'm sure I'm overlooking something simple.

P.S.

An explanation of what I'm doing wrong instead of just updated code is greatly appreciated!

  • Amani Swann

SOURCE:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AppActivity extends Activity {
private Button button1,button2,button3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3); 

}
public void openDialog(View v){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(AppActivity.this)
.setTitle("Settings Menu")
.setMessage("Delete Edit or Link?")
.setCancelable(false)
.setNeutralButton("Edit", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,int id) {
         Intent intentApp2Activity = new Intent(AppActivity.this,     User1Settings.class);
         startActivity(intentApp2Activity);
     }
})
.setPositiveButton("Link",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {
        Intent intentApp2Activity = new Intent(AppActivity.this, User1.class);
        startActivity(intentApp2Activity);
    }
})    
.setNegativeButton("Delete",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {

        dialog.cancel();
    }
 });

AlertDialog alertDialog = alertDialogBuilder.create();

alertDialog.show();
}    

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}   
public boolean onOptionsItemSelected(MenuItem item) {
Intent intentApp2Activity = new Intent(AppActivity.this, User1.class);
startActivity(intentApp2Activity);
}
}


LOGCAT

03-14 01:51:12.781: D/dalvikvm(1982): GC_FOR_ALLOC freed 36K, 7% free 2467K/2648K, paused 51ms, total 56ms
03-14 01:51:12.831: I/dalvikvm-heap(1982): Grow heap (frag case) to 6.061MB for 3686416-byte allocation
03-14 01:51:12.951: D/dalvikvm(1982): GC_FOR_ALLOC freed 2K, 3% free 6065K/6252K, paused 119ms, total 119ms
03-14 01:51:13.131: D/dalvikvm(1982): GC_CONCURRENT freed <1K, 3% free 6065K/6252K, paused 7ms+6ms, total 178ms
03-14 01:51:14.391: D/libEGL(1982): loaded /system/lib/egl/libEGL_emulation.so
03-14 01:51:14.487: D/(1982): HostConnection::get() New Host Connection established 0x2a152778, tid 1982
03-14 01:51:14.711: D/libEGL(1982): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-14 01:51:14.731: D/libEGL(1982): loaded /system/lib/egl/libGLESv2_emulation.so
03-14 01:51:14.981: W/EGL_emulation(1982): eglSurfaceAttrib not implemented
03-14 01:51:15.000: D/OpenGLRenderer(1982): Enabling debug mode 0
03-14 01:51:16.871: D/dalvikvm(1982): GC_FOR_ALLOC freed 12K, 3% free 6125K/6280K, paused     53ms, total 60ms
03-14 01:51:16.941: I/dalvikvm-heap(1982): Grow heap (frag case) to 9.634MB for 3686416-byte allocation
03-14 01:51:17.161: D/dalvikvm(1982): GC_CONCURRENT freed 2K, 2% free 9722K/9884K, paused     81ms+17ms, total 227ms
03-14 01:51:19.020: W/EGL_emulation(1982): eglSurfaceAttrib not implemented
03-14 01:51:42.030: D/AndroidRuntime(1982): Shutting down VM
03-14 01:51:42.040: W/dalvikvm(1982): threadid=1: thread exiting with uncaught exception     (group=0x40a71930)
03-14 01:51:42.160: E/AndroidRuntime(1982): FATAL EXCEPTION: main
03-14 01:51:42.160: E/AndroidRuntime(1982): java.lang.Error: Unresolved compilation problem: 
03-14 01:51:42.160: E/AndroidRuntime(1982):     This method must return a result of type boolean
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.example.linkingmanager.AppActivity.onOptionsItemSelected(AppActivity.java:84)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at android.app.Activity.onMenuItemSelected(Activity.java:2548)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at android.view.View.performClick(View.java:4204)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at android.view.View$PerformClick.run(View.java:17355)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at android.os.Handler.handleCallback(Handler.java:725)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at android.os.Looper.loop(Looper.java:137)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at     java.lang.reflect.Method.invoke(Method.java:511)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-14 01:51:42.160: E/AndroidRuntime(1982):     at dalvik.system.NativeStart.main(Native    Method)

解决方案

public boolean onOptionsItemSelected(MenuItem item) {
Intent intentApp2Activity = new Intent(AppActivity.this, User1.class);
startActivity(intentApp2Activity);
}

needs to be

public boolean onOptionsItemSelected(MenuItem item) {
Intent intentApp2Activity = new Intent(AppActivity.this, User1.class);
startActivity(intentApp2Activity);
return true;
}

Your method isn't returning a boolean, but it needs to do so since your method signature is

public boolean onOptionsItemSelected(MenuItem item) {

No boolean return results in the unresolved compilation error that you see.

Also use @Override annotations when possile to cut down on non overrides when the method should be overidden.

这篇关于如何从选项菜单startActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆