【问题标题】:Selecting Menu Items has no effect选择菜单项无效
【发布时间】:2013-08-16 13:04:41
【问题描述】:

我正在为 Android 创建一个应用程序,我想在上下文菜单中创建项目。 这不是问题,他们被展示了。但是当我点击它们时,什么也没有发生。

我配置了我需要配置的东西,但我真的找不到问题。你看到什么了吗?这是我的 Main-Java-Code 的完整代码。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView myWebView = (WebView) findViewById(R.id.webView);
    myWebView.loadUrl("ABC");}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.optionsmenu, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem about) {
    //respond to menu item selection
    switch (R.menu.optionsmenu) {
        case R.id.about:
            startActivity(new Intent(this, SecondActivity.class));
            return true;
        case R.id.download:
            startActivity(new Intent(this, DownloadActivity.class));
            return true;
        case R.id.impressum:
            startActivity(new Intent(this, ImpressumActivity.class));
        case R.id.license:
            startActivity(new Intent(this, LicenseActivity.class));
    }
    return false;
}

我希望他们显示活动,但没有任何反应。

感谢您的帮助

感谢 Phil,项目选择现在可以正常工作了。这是我的其他代码,每次我选择其他代码时应用程序都会崩溃。

这是 LicenseActivity:

            import android.app.Activity;
            import android.os.Bundle;
            import android.webkit.WebView;

            /**
             * Created by Florent on 16.08.13.
            */
            public class LicenseActivity extends Activity {
                        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                        WebView myWebView = (WebView) findViewById(R.id.licenseview);
                        myWebView.loadUrl("URL");
                        }
            }

Second Activity只是一个设计活动,其他活动同License Activity。

【问题讨论】:

    标签: android android-activity menu options


    【解决方案1】:

    我明白你的错误。您需要使用 MenuItem.getItemId() 获取开关内的菜单项 ID,然后 return return super.onOptionsItemSelected();

    public boolean onOptionsItemSelected(MenuItem about) {
        //respond to menu item selection
    
        switch (about.getItemId()) { // call this here
            case R.id.about:
                startActivity(new Intent(this, SecondActivity.class));
                return true;
            case R.id.download:
                startActivity(new Intent(this, DownloadActivity.class));
                return true;
            case R.id.impressum:
                startActivity(new Intent(this, ImpressumActivity.class));
            case R.id.license:
                startActivity(new Intent(this, LicenseActivity.class));
        }
    
        return super.onOptionsItemSelected(about); // return this instead of false
    }
    

    另外,不要忘记在清单文件中注册您的活动

    并确保在 Activity 的 onCreate() 方法中调用 setContentView(...)

    public class LicenseActivity extends Activity {
    
         public void onCreate(Bundle savedInstanceState) {
    
                super.onCreate(savedInstanceState);
    
                setContentView(R.layout.whateveryourlayoutis); // DONT FORGET THIS
    
                WebView myWebView = (WebView) findViewById(R.id.licenseview);
                myWebView.loadUrl("URL");
         }
    }
    

    【讨论】:

    • 我尝试了你的代码,然后当我点击它时它做了一些事情,谢谢你,但每次应用程序崩溃并自行关闭几秒钟后。
    • 您的应用在选择项目后关闭的原因可能有很多。但重要的是,项目的选择现在正在工作。请选择正确答案考虑根据您现在收到的错误消息打开一个新问题。
    • 您可以发布您收到的错误消息吗?您尝试通过 Menuitems 启动的 Activity 的 onCreate() 或 onStart() 方法很可能有错误。
    • 很遗憾,(应用程序)已停止。
    • 您是否在 Manifest 文件中注册了您正在启动的活动?每个新的 Activity 都需要在 Manifest 中注册才能启动。看看这里:stackoverflow.com/questions/3832619/… 此外,我看到你的新 Activity 永远不会调用 setContentView(R.layout.yourlayout) 所以 WebView 将为空并且“myWebView.loadUrl("URL");”将导致 NullPointerException。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    相关资源
    最近更新 更多