0

我遇到了这个未知的问题,我只是无法解决问题是,当我从 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

如果我使用硬编码字符串,我的方法有效,它只会在从数据库获取值时出错。

4

2 回答 2

0

您还没有使用 SendGlass 中的任何 Activity 方法...

  • 必须通过调用启动 ActivitystartActivity(Intent)
  • 必须等到onCreate()被调用才能使用 Activity 的上下文
  • 你不能打电话sms()new SendGlass().sms(phone, sms)

否则,您会在 ContextWrapper 类中看到这些 NullPointerExceptions 被抛出。

例如,SendGlass应该看起来更像这样:

public class SendGlass extends Activity{
    static String replay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if(intent != null) {
            sms(intent.getStringExtra("phone", ""), intent.getStringExtra("sms", ""));
        }
    }
    ...    

另外
,我看到您添加了 BroadcastReceiver 代码。就像我猜想你正在尝试通过以下方式启动你的活动:

SendGlass obj = new SendGlass();
obj.sms(from, body);

正如我所说,您必须使用 Intents 和startActivity()

Intent intent = new Intent(cont, SendGlass.class);
intent.putExtra("phone", from);
intent.putExtra("sms", body);
cont.startActivity(intent);
于 2012-12-05T19:16:26.350 回答
0

您对 openOrCreateDatabase() 的调用看起来搞砸了。我没有看到使用这些类型的方法的任何签名。我在 javadoc 中看到的是这三个签名:

  • openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler)
  • openOrCreateDatabase(字符串路径,SQLiteDatabase.CursorFactory 工厂)
  • openOrCreateDatabase(文件文件,SQLiteDatabase.CursorFactory 工厂)

这些都不符合您的用法,这似乎是 openOrCreateDatabase(path path, int mode, Object)

我还注意到您尝试使用数据库变量“db”而没有先检查它是否包含某些内容。这不是好的编程习惯。我从不认为打开的文件或数据库有效。一个永远不知道。

此外,您试图在光标“c”上调用 c.moveToNext() 而不首先验证 c 是否存在并包含一行。这绝对不是好的编程习惯。始终测试 c != null,并测试 c.moveToFirst == true。这确保了数据库调用有效(即,它没有返回 null)并且您得到了返回行(c.MoveToFirst 有效)。

于 2012-12-05T19:26:36.723 回答