Java 无法在方法()中运行SqLite;

Java 无法在方法()中运行SqLite;,java,android,windows,oop,Java,Android,Windows,Oop,我有一个未知的问题,我无法解决的问题是,当我在android中从sqlite中检索值时,它工作正常,我在两个类中做了相同的事情,它完全正常,做了我希望它做的事情,但当我在一个方法中这样做时,它不工作,它给我一个空指针异常,它在任何地方都工作,除了在方法中,当我通过时参数在我的方法中给出错误,我的应用程序关闭 我的数据库在这里工作 public class Activity5 extends Activity { public static String tableler; final Co

我有一个未知的问题,我无法解决的问题是,当我在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);
        }
    }

}  
从中获取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);
公共类BroadcastReceiver扩展了BroadcastReceiver{
来自的静态字符串;
静态弦体;
@凌驾
接收时公共无效(上下文控制,意图){
//TODO自动生成方法
//SmsManager SmsManager=smsmsmanager.getDefault();
//TODO自动生成的方法s
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];

对于(int i=0;i您没有使用SendGlass中的任何活动方法

  • 您必须通过调用
    startActivity(Intent)
  • 必须等到调用了
    onCreate()
    之后,才能使用活动的上下文
  • 您不能使用
    new SendGlass()调用
    sms()
    。短信息(手机、短信息)
否则,您将看到在ContextWrapper类中抛出这些NullPointerException

例如,
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);
正如我所说,您必须使用意图和开始触觉()


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

  • openOrCreateDatabase(字符串路径,SQLiteDatabase.CursorFactory工厂,DatabaseErrorHandler errorHandler)
  • openOrCreateDatabase(字符串路径,SQLiteDatabase.CursorFactory)
  • openOrCreateDatabase(文件,SQLiteDatabase.CursorFactory)
这些都不符合您的用法,这似乎是 openOrCreateDatabase(路径、int模式、对象)

我还注意到,您试图使用数据库变量“db”,而没有首先检查它是否包含某些内容。这不是良好的编程实践。我从不认为打开的文件或数据库有效;人们永远不知道


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

SendGlass类中的第12行是什么?我该如何实现它?我正在从一个类中获取参数如果您询问如何传递两个字符串参数,您应该使用
putExtra()传递内部意图
并像我所展示的那样阅读它们。是否需要扩展活动类?如果不打算使用任何活动的方法,请将声明更改为
public class SendGlass{
并在构造函数
public SendGlass(上下文)中传递有效上下文{
。您在错误的类中查找,LogCat显示代码进入正常状态。问题是活动的上下文
mBase
为空。
SendGlass obj = new SendGlass();
obj.sms(from, body);
Intent intent = new Intent(cont, SendGlass.class);
intent.putExtra("phone", from);
intent.putExtra("sms", body);
cont.startActivity(intent);