Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4623184
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:57:00+00:00 2026-05-22T02:57:00+00:00

I’m calling from one class another class. I can set values and get values

  • 0

I’m calling from one class another class. I can set values and get values when created an object (see Log) but there is lots of methods which throw an exception (see log as well). For example toasting something. If i put the code to toast something in the original class it works, if i try to call it, it doesnt work. In my code below these things which do not work are commented out (// code...).

Here is the Main.class:

public class Main extends Activity {
private MyMenu myMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myMenu = new MyMenu ();

    myMenu.setMyMenu (3, "some text");
    int number = myMenu.getMyMenuNumber();
    String text = myMenu.getMyMenuString();

    Log.d("LOG", "number is " + number + " and text is '" + text + "'.");

    try {
        Log.d ("LOG", "startMain error");
        //myMenu.startMain();
        startActivity(new Intent (this, Page1.class));
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    try {
        Log.d ("LOG", "putToast error");
        //myMenu.putToast();
        Toast.makeText(
                this, 
                "This is a toast", 
                Toast.LENGTH_LONG)
                .show();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    try {
        Log.d ("LOG", "inflateMenu error");
        //myMenu.inflateMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    try {
        Log.d ("LOG", "isOptionsButtonClicked error");
        //myMenu.isOptionsButtonClicked(item);
        switch (item.getItemId()) {     
        case R.id.feedback:
            startActivity(new Intent (this, Page1.class));
            break;
        case R.id.more_information:
            startActivity(new Intent (this, Page1.class));
            break;
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return super.onOptionsItemSelected(item);
}
}

and this is MyMenu.class:

public class MyMenu extends Activity {
    int number;
    String text;

public MyMenu() {

}
public void setMyMenu (int number, String text) {
    this.number = number;
    this.text = text;
}
public int getMyMenuNumber () {
    return number;
}
public String getMyMenuString () {
    return text;
}

public void startMain () {
    startActivity(new Intent (this, Page1.class));
}

public void inflateMenu (Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
}
public void putToast () {
    Toast.makeText(
            this, 
            "This is a toast", 
            Toast.LENGTH_LONG)
    .show();
}
public void isOptionsButtonClicked (MenuItem item) {
    switch (item.getItemId()) {     
    case R.id.feedback:
        startActivity(new Intent (this, Page1.class));
        break;
    case R.id.more_information:
        startActivity(new Intent (this, Page1.class));
        break;
    }
}
}

To stress it out again, running everything in Main.class works perfectly but calling the other classes doesnt work.

UPDATE

Here is parts of the Log/StackTrace:

05-15 08:13:13.502: DEBUG/LOG(546): inflateMenu error
05-15 08:13:13.513: WARN/System.err(546): java.lang.NullPointerException
05-15 08:13:13.522: WARN/System.err(546):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
05-15 08:13:13.522: WARN/System.err(546):     at android.view.MenuInflater.inflate(MenuInflater.java:77)
05-15 08:13:13.522: WARN/System.err(546):     at com.test.MyMenu.inflateMenu(MyMenu.java:34)
05-15 08:13:13.533: WARN/System.err(546):     at com.test.Main.onCreateOptionsMenu(Main.java:52)
05-15 08:13:13.542: WARN/System.err(546):     at android.app.Activity.onCreatePanelMenu(Activity.java:2123)
05-15 08:13:13.542: WARN/System.err(546):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:305)
05-15 08:13:13.542: WARN/System.err(546):     at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:550)
05-15 08:13:13.582: WARN/System.err(546):     at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1192)
05-15 08:13:13.582: WARN/System.err(546):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1636)
05-15 08:13:13.582: WARN/System.err(546):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2368)
05-15 08:13:13.582: WARN/System.err(546):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2338)
05-15 08:13:13.582: WARN/System.err(546):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1641)
05-15 08:13:13.582: WARN/System.err(546):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 08:13:13.592: WARN/System.err(546):     at android.os.Looper.loop(Looper.java:123)
05-15 08:13:13.592: WARN/System.err(546):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-15 08:13:13.602: WARN/System.err(546):     at     java.lang.reflect.Method.invokeNative(Native Method)
05-15 08:13:13.602: WARN/System.err(546):     at java.lang.reflect.Method.invoke(Method.java:521)
05-15 08:13:13.602: WARN/System.err(546):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-15 08:13:13.602: WARN/System.err(546):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-15 08:13:13.602: WARN/System.err(546):     at dalvik.system.NativeStart.main(Native Method)

and:

05-15 08:13:03.592: DEBUG/LOG(546): putToast error
05-15 08:13:03.612: WARN/System.err(546): java.lang.NullPointerException
05-15 08:13:03.621: WARN/System.err(546):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
05-15 08:13:03.633: WARN/System.err(546):     at android.widget.Toast.<init>(Toast.java:89)
05-15 08:13:03.633: WARN/System.err(546):     at android.widget.Toast.makeText(Toast.java:231)
05-15 08:13:03.633: WARN/System.err(546):     at com.test.MyMenu.putToast(MyMenu.java:37)
05-15 08:13:03.641: WARN/System.err(546):     at com.test.Main.onCreate(Main.java:37)
05-15 08:13:03.641: WARN/System.err(546):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-15 08:13:03.652: WARN/System.err(546):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-15 08:13:03.652: WARN/System.err(546):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-15 08:13:03.652: WARN/System.err(546):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-15 08:13:03.661: WARN/System.err(546):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-15 08:13:03.661: WARN/System.err(546):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 08:13:03.671: WARN/System.err(546):     at    android.os.Looper.loop(Looper.java:123)
05-15 08:13:03.671: WARN/System.err(546):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-15 08:13:03.671: WARN/System.err(546):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 08:13:03.683: WARN/System.err(546):     at java.lang.reflect.Method.invoke(Method.java:521)
05-15 08:13:03.683: WARN/System.err(546):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-15 08:13:03.691: WARN/System.err(546):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-15 08:13:03.691: WARN/System.err(546):     at dalvik.system.NativeStart.main(Native Method)

hope that helps!

UDATE 2

Here is the requested Page1.class. But again everything runs fine when i do not use the MyMenu.class.

public class Page1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page1);
}

}

Furthermore you can download the android project from microsoft’s/windows live skydrive:
http://cid-f45ce92851885387.office.live.com/browse.aspx/Stackoverflow

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:57:01+00:00Added an answer on May 22, 2026 at 2:57 am

    I can’t figure out why you’re delegating this method below to other activity:

    public void startMain () {
       startActivity(new Intent (this, Page1.class)); // this <- here is problem
    }
    

    I suppose that’s because of bad desing. Don’t do that. Use Intents. Pass data between Activities in Intents in Bundles.
    In your case you didn’t pass Context to MyMenu Activity. MyMenu hasn’t got instance of Application Context therefore you’re getting NPE.

    response for comment:
    You can do static startMain method, of course. But pay attention to what is ‘this’ in new Intent(this, Page1.class). You should pass Context of Activity from which you’re starting the new Activity.

    public static void startMain (Activity context) {
       context.startActivity(new Intent (context, Page1.class));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.