2014-03-13 62 views
1

我是初學者,在主活動文件中出現該錯誤。我希望你能幫我解決我的問題。我正在嘗試爲我的項目製作提醒郵件發件人,我真的不知道如何更正錯誤,因爲我是新手。錯誤:類型alertFunction中的onCreate(Bundle)方法不適用於參數()

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); 
} 

}); 
} 

} 

回答

0

你並不需要調用onCreate()明確地。一旦你聲明瞭一個類的實例,如果它有onCreate()類,它將在適當的時候自動調用。

如果您覆蓋該類,請務必在其上面聲明@Override語句,這將確保您覆蓋正確的函數,因爲如果您不這樣做,並且未指定正確的參數以前的方法,它不會被調用,你會認爲它的確如此。

此外,最好將super.onCreate(savedInstance)作爲您的重寫方法的第一行。

---- ---- EDIT

正如上面所說的,onCreate()上調用對象創建,即,當聲明:alertFunction alertClass = new alertFunction();

如果您想要多次撥打這些電話,請將內容放入班級的公共功能中,並在需要時從外部呼叫onCreate()。例如:

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

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

,然後如果你需要在類的外部再次調用它,請執行下列操作:

alertClass.myFunction(); 
+0

我應該用什麼代碼代替alertClass呢? – Chix

+0

查看我的編輯,我爲此添加了一個示例。 – nKn

相關問題