0

我在片段中有一个广播接收器,它正在更新数据库,它正在从服务之一运行数据。在广播接收器中,我首先更新数据库,即使片段未打开,它也会正确更新,然后更新列表仅当该类处于活动状态时才在其他类中,这给了我错误。这是我的广播接收器,当 messagestatus=='t' 时,它将检查其他类的状态并更新该类中的列表。

 private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateUI(intent);       
        }};

        private void updateUI(Intent intent) {

            @SuppressWarnings("unchecked")
            ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) getListAdapter();
            Message message = null;
            //Toast.makeText(getActivity().getApplicationContext(), "Yea!!! Service called", Toast.LENGTH_SHORT).show();
    String messages = intent.getStringExtra("messages"); //getting received message from server class
// String senderip = intent.getStringExtra("sender");


    String[] arr = messages.split(" ", 3);
    String threadid = arr[0];

    //Toast.makeText( getActivity().getApplicationContext(),threadid,Toast.LENGTH_SHORT).show();

    char messagestatus = arr[1].charAt(0);
    String msg = arr[2];
   char ch = 0;
    if(messagestatus=='s')
    {
    ch='r';
    }
    if(messagestatus=='t')
    {

    ch='t';
    }
    int id = Integer.parseInt(threadid);

    message = datasource.createMessage(msg,ch,id);

    if(ch=='r')
    {
    adapter.add(message);
    adapter.notifyDataSetChanged();
    }                                                           //It is updating the respond list or sent message responses list
                                                        try {
                                                if (messagestatus == 't')
                                                {
                                                    long originalId = message.getId();
                                                    Respond res = new Respond();
                                                if (res.getActivityStatus() == true)
                                                {
                                                        if (res.getrthreadid().compareTo(threadid) == 0)
                                                        {
                                                            res.notiffy(msg,originalId);

                                                    }
                                                }

                                                    SResponses sres = new SResponses();
                                                if (sres.getActivityStatus() == true)
                                                        {
                                                if (sres.getsrthreadid().compareTo(threadid) == 0)
                                                {

                                            sres.notiffy(msg,originalId);
                                                }
                                                }
                                                }
                                                } catch (RuntimeException e) {
                                                    // TODO: handle exception
                                                e.printStackTrace();
                                                }

        }

我在 SResponses 类中将更新该类中的列表视图的函数是,我的 SResponses 类是:

 public class SResponses extends ListActivity{
    static boolean active = false;
    private DataSource datasource;
    long threadid;
    public static String srthreadid;

public  String  getsrthreadid()
    {
        return srthreadid;
        }
    public boolean getActivityStatus()
    {
        return active;
        }
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

         super.onCreate(savedInstanceState);
         setContentView(R.layout.sresponses);
         datasource = new DataSource(getApplicationContext());
          datasource.open();

          Intent intent=getIntent();

          threadid=  intent.getLongExtra("threadid", 0);
          srthreadid=String.valueOf(threadid);
          char ch='e';
          List<Message> values = datasource.getAllMessages(ch);
             ArrayAdapter<Message> adapter = new ArrayAdapter<Message>(getApplicationContext(), R.layout.custom_list_item, values);
               setListAdapter(adapter);


    }

    public void notiffy(String msg,long originalId)
    {
        // finish();
          // startActivity(getIntent());
        @SuppressWarnings("unchecked")
        ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) getListAdapter();
        Message messages = new Message();

        messages.setId(originalId);
        messages.setmessage(msg);
        messages.setthreadid(threadid);
        adapter.add(messages);
     adapter.notifyDataSetChanged();
        }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        datasource.close();
    }
    @Override
    public void onStart() {
       super.onStart();
       active = true;
    } 
    @Override
    public void onStop() {
       super.onStop();
       active = false;
    }
}

我在列表中获取消息的方法:

public List<Message> getAllMessages(char ch) {
      Cursor cursors = null;


//String thid=res.getrthreadid();

    List<Message> message = new ArrayList<Message>();
if(ch=='r')
        {
        String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+","+MySQLiteHelper.COLUMN_THREADID + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE " +MySQLiteHelper.COLUMN_STATUS + "= 'r'";
    cursors = database.rawQuery(queryz, null);
    }
    if(ch=='s')
    {
    String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+","+MySQLiteHelper.COLUMN_THREADID + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE " +MySQLiteHelper.COLUMN_STATUS + "= 's'";
    cursors = database.rawQuery(queryz, null);
    }
    if(ch=='t')
    {
         Respond res=new Respond();
    String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+","+MySQLiteHelper.COLUMN_THREADID + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE "+MySQLiteHelper.COLUMN_THREADID+ "="+res.getrthreadid();
    cursors = database.rawQuery(queryz, null);
    }
    if(ch=='e')
    {
        SResponses sres=new SResponses();
    String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+","+MySQLiteHelper.COLUMN_THREADID + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE "+MySQLiteHelper.COLUMN_THREADID+ "="+sres.getsrthreadid();
    cursors = database.rawQuery(queryz, null);
    }
    /*if(ch=='s')
        cursor = database.query(MySQLiteHelper.TABLE_NAME,allColumns, MySQLiteHelper.COLUMN_STATUS+"="+"s", null, null, null, null);*/

       cursors.moveToFirst();
        while (!cursors.isAfterLast()) {
          Message message1 = cursorToMessage(cursors);
          message.add(message1);
          cursors.moveToNext();
        }
        // Make sure to close the cursor
        cursors.close();
        return message;
      }


      private Message cursorToMessage(Cursor cursor) {
        Message message = new Message();
        message.setId(cursor.getLong(cursor.getColumnIndex("id")));
       // message.setparentid(cursor.getLong(1));
        message.setmessage(cursor.getString(cursor.getColumnIndex("message")));
        message.setthreadid(cursor.getLong(cursor.getColumnIndex("threadid")));
     //  message.status(cursor.getString(4)); 
        return message;

      }

但是我收到了这个错误,我已经为此付出了很多,请帮忙。

    09-18 03:56:34.469: W/System.err(22580): java.lang.NullPointerException
            09-18 03:56:34.473: W/System.err(22580):    at soft.b.peopleassist.SResponses.notiffy(SResponses.java:56)
            09-18 03:56:34.474: W/System.err(22580):    at soft.b.peopleassist.Receive.updateUI(Receive.java:122)
            09-18 03:56:34.474: W/System.err(22580):    at soft.b.peopleassist.Receive.access$0(Receive.java:66)
            09-18 03:56:34.475: W/System.err(22580):    at soft.b.peopleassist.Receive$1.onReceive(Receive.java:63)
            09-18 03:56:34.476: W/System.err(22580):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:785)
            09-18 03:56:34.478: W/System.err(22580):    at android.os.Handler.handleCallback(Handler.java:615)
            09-18 03:56:34.479: W/System.err(22580):    at android.os.Handler.dispatchMessage(Handler.java:92)
            09-18 03:56:34.479: W/System.err(22580):    at android.os.Looper.loop(Looper.java:153)
            09-18 03:56:34.481: W/System.err(22580):    at android.app.ActivityThread.main(ActivityThread.java:5000)
            09-18 03:56:34.481: W/System.err(22580):    at java.lang.reflect.Method.invokeNative(Native Method)
            09-18 03:56:34.483: W/System.err(22580):    at java.lang.reflect.Method.invoke(Method.java:511)
            09-18 03:56:34.484: W/System.err(22580):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
            09-18 03:56:34.485: W/System.err(22580):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            09-18 03:56:34.485: W/System.err(22580):    at dalvik.system.NativeStart.mai

n(Native Method)
4

0 回答 0