2016-09-23 118 views
0

这里的代码会做的TextView的更新和​​列表视图使用后台服务。广播意图接收空值

Broadcastservice类

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 

造成的:显示java.lang.NullPointerException

的NullPointerException异常点我first_Name.getCount()方法在我adapterclass 和我的活动的这些行

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

但发送的广播无法在活动类中接收。

回答

0

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

shiftCode方法在调用API方法"RestExample/shiftcode"和注册自定义监听到它。然后在onSuccess回调你有以下两行:

getEmployees(); 
sendBroadcast(intentBS); 

根据你的想法第一种方法应该准备intentBS和第二与员工发送此意图。但它不会发生。因为首先sendBroadcast(intentBS)将调用,然后你会因为使用API​​调用,都到另一个线程,然后最近(sendBroadcast后)调用onSuccess回调getEmployees填补intentBS与员工数据。

所以其实你一开始发送广播,然后才补intentBS

+0

我一些什么理解你在说什么。其实我得到shiftcode值,如果我禁用getemployees()方法。所以我现在应该做什么来获取arraylist值。 – user3785322

+0

@ user3785322在你的情况下,快速的解决方案,你可以轻松地将'sendBroadcast(intentBS);''来和getEmployees'把它之后'intentBS.putExtra( “employee_Array2”,employeeArray2);' –

+0

大。有效。除了这个,我尝试了其他一切。顺便说一句,这是正确的方式来做到这一点。 – user3785322