2016-10-16 22 views
1

私は基本的なSMSアプリケーションを開発しようとしていますが、NullPointerExceptionの問題があります。 まあ、コードがある:Android SMS BroadcastReceiver NullPointer

MainActivity.java

public class MainActivity extends AppCompatActivity { 

Button btnSendSMS; 
EditText txtPhoneNo; 
EditText txtMessage; 
sendSMS sendSMS=new sendSMS(); 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); 
    txtMessage = (EditText) findViewById(R.id.txtMessage); 
    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},1); 

    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      String phoneNo = txtPhoneNo.getText().toString(); 
      String message = txtMessage.getText().toString(); 
      if (phoneNo.length()>0 && message.length()>0) { 
       sendSMS.sendSMS(phoneNo, message,getApplicationContext()); 

      } 
      else 
       Toast.makeText(getApplicationContext(), 
         "Please enter both phone number and message.", 
         Toast.LENGTH_SHORT).show(); 
     } 
    }); 
}} 

そしてsendSMSクラスがあります:

public class sendSMS extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
} 


//---sends an SMS message to another device--- 
public void sendSMS(String phoneNumber, String message,Context context) 
{ 
    String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 

    PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, 
      new Intent(SENT), 0); 

    PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, 
      new Intent(DELIVERED), 0); 

    //---when the SMS has been sent--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        Toast.makeText(getBaseContext(), "SMS sent", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
        Toast.makeText(getBaseContext(), "Generic failure", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_NO_SERVICE: 
        Toast.makeText(getBaseContext(), "No service", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_NULL_PDU: 
        Toast.makeText(getBaseContext(), "Null PDU", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_RADIO_OFF: 
        Toast.makeText(getBaseContext(), "Radio off", 
          Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
    }, new IntentFilter(SENT)); 

    //---when the SMS has been delivered--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        Toast.makeText(getBaseContext(), "SMS delivered", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case Activity.RESULT_CANCELED: 
        Toast.makeText(getBaseContext(), "SMS not delivered", 
          Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
    }, new IntentFilter(DELIVERED)); 

    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 
}} 

はまた、エラーに関するログがあります:

FATAL EXCEPTION: main 
       Process: com.example.lcssgml.appsmsmms, PID: 5861 
       java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)' on a null object reference 
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:586) 
        at com.example.lcssgml.appsmsmms.sendSMS.sendSMS(sendSMS.java:41) 
        at com.example.lcssgml.appsmsmms.MainActivity$1.onClick(MainActivity.java:46) 
        at android.view.View.performClick(View.java:5610) 
        at android.view.View$PerformClick.run(View.java:22260) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

私は問題を理解することはできません。あなたが私を助けることを願っています! Thx

答えて

1

sendSMSクラスをActivityサブクラスにすると、おそらくregisterReceiver()メソッドが解決されます。 newActivityをインスタンス化することはできません。正しく動作させることはできません。 Contextメンバは正しく初期化されません。そのため、NullPointerExceptionが得られています。

すでにContextsendSMS()メソッドに渡しているので、registerReceiver()に電話することができます。

context.registerReceiver(...); 

さらに、sendSMSクラスがActivityサブクラスではありませんので、あなたはextends Activity、およびonCreate()オーバーライドを削除する必要があります。また、sendSMS()メソッドはstaticになりました。したがって、クラスのインスタンスを作成する必要はなく、クラスで直接メソッドを呼び出すこともできます。また、Javaのクラス名は大文字で始める必要があります。

public class SendSMS { 

    public static void sendSMS(...) { 
     ... 
    } 
    ... 
} 

それを呼び出すには:

SendSMS.sendSMS(...); 

レシーバの登録を解除することをお勧めしますがContext#unregisterReceiver()を使用して、彼らと一緒に行われているとき。匿名のBroadcastReceiverインスタンスを使用しないことで、これを簡単に行うことができます。

また、使用しているアルファベットの単一部分のメッセージの文字制限を超えるメッセージを送信すると、通常SmsManager#sendTextMessage()メソッドが自動的に失敗することを指摘しておきます。

+1

Thx、私はあなたの答えをありがとう!あなたの役に立つ記述で私は問題を解決しました。 –