2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

0

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");
     }
}
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • I tried your code and then it did something when I clicked, thank you but after a few seconds every time the app breaks down and Closes itself. – user2689457 Aug 16 '13 at 13:14
  • There could be numerous reasons for your app to close after selecting an item. But the important thing is that the selecting of Items is now working. Please select the correct answer consider opening a new question depending on the error messages you are now getting. – Philipp Jahoda Aug 16 '13 at 13:19
  • Can you post the error message you are getting? It is highly likely that you have errors in the onCreate() or onStart() method of the Activitys you are trying to start via the Menuitems. – Philipp Jahoda Aug 16 '13 at 13:22
  • Unfortunately, (App) has stopped. – user2689457 Aug 16 '13 at 13:25
  • Did you register the Activitys you are starting inside the Manifest file? Each new Activity needs to be registered inside the Manifest before it can be launched. Take a look here: http://stackoverflow.com/questions/3832619/add-a-new-activity-to-the-androidmanifest Furthermore, I see that your new Activity never calls setContentView(R.layout.yourlayout) so the WebView will be null and "myWebView.loadUrl("URL");" will result in a NullPointerException. – Philipp Jahoda Aug 16 '13 at 13:28
  • Yes, every is registered in the Manifest. – user2689457 Aug 16 '13 at 13:30
  • For example – user2689457 Aug 16 '13 at 13:30
  • Read my whole comment, you are not calling setContentView(...) :) – Philipp Jahoda Aug 16 '13 at 13:30
  • I am testing it. Thanks – user2689457 Aug 16 '13 at 13:33
  • THANK YOU VERY MUCH!! IT WORKED;) But it is showing activity_second and activity_impressum etc. instead of ConnectMe on the top bar? – user2689457 Aug 16 '13 at 13:36
  • You are welcome. If you want help on your "ConnectMe" problem, please open a new question and be more specific about it :) – Philipp Jahoda Aug 16 '13 at 13:38