我遇到了这个未知的问题,我只是无法解决问题是,当我从 android 中的 sqlite 检索值时,它的工作正常我在两个类中做了同样的事情,它都很好地完成了我想要它做的事情,但是当我在一个方法它不起作用它给了我一个空指针异常它在除方法之外的所有地方都起作用当我在我的方法中传递参数时它给出错误并且我的应用程序关闭。
我的数据库在这里工作
public class Activity5 extends Activity {
public static String tableler;
final Context cont = this;
static String a1,a2,a3,a4,a5,a6,a7,a8;
static String out,out2,out3,out4,out5,out6,out7,out8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity5);
final EditText edt = (EditText)findViewById(R.id.replayedit);
Button btnsave = (Button) findViewById(R.id.btnSaveChange);
Button btncancel = (Button) findViewById(R.id.btnCancel);
SQLiteDatabase db = openOrCreateDatabase("ComDB", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = "+Activity3.replays, null);
c.moveToFirst();
out = c.getString(c.getColumnIndex("REPLAYS"));
db.close();
c.close();
if(Activity3.replays.contains("1"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("2"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("3"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("4"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("5"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("6"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("7"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("8"))
{
edt.setText("great"+out);
}
// this.vieww();
// this.edit();
btnsave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String updates = edt.getText().toString();
updater(Activity3.replays, updates);
Toast.makeText(cont, "Your Implementation complete",Toast.LENGTH_LONG ).show();
}
});
btncancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
public void updater(String sid,String vals)
{
String tableNb = "REPLAYS";
String tablela = "Rtm";
String ID = "ID";
SQLiteDatabase db = openOrCreateDatabase("ComDB", MODE_PRIVATE, null);
ContentValues cv = new ContentValues();
cv.put(tableNb, vals);
db.update(tablela, cv, ID+"="+sid, null);
// Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHE", selectionArgs)
db.close();
}
}
它在这里给出错误并关闭我的应用程序
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"))
{
// String replay = c.getString(c.getColumnIndex("REPLAYS"));
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}if(rep.equals("2"))
{
// String replay = c.getString(c.getColumnIndex("REPLAYS"));
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);
}
}
}
我的 BroadCast 类从中获取 sendGlass 的参数
public class BroadCastRecivers extends BroadcastReceiver{
static String from;
static String body;
@Override
public void onReceive(Context cont, Intent intent) {
// TODO Auto-generated method
// SmsManager smsManager = SmsManager.getDefault();
// TODO Auto-generated method s
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
from = str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
body = str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(cont, str, Toast.LENGTH_LONG).show();
// ret = str;
// int tos = Integer.valueOf(body);
// obj.Decision(from, tos);
// sms(from, body);
// String phoneNo = phone;
// String sms = Sms;
// SmsManager smsManager = SmsManager.getDefault();
// smsManager.sendTextMessage(phoneNo, null, sms, null, null);
/** Activity5 obj = new Activity5();
String rep = body.replace("from"+" :", "");
if(rep.equals("1"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("2"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("3"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("4"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("5"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("6"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("7"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("8"))
{
Activity3.replays = rep;
obj.select(from, rep);
}else
{
rep = "Sorry man";
obj.select(from, rep);
}
**/
from = "somehing";
body = "exception";
SendGlass obj = new SendGlass();
obj.sms(from, body);
} }
我的日志猫。
12-05 23:37:38.507: D/AndroidRuntime(267): Shutting down VM
12-05 23:37:38.507: W/dalvikvm(267): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-05 23:37:38.597: E/AndroidRuntime(267): FATAL EXCEPTION: main
12-05 23:37:38.597: E/AndroidRuntime(267): java.lang.RuntimeException: Unable to start receiver automator.mator.BroadCastRecivers: java.lang.NullPointerException
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.os.Looper.loop(Looper.java:123)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-05 23:37:38.597: E/AndroidRuntime(267): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 23:37:38.597: E/AndroidRuntime(267): at java.lang.reflect.Method.invoke(Method.java:521)
12-05 23:37:38.597: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-05 23:37:38.597: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-05 23:37:38.597: E/AndroidRuntime(267): at dalvik.system.NativeStart.main(Native Method)
12-05 23:37:38.597: E/AndroidRuntime(267): Caused by: java.lang.NullPointerException
12-05 23:37:38.597: E/AndroidRuntime(267): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-05 23:37:38.597: E/AndroidRuntime(267): at automator.mator.SendGlass.<init>(SendGlass.java:12)
12-05 23:37:38.597: E/AndroidRuntime(267): at automator.mator.BroadCastRecivers.onReceive(BroadCastRecivers.java:100)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
12-05 23:37:38.597: E/AndroidRuntime(267): ... 10 more
12-05 23:37:43.027: I/Process(267): Sending signal. PID: 267 SIG: 9
如果我使用硬编码字符串,我的方法有效,它只会在从数据库获取值时出错。