广播意图接收空值

Broadcast Intent receive null value

此处的代码将使用后台服务更新 textview 和 listview。

广播服务class

public class BroadcastService extends Service {

    private static final String TAG = "BroadcastService";
    public static final String BROADCAST_ACTION = "com.example.activity.displayevent";
    private final Handler handler = new Handler();
    Intent intentBS;
    int counter = 0;
@Override
    public void onCreate() {
        super.onCreate();

        intentBS = new Intent(BROADCAST_ACTION);    
}
 @Override
    public void onStart(Intent intentBS, int startId) {
        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, 1000); // 1 second

    }

    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            getShiftCode();         
            handler.postDelayed(this, 10000); // 10 seconds
        }
    };    
private void getShiftCode() {
     List<Header> headers = new ArrayList<Header>();
     headers.add(new BasicHeader("Accept", "application/json"));

     EmployeeRestClient.get(BroadcastService.this, "RestExample/shiftcode", 
    headers.toArray(new Header[headers.size()]),null, 
        new JsonHttpResponseHandler() {
     @Override
     public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
     ArrayList<String> scArray = new ArrayList<String>();

       for (int i = 0; i < response.length(); i++) {
        try {
             String Sc_array = c.getString("shift_Code");
             scArray.add(Sc_array);
             } catch (JSONException e) {
             e.printStackTrace();
               }
           }
                shift_Code1= scArray.get(0);
                intentBS.putExtra("shift_Code1", shift_Code1);

           getEmployees();
           sendBroadcast(intentBS);

         }
    });
 }

private void getEmployees() {
     List<Header> headers = new ArrayList<Header>();
     headers.add(new BasicHeader("Accept", "application/json"));

     EmployeeRestClient.get(BroadcastService.this, "RestExample/employee", 
    headers.toArray(new Header[headers.size()]),null, 
        new JsonHttpResponseHandler() {
     @Override
     public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
     ArrayList<String> employeeArray1 = new ArrayList<String>();
     ArrayList<String> employeeArray2 = new ArrayList<String>();

       for (int i = 0; i < response.length(); i++) {
        try {
              String  First_NameArray = c.getString("first_Name");
          String  Second_NameArray = c.getString("second_name");
          employeeArray1.add(First_NameArray );
          employeeArray2.add(Second_NameArray );

             } catch (JSONException e) {
             e.printStackTrace();
               }
           }
             intentBS.putExtra("employee_Array1", employeeArray1);
         intentBS.putExtra("employee_Array2", employeeArray2);
      }
    });
 }

错误详情:

java.lang.RuntimeException: Error receiving broadcast Intent { act=com.example.activity.displayevent flg=0x10 (has extras) } and

Caused by: java.lang.NullPointerException

nullpointerexception 指向我的适配器中的 first_Name.getCount() 方法class 以及我的 activity

的这些行
updateUI(intentBS);
private void updateUI(Intent intentBS) {
final  MyListAdapter myListAdapter = new MyListAdapter (SecondActivity.this,nameArray1,nameArray2);

但是发送的广播在activityclass中收不到。

抱歉,也许我错过了什么,但如果我正确理解代码,你的问题是你没有发送两个数组列表。

shiftCode 方法中调用 api 方法 "RestExample/shiftcode" 并向其注册自定义侦听器。然后在 onSuccess 回调中你有这两行:

getEmployees();
sendBroadcast(intentBS);

根据您的想法,第一种方法应该准备 intentBS,然后将此意图发送给员工。但它不会发生。因为首先 sendBroadcast(intentBS) 将被调用,然后你将用员工数据填充 intentBS,因为 getEmployees 使用 Api 调用,它转到另一个线程然后最近(在 sendBroadcast) 调用 onSuccess 回调。

所以实际上一开始你发送广播然后才填充intentBS