MainActivity activity=new MainActivity();
Never instantiate activities with new
. They won't be good for anything you'd want an activity for.
Specifically in your case: the lifecycle method onCreate()
won't get run for that activity object and the TextView
reference there is null
.
If you really need to launch an activity, use an Intent
.
If you just need to get a reference to your activity, pass it around as an argument, e.g.
void methodThatNeedsActivity(MainActivity activity) {
activity.someMethod();
}
// call from MainActivity
otherObject.methodThatNeedsActivity(this);