Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity"

Tags:

java

android

private int pos = position;
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show(); 
                        iBean = (InboxBean)result.get(position);
                        ConstantData.inbox_subject = iBean.subject;
                        ConstantData.inbox_body = iBean.body;
                        ConstantData.inbox_postDate = iBean.postdate;

                        startActivity(new Intent(InboxActivity.this,InboxDetailActivity.class));//getting error at startActivity 
                    }
            });

I am getting the following error at startActivity line: "Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity"

Please help.

like image 653
shripal Avatar asked May 03 '26 16:05

shripal


2 Answers

I assume you have this in the inner static class of your activity. Don't do that. OnClickListeners should exist in the context of the particular instance of activity.

like image 68
Vladimir Ivanov Avatar answered May 05 '26 06:05

Vladimir Ivanov


To make static reference to non static methods, you need to call them by using the context of the activity or application.

This is also true when you need to call activity from non activity class.

The following type of code can be used

Intent cc=new Intent(yourcontext,Yourpackagename.ClassName.class);
yourcontext.startActivity(cc);
like image 24
Jaldip Katre Avatar answered May 05 '26 06:05

Jaldip Katre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!