0

嗨,我正在使用服务读取收件箱,但是当我创建光标以读取收件箱时出现空指针异常以前我能够使用相同的代码读取短信现在我想将这些短信存储到 sd 卡中的 xml 文件中,但我得到了空指针异常。任何帮助将不胜感激。我的代码在这里

//这里输入代码

public class CreateBackupXml extends Service 
{
CreateBackup createBackup = null;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
public void onCreate(Bundle savedinstance)
{

    Log.d("service","inside on create of service");
}
public void onDestroy()
{

}
public void onStart(Intent intent, int startid) 
{
    Toast.makeText(this, "Creating Back up", Toast.LENGTH_LONG).show();
    Log.d("animation", "animation should be Start");
    createBackup = new CreateBackup();
    createBackup.createXml("all");
}

public class CreateBackup
{
    Uri uriSMSURI = Uri.parse("");
    Cursor cur=null;
    public void createXml(String choice)
    {
        if(choice=="all")
        {
            this.readSms("Inbox");
        }
    }
    public void readSms(String folder)
    {

        if(folder.equals("inbox"))
        {
            uriSMSURI = Uri.parse("content://sms/inbox");
        }
        else if(folder.equals("sent"))
        {
                uriSMSURI = Uri.parse("content://sms/sent");
        }
        try{
            Log.d("folder","inside of inbox");
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            Log.d("checking","1111111111111");
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Log.d("checking","2222222222");
            Document document = documentBuilder.newDocument();
            Log.d("checking","333333333333");
            Element rootElement = document.createElement("SMSBackUp");
            Log.d("checking","444444444");
            rootElement.setAttribute("category", folder);
            Log.d("checking","5555555555");
            rootElement.setAttribute("owener", "gaurav");      
            Log.d("checking","6666666666");             
            cur = getContentResolver().query(uriSMSURI, null, null, null,null);
            Log.d("checking",""+cur);
            Log.d("checking","7777777777777");
            while (cur.moveToNext()) 
            {
                Log.d("checking","88888888");
                Element smsElement = document.createElement("sms");
                rootElement.appendChild(smsElement);

                Element text = document.createElement("text");
                smsElement.appendChild(text);
                text.appendChild(document.createTextNode(cur.getString(11)));

                Element phonenoElement = document.createElement("number");
                smsElement.appendChild(phonenoElement);
                phonenoElement.appendChild(document.createTextNode(cur.getString(2)));

                Element nameElement = document.createElement("author");
                smsElement.appendChild(nameElement);
                nameElement.appendChild(document.createTextNode(getContact(cur.getString(2))));                 
            }
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            Properties outFormat = new Properties();
            outFormat.setProperty(OutputKeys.INDENT, "yes");
            outFormat.setProperty(OutputKeys.METHOD, "xml");
            outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            outFormat.setProperty(OutputKeys.VERSION, "1.0");
            outFormat.setProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperties(outFormat);
            DOMSource domSource = new DOMSource(document.getDocumentElement());
            OutputStream output = new ByteArrayOutputStream();
            StreamResult result = new StreamResult(output);
            String xmlString = output.toString();
            Log.d("output xml",xmlString);

        }
        catch(Exception e)
        {
            Log.e("execption ","can not create back up" +e);
        }
    }
    private String getContact(String number)
    {

        Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
        String returnName= "";
        if(cur.getCount() > 0)
        {
            while(cur.moveToNext())
            {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  Log.d("Contact","ID:" + id );
                  String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  Log.d("Contact","name:" + name);
                  if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                  {
                      Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                      ContactsContract.CommonDataKinds.Phone.NUMBER + "=?",new String[]{number},null);
                      int numindex = pcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);
                      if(pcur.moveToFirst())
                      {
                          String dbNum = pcur.getString(numindex);
                          if(dbNum.equals(number))
                          {
                              returnName = name;
                              Log.d("number","number:" + dbNum);
                          }
                          else 
                          {
                              Log.d("number","numbers dont match!");
                          }
                      }
                      else 
                      {
                          Log.d("number", "no result");
                      }
                  }
                  else 
                  {
                       Log.d("number", "No Number");
                  }
            }
        }
        return returnName;
    }

}

}

日志猫视图

04-05 12:00:07.770: D/reading sms(456): inside of create backup for inbox
04-05 12:00:07.830: D/animation(456): animation should be Start
04-05 12:00:07.830: D/folder(456): inside of inbox
04-05 12:00:07.858: D/checking(456): 1111111111111
04-05 12:00:07.860: D/checking(456): 2222222222
04-05 12:00:07.881: D/checking(456): 333333333333
04-05 12:00:07.890: D/checking(456): 444444444
04-05 12:00:07.920: D/checking(456): 5555555555
04-05 12:00:07.920: D/checking(456): 6666666666
04-05 12:00:07.920: D/checking(456): null
04-05 12:00:07.920: D/checking(456): 7777777777777
04-05 12:00:07.920: E/execption(456): can not create back upjava.lang.NullPointerException
4

1 回答 1

0

//因为区分大小写,您的条件已归档

将收件箱更改为收件箱

或尝试

equalsIgnoreCase()

      {
            this.readSms("inbox");
 Log.d("entering into ","inbox");
        }
    }
    public void readSms(String folder)
    {

        if(folder.equals("inbox"))

uriSMSURI = Uri.parse("content://sms/inbox");
}
于 2012-04-05T07:19:34.800 回答