-2
我正在編寫一個程序,它具有核心類的幾個事情和接口,允許使用不同參數調用核心函數。它給了我錯誤Nulljava.lang.NullPointerException
當我打電話 utilitariosprincipal
。Nulljava.lang.NullPointerException調用其他類的方法
我的代碼如下:
public class utilitariosprincipal extends Activity {
String nom="", num="", mai="";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.utilitarios);
interfacescontactos inte=new interfacescontactos();
contactos cont = new contactos(); //class where contacts are defined
num=inte.sel_num(cont.Meu); //this contains a String (telephone number)
core_sms sm=new core_sms();
try{
sm.EnviaSms(num,"Experiência", false);
}
catch(Exception e) {
Toast.makeText(this, e.getMessage()+e.toString(), Toast.LENGTH_LONG).show();
}
}
public class core_sms extends Activity {
private static final int TAMANHO_MAX_SMS = 160;
private static final int PORTA_SMS = 8091;
private static final String SMS_ENTREGUE = "SMS_DELIVERED";
private static final String SMS_ENVIADO = "SMS_SENT";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.utilitarios);
}
public void EnviaSms(String ntelefone,String mensagem,boolean isBinary) {
SmsManager gestor = SmsManager.getDefault();
PendingIntent piEnvio = PendingIntent.getBroadcast(this, 0, new Intent(SMS_ENVIADO), 0);
PendingIntent piEntregue = PendingIntent.getBroadcast(this, 0, new Intent(SMS_ENTREGUE), 0);
if(isBinary) {
byte[] dados = new byte[mensagem.length()];
for(int indice=0; indice<mensagem.length() && indice < TAMANHO_MAX_SMS; ++indice) {
dados[indice] = (byte)mensagem.charAt(indice);
}
gestor.sendDataMessage(ntelefone, null, (short) PORTA_SMS, dados, piEnvio, piEntregue);
} else {
int length = mensagem.length();
if(lengthTAMANHO_MAX_SMS) {
ArrayList<String> messagelist = gestor.divideMessage(mensagem);
gestor.sendMultipartTextMessage(ntelefone, null, messagelist, null, null);
} else {
gestor.sendTextMessage(ntelefone, null, mensagem, piEnvio, piEntregue);
}
}
}
}
core_sms是一項活動。您不應將其實例創建爲「core_sms sm = new core_sms();」 –
如果你想使用它而不創建任何對象,則將該方法設置爲靜態。 – SilentKiller