错误:类型alertFunction中的onCreate(Bundle)方法不适用于arguments()

时间:2014-03-13 07:58:43

标签: android sms

我是初学者,我在主要活动文件中收到了该错误。我希望你能解决我的问题。我试图为我的项目制作一个提醒信息发件人,我真的不知道如何纠正错误,因为我是新手。

MainActivity代码:

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Dialog;


public class MainActivity extends Activity

{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
timeAndDatePicker timeClass = new timeAndDatePicker();
alertFunction alertClass = new alertFunction();

/** Called when the activity is first created. */
@Override
public 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);

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(phoneNo, message);                
       else
           Toast.makeText(getBaseContext(), 
               "Please enter both phone number and message.", 
               Toast.LENGTH_SHORT).show();


       timeClass.setCurrentDateOnView();
       timeClass.addListenerOnButton();
       alertClass.onCreate(); <<---- THIS IS WHERE I GET THE ERROR.

   }


 private void sendSMS(String phoneNo, String message) {
   String SENT = "SMS_SENT";
   String DELIVERED = "SMS_DELIVERED";

   PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT), 0); // <--- THIS IS WHERE I GET THE ERROR

   PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(DELIVERED), 0); // <--- THIS IS WHERE I GET THE ERROR

   // ---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(phoneNo, null, message, sentPI, deliveredPI);
}

});
}

}

1 个答案:

答案 0 :(得分:0)

您无需明确致电onCreate()。一旦声明了一个类的实例,如果它具有onCreate()类,它将在适当的时候自动调用。

如果覆盖该类,请务必在其上面声明@Override语句,这将确保您覆盖正确的函数,因为如果不这样做,并且您没有指定正确的前一种方法具有的参数,它不会被调用,你会认为它。

此外,最好将super.onCreate(savedInstance)作为覆盖方法的第一行。

----编辑----

如上所述,在创建对象时调用onCreate(),即声明:alertFunction alertClass = new alertFunction();时。{/ p>

如果这是您想要多次调用的内容,请将内容放在类中的公共函数中,并在需要时从onCreate()和外部调用它。例如:

public void myFunction() {
  // Put here the current code of onCreate()
  ...
}

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

然后,如果您需要从课外再次拨打电话,请执行以下操作:

alertClass.myFunction();