I am working on an SMS application and I have a method in my MainActivity
to perform a button click:
public void updateMessage() {
ViewMessages.performClick();
}
This method works fine and performs the button click when I call this method from inside the MainActivity
class.
But, when I call this method from any other class as shown below, where I call the Main Activity's updateMessage
method from the IntentServiceHandler
class, I am getting a NullPointerException
:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.Button.performClick()' on a null object reference
public class IntentServiceHandler extends IntentService {
public IntentServiceHandler() {
super("IntentServiceHandler");
}
@Override
protected void onHandleIntent(Intent intent) {
String message = intent.getStringExtra("message");
TransactionDataBase transactionDB = new TransactionDataBase(this, 1);
transactionDB.addMessage(message);
MainActivity mainActivity = new MainActivity();
mainActivity.updateMessage();
}
}
How can I handle this?
Edit: I even tried to make the updateMessage method static, and now i get the following exception
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.