[英]Database access gives a java.null pointer exception
我創建了我的第一個android應用。 一切正常,除了一個問題。 當我在應用程序中打開數據庫時。 在我的oncreate活動中,它運行良好。 但是,當我使用相同的代碼來檢索方法中的值時,我的應用程序崩潰並給出了java.null pointer exception
。
所以我的問題是,有沒有一種方法打開數據庫或我做錯事的另一種方式。 這是我的代碼。
在這里,我的方法給出了java.null指針異常。
public class SendGlass extends Activity{
static String replay;
public void sms (String phone,String Sms )
{
SmsManager smsManager = SmsManager.getDefault();
String phoneNo = phone;
String sms = Sms;
String api = "You Entered one";
String api2 = "You Entered two";
String api3 = "Nothing great";
String rep = sms.replace(phoneNo+" :", "");
SQLiteDatabase db = openOrCreateDatabase("ComDB", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = 1", null);
while(c.moveToNext())
{
replay = c.getString(c.getColumnIndex("REPLAYS"));
if(rep.equals("1"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
if(rep.equals("2"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
if(rep.equals("3"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
if(rep.equals("4"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
if(rep.equals("5"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
if(rep.equals("6"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
if(rep.equals("7"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
if(rep.equals("8"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
}
}
我通過以下方式將此代碼提供參數
class obj = new Class();
obj.sms (value,value )
嘗試這個
c.moveToFirst():
做{您的代碼/////
} while(c.moveToNext)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.