Reputation: 1
i want to call function from subclass to main activity class. here is my source code : SMS.java
public class SMS extends ListActivity {
public void testerr(String kata) {
Toast.makeText(getBaseContext(), "test coyyyy="+kata, Toast.LENGTH_LONG).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
SMSReceiver.java
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
try {
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
SMS sss = new SMS(); ---> call the main class
sss.testerr("try the words"); ---> call method from main class
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
}
}
catch(NullPointerException ex){
Toast.makeText(context, "penyakite neng kene:"+ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
my program run after it receive new text message, and i always get NullPointerException when call that sss.testerr("my example words"); any clue guys? thankyou so much for your help
Upvotes: 0
Views: 1546
Reputation: 137332
You should not instantiate the Activity with it's constructor, either start it with an intent, or, if the Activity is already up, make it implement the broadcast receiver.
If you only want to show toast from your receiver, you can use the answer for this question
Upvotes: 1