스레드를 생성하고 그 스레드에서 서버에서 데이터를 받고 데이터를 인텐트에 담아 방송하면 메인 엑티비티에서 데이터를 받고 어레이 리스트에 저장하는 소스입니다..구글링해도 뭐가 문제인지 잘 모르겠습니다...선배님들 도와주세요 ㅠ
데이터를 저장하는 클래스
public class BranchGPSData {
private double latitude;
private double longitude;
private String branchID;
public void setData(String branchID, double latitude, double longitude){
this.branchID=branchID;
this.latitude=latitude;
this.longitude=longitude;
}
}
메인 엑티비티
public class MainActivity extends ActionActivity {
UpdateThread thread;
public BranchGPSData branchGPSData[];
public ArrayList<BranchGPSData> branchGPSDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(broadcastReceiver,new IntentFilter("com.example.ab.ab1.branchGPSData"));
thread=new UpdateThread(this);
thread.start();
}
BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String[] branchID=intent.getStringArrayExtra("branchID");
double[] latitude=intent.getDoubleArrayExtra("latitude");
double[] longitude=intent.getDoubleArrayExtra("longitude");
branchGPSData=new BranchGPSData[branchID.length];
for(int i=0;i<branchID.length;i++){
branchGPSData[i].setData(branchID[i],latitude[i],longitude[i]);
}
}
};
서버와의 통신을 하는 클래스(스레드)
public class UpdateThread extends Thread {
final String URL="http://192.168.0.94:8080/zolproject/andcon/shop_location_pro.jsp";
private double latitudeArray[];
private double longitudeArray[];
private String branchIDArray[];
Context context;
public UpdateThread(Context context){
this.context=context;
}
@Override
public void run() {
super.run();
Looper.prepare();
try{
HttpPost post=new HttpPost(URL);
HttpClient client=new DefaultHttpClient();
HttpResponse response=client.execute(post);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"utf-8"));
String line=null;
String page="";
while ((line=bufferedReader.readLine())!=null){
page+=line;
}
JSONObject jsonObject=new JSONObject(page);
JSONArray jsonArray=jsonObject.getJSONArray("shoplocation");
branchIDArray=new String[jsonArray.length()];
latitudeArray=new double[jsonArray.length()];
longitudeArray=new double[jsonArray.length()];
for(int i=0;i<jsonArray.length();i++){
jsonObject=jsonArray.getJSONObject(i);
branchIDArray[i]=jsonObject.getString("id");
latitudeArray[i]=jsonObject.getDouble("gps1");
longitudeArray[i]=jsonObject.getDouble("gps2");
}
Intent intent1=new Intent("com.example.ab.ab1.branchGPSData");
intent1.putExtra("branchID",branchIDArray);
intent1.putExtra("latitude",latitudeArray);
intent1.putExtra("longitude",longitudeArray);
context.sendBroadcast(intent1);
}catch (IOException e){
e.printStackTrace();
}catch (JSONException e){
e.printStackTrace();
}
Looper.loop();
}
}
에러코드
java.lang.RuntimeException: Error receiving broadcast Intent { act=com.example.ab.ab1.branchGPSData flg=0x10 (has extras) } in com.example.ab.updatetest.MainActivity$1@158fafb2
......
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ab.updatetest.MainActivity$BranchGPSData.setData(java.lang.String, double, double)' on a null object reference
......
과정을 좀 설명하면 일단 서버에서 데이터를 받고 방송을 하면 방송수신까지 데이터가 잘 도착합니다.
근데 방송수신자에서 branchGPSData[i].setData(branchID[i],latitude[i],longitude[i]);
여기서 문제입니다...뭐가 문제일까요?