[英]android another class sendSMS and call in Mainactivity error
我想在MainActivity.java中調用sendSMS.java。我是初學者,我只是從該站點和Google復制粘貼代碼...但是當我想分別創建SendSMS類和調用方法sendSMS(message,phone)時,我得到了錯誤...
當我將sendSMS()放入MainActivity時工作正常...
MainActivity {
//some code here
SendSMS some = new SendSMS();
some.sendSMS(message,phone);
//some code here
}
和
public class SendSMS extends AppCompatActivity {
public void sendSMS(String message, String phone) {
String phoneNo = phone;
String message = message;
try {
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0, Intent arg1)
{
int resultCode = getResultCode();
switch (resultCode)
{
case Activity.RESULT_OK: {
Toast.makeText(getBaseContext(), "Message SENT!",Toast.LENGTH_LONG).show();
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("SMS_SENT"));
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(phoneNo, null, message, sentPI, null);
} catch (Exception e) {
e.printStackTrace();
}
}
我有這個錯誤:
W/System.err: java.lang.NullPointerException
W/System.err: at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
W/System.err: at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:478)
W/System.err: at android.app.PendingIntent.getBroadcast(PendingIntent.java:467)
W/System.err: at com.example.sendersms.SendSMS.sendSMS(SendSMS.java:84)
W/System.err: at com.example.sendersms.MainActivity.takeJSON(MainActivity.java:216)
W/System.err: at com.example.sendersms.MainActivity$1$1.run(MainActivity.java:90)
W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:136)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5001)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
切勿用new
實例化活動。 該實例不會被初始化為活動,並且不能用於您想要該活動的任何事物。
要修復您的代碼:
刪除extends AppCompatActivity
由於您的sendSMS()
方法需要一個Context
,請將其作為參數傳遞:
public void sendSMS(Context context, String message, String phone) { ...
在您的呼叫者中,傳遞Context
。 例如,在有效的Activity
:
sendSMS(this,
在需要Context
地方使用參數,例如
PendingIntent.getBroadcast(context, ...
自擴展AppCompatActivity以來,SendSMS是一項活動。 您必須在AndroidManifest.xml文件中注冊Activity類。 您不應該實例化一個活動。 您應該通過Intents與之對話。 Android會實例化Activity,以便它可以關聯Context和其他依賴項。 之所以發生NullPointerException,是因為您實例化了SendSMS,並且Android均未連接Activity的任何依賴項。
Laalto幫助我找到問題的解決方案...完整的答案是
在MainActivity類中
SendSMS smsclass = new SendSMS(this);
smsclass.method();
在課堂上SendSMS
private Context mContext;
public SendSMS() {
super();
}
protected SendSMS(Context context){
mContext = context;
}
我不知道這是如何工作的,但是對我有用
再次感謝laalto!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.