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 1039229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:04:26+00:00 2026-05-16T15:04:26+00:00

I have a beginners problem. Here is my situation: I want to start a

  • 0

I have a beginners problem. Here is my situation:

I want to start a new activity from the main activity. The code to launch the new activity is found in a separate class file. I seem to be passing the wrong arguments and I am ending up in a nullpointerexception when trying to launch the new activity. The new activity launches fine when I place the code in the main activity class file, therefore the second activity and the manifest are fine. Here is a sample of my code:

In my main activity class where I instanciate the second class (THIS IS MY MAIN ACTIVITY. I OMITTED THE REST BECAUSE I DO NOT THINK IT IS RELATED TO THE PROBLEM):

Tester mytest = new Tester();
mytest.test(this);

In my second class file (THIS IS NOT AN ACTIVITY; IT IS A CLASS THAT IS INSTANTIATED IN THE ACTIVITY):

public class Tester extends Activity {
     Intent myIntent;
     public void test (Context context) {
               myIntent = new Intent (Intent.ACTION_VIEW);
               myIntent.setClass(context, newActivity.class);
               thebutton.setOnClickListener(
            new OnClickListener() {  
                public void onClick(View v) { 
                    startActivity(myIntent);
                }  
            }       
        ):}

When I perform the click I receive a nullpointerexception at startactivity. Can anyone enlighten me on this please?I am sure that I am wrongly using the context.

  • 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-16T15:04:26+00:00Added an answer on May 16, 2026 at 3:04 pm

    Activities are started with Intents. Please read the Android Application Fundamentals first and try the Hello World app 🙂

    I understood that you will use your separate Tester class at all cost 😉 so I’m trying to adapt and help you out there.

    First of all, don’t let your class inherit from Activity. This won’t help you, cause this calls will probably not have any valid context. Activity somehow implements the template pattern, providing you key method like onCreate(...), onPause(...) etc and is instantiated by the Android OS.

    If you still want to use the class, you have to pass in the context. Probably you’re aiming for some MVC/MVP pattern structure, anyway.

    public class Tester {
        private Context context;
        public Tester(Context context){
            this.context = context;
        }
    
        public void test () {
           final Intent myIntent = new Intent(context, NewActivity.class);
    
           //guess this comes from somewhere, hope through a findViewById method
           thebutton.setOnClickListener(
                  new OnClickListener() {  
                    public void onClick(View v) { 
                        context.startActivity(myIntent);
                    }  
                  }       
            )};
        }
    }
    

    This would be a proposed solution from my side. A problem I still see here is on how you retrieve the button in that test() method. In order to have that work properly you have to retrieve it from some View class (with view.findViewByid(R.id.myButton)) or to create it dynamically and associate it with the view during the onCreate(...) of your Activity (probably using an Inflater).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this example on StackOverflow: var people = new List<Person> { new
I have a question which might be for beginners. I want to multiply a
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
I'm maintaining the Perl Beginners' Site and used a modified template from Open Source
I have some C# code which is using CSharpCodeProvider.CompileAssemblyFromSource to create an assembly in
I'm a beginner in Java programming language, and I have a problem that I
I am total beginner with Solr and have a problem with unwanted characters getting
I have a problem with compiling a program written in ocaml, the error appears
learning about loops (still a beginner) in VB.net. I have got the below code

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.