How to open new view (call an activity) from options menu defined in XML? (android)

I cant seem to open a new view from an options menu item. The program keeps crashing as it applies the intent and listener to the item. I am just beginning, so please be nice.

The current view is mnfsms, and the view I am trying to open is mnfsms_settings. I am developing for 1.5.

Could someone please help me get the menu working.

The menu (called options_menu.xml):

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/settings_button"
          android:title="Settings"
          android:icon="@android:drawable/ic_menu_preferences" />
    <item android:id="@+id/about_button"
          android:title="About"
          android:icon="@android:drawable/ic_menu_myplaces" />
</menu>

The main view (called mnfsms.java):

package com.example.mnfsms;

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

public class mnfsms extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /*        OnClickListener myocl = new View.OnClickListener() {
 public void onClick(View v){
  Intent myi = new Intent(mnfsms.this, mnfsms_settings.class);
  startActivity(myi);
 }
};*/    
}

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    MenuItem mi_settings = (MenuItem)findViewById(R.id.settings_button);
    mi_settings.setIntent(new Intent(this, mnfsms_settings.class));

    return true;
}
}

The manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.mnfsms"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".mnfsms" android:label="@string/main_window_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
      <activity android:name=".mnfsms_settings" android:label="string/main_window_name">
            </activity>
        </application>
        <uses-sdk android:minSdkVersion="3" />
</manifest>

The stacktrace:

01-06 15:07:58.045: ERROR/AndroidRuntime(2123): Uncaught handler: thread main exiting due to uncaught exception
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): java.lang.NullPointerException
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at com.example.mnfsms.mnfsms.onCreateOptionsMenu(mnfsms.java:30)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at android.app.Activity.onCreatePanelMenu(Activity.java:2038)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:421)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:664)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1278)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1735)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2188)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2158)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1490)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at android.os.Looper.loop(Looper.java:123)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at android.app.ActivityThread.main(ActivityThread.java:3948)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at java.lang.reflect.Method.invoke(Method.java:521)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123):     at dalvik.system.NativeStart.main(Native Method)


Asked by: Brad712 | Posted: 24-01-2022






Answer 1

replace the findViewById call with menu.findItem

Answered by: Anna565 | Posted: 25-02-2022



Answer 2

Your code and XML looks about right. However you're not calling super.onCreateOptionsMenu() at the start of your method.

Can you post the stack trace you get when the app crashes? At what point does it crash? When you press Menu while in this Activity?

Answered by: Adelaide363 | Posted: 25-02-2022



Answer 3

you can use MenuItem mi_settings = menu.getItem(0) not MenuItem mi_settings = (MenuItem)findViewById(R.id.settings_button); it is work find.

Answered by: Sawyer945 | Posted: 25-02-2022



Similar questions

Android: calling a method in an activity from a service (on same Activity)

I've had a similar question but the answer did not entirely fix my problem. Here is what I am trying to do: I have a Service class that loops to receive TCP connections. If a connection is received, a string can be read. It can be one of those two: "START" or "STOP". When "START" comes in, I create (start) a new activity which has some GUI, and also two threads (one sends UDP packets and records audio, the ...


android - Problem ending my game (UI Thread won't let me open a new Activity)

I'm having trouble tying off my program (ending it up). I have a SurfaceView in an Activity, and in that SurfaceView when you lose three lives the game is supposed to end. I have tried opening up a new Activity but I get a keyDispatchingTimedOut error. I know it has to do with my UIThread. I have been told to create a handler in my activity and then accessible to my surfaceview. Then I can send a message to my handler and ...


android - Alert Dialog outside of an App (not in an Activity)

Is there a way to create some sort of popup window like an Alert Dialog, outside of an App? The context of this question is I have a need to display something to the user in the event of a Push Notification. Basically, a user receives some message, the App receives it even if it's not currently open, and a notification appears in the user's task bar. If the user opens the notification in their task bar, I want a popup to a...


android - Get E-Mail, ID's and Phone from Contact (not using activity)

My class doesn't have extends Activity or onCreate() method. So pass the context parameter from the class which extends Activity to this class: public static void getContactNumbers(Context context) { String contactNumber = null; int contactNumberType = Phone.TYPE_MOBILE; String nameOfContact = null; ContentResolver cr = context.getContentResolver(); ...


android - How to call widget on the Activity (Widget calling from Activity)

I am new to android widget.I did sample application , that want to show time .How to call widget from activity: I tried like this but It say Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.weather/com.weather.CurrentCity}; have you declared this activity in your AndroidManifest.xml? &lt;manifest xmlns:android="http://schemas.andr...


android - simple listview (not activity) example?

I've been doing this for a few months now and I've found it almost impossible to find an example where the ListView is a ListView not a ListActivity. In my program all I'm wanting to do is have half the screen a ListView and the other half something else so the ListView can be moved around in the layout. Does anyone know any such examples?


android - How to switch between 2 view (map and some activity) and both work together?

My app is running recording. If i wanna make 2 page views; 1) record running, when swipe to right it show 2nd page which is map (map also work accompany with recording activity) in addition, when we are at map view, recording activity still work in background. Which method or function i should look at? now, I have study some about PageAdapter, but, i still have no idea how to make 2 different page...


android - Trouble putting a Listview in a TabHost (as a view, not an activity)

I'm trying to insert a ListView as one of the tabs in a tabhost. I've scoured the web and found several implementations that say they work, but don't for me (either in their own projects or when incorporated into mine). I've settled on the following, which works (as far as not crashing) and I'm not getting any errors in LogCat, but the tab appears empty. I've checked the array supplying the list (tooldis...


Invoke routine in class from other class (not activity) in JAVA for Android

I have ; - MainGamePanel (Extends SurfaceView) - Button (class) - Fruitmanager (class) - Apple (class) In MainGamePanel, I check for MotionEvent.ACTION_DOWN I hand it over to the button. Button checks coordinates and sets Touched boolean to true. In MainGamePanel, I check for MotionEvent.ACTION_UP If button Touched = true, I hand it over to the button. Button checks coordinates and...


java - How to show Android Toast message (run other Activity) in libGDX?

I read this tutorial and using code from it to implement ScoreLoop service in my Game. And submiting score working fine, but show other android activity or simple Toast message are crashes with NullPointer error. Crashes in line: Intent intent = new Intent(mContext, EntryScreenActivity.class);






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top