Question

I'm creating an app for Android and I wanted to make items in the context menu. This wasn't a Problem and they were shown. But when I click on them, nothing happens.

I configured the things I needed to configure, but I really am not able to find the Problem. Do you see something? Here my complete code of the 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;
}

I want them to Show the Activitys, but nothing happens.

Thanks for your help

Thanks to Phil, the item selection is now working. Here my other codes, the app breaks down every time I select the others.

Here is 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 is only a design activity, the other activitys are the sames as the License Activity.

Was it helpful?

Solution

I see your misstake. You need to get the menuitems Id inside the switch using MenuItem.getItemId(), and 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
}

Also, do not forget to register your Activities inside your Manifest file.

And make sure you are calling setContentView(...) inside your Activity's onCreate() method.

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");
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top