views:

1061

answers:

5

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"&gt;
    <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)
A: 

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?

Christopher
Added stacktrace. It crashes when I press "Menu" while in the activity.
Portablejim
+1  A: 

replace the findViewById call with menu.findItem

Lance Nanek
Thanks. I went through another debug and realised the findViewById was not working and was not assigning mi_settings. I was looking for the right command.Thanks Heaps
Portablejim
A: 

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

pengwang
A: 

hey … i want to open up options menu by clicking upon my own created button … is it possible ? if so, please help me out … thanks in advance

sam
A: 

Also don't forget to say in the Manifest which classes are you going to use in order to implement intents ;).

Enrique Diaz