Maya
Maya

Reputation: 1412

Error starting Activity from a Class on Android

I have the following class SubMenuToolBar which I usually instantiate from an activity (MainActivity) that passes itself (this) in the constructor upon instantiation:

public class SubMenuToolBar extends Activity {
        private android.support.v7.widget.Toolbar mToolbar;
        private Activity mActivity;
        RelativeLayout mLayout;

public SubToolbar(android.support.v7.widget.Toolbar toolbar, Activity activity) {
            mActivity = activity;
            mToolbar = toolbar;
            mLayout = (RelativeLayout) mToolbar.findViewById(R.id.layout_toolbar);

            mLayout.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(mActivity, UserMessagesActivity.class);
                    startActivity(intent);
                }
            });

    }

MainActivity.cs

protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    new SubMenuToolBar ((android.support.v7.widget.Toolbar) findViewById(R.id.sub_toolbar), this);
    }

Everytime I click on the element registered with the click event handler (in the activity) I get the popular error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference

11-20 08:31:33.422 23802-23802/com.xxx.apps.xxx E/AndroidRuntime:     at android.app.Activity.startActivityForResult(Activity.java:3918)
11-20 08:31:33.422 23802-23802/com.xxx .apps.xxx E/AndroidRuntime:     at android.app.Activity.startActivityForResult(Activity.java:3877)
11-20 08:31:33.422 23802-23802/com.xxx.apps.xxx E/AndroidRuntime:     at android.app.Activity.startActivity(Activity.java:4200)
11-20 08:31:33.422 23802-23802/com.xxx.apps.xxx  E/AndroidRuntime:     at android.app.Activity.startActivity(Activity.java:4168)
11-20 08:31:33.422 23802-23802/com.xxx.apps.xxx E/AndroidRuntime:     at com.xxx.apps.xxx.SubMenuToolBar$1.onClick(SubToolbar.java:28)

I have tried using the Activity in the Intent, the Context of the activity, getApplicationContext, and getBaseContext. All throws the same error on line:

 Intent intent = new Intent(mActivity, MessagesActivity.class);

I have checked SO and various web resource where most of them point to the activity need to be passed to the class so it's Context is used in the Intent, I have done that as you can see above and made sure all details of the calling Activity is passed and used, the problem is still there.

Any idea what I'm doing wrong here?

Upvotes: 0

Views: 3414

Answers (2)

Héctor
Héctor

Reputation: 26054

You are a little confused. Activity must not be instantiated with new. You have to start it using Intent. There is two activities but I think you only need one.

public class SubMenuToolBar extends Activity {

    RelativeLayout mLayout;

    @Override
    protected void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.your_activity_layout);

        android.support.v7.widget.Toolbar mToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.sub_toolbar);

        mLayout = (RelativeLayout)mToolbar.findViewById(R.id.layout_toolbar);

        mLayout.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(mActivity, UserMessagesActivity.class);
            startActivity(intent);
        });
}

And use SubMenuToolBar as your main activity.

Upvotes: 0

walkmn
walkmn

Reputation: 2391

try replace this:

Intent intent = new Intent(mActivity, UserMessagesActivity.class);
startActivity(intent);

to this:

Intent intent = new Intent(mActivity, UserMessagesActivity.class);
mActivity.startActivity(intent);

Upvotes: 1

Related Questions