Accessing Class level variable in thread
How can I access class level variables inside thread, I have tried to access variable directly but the exception occur. Is it possible to access class level variable inside thread or I have to use handler?
Here is my Code.
public class MainMapActivity extends MapActivity
{
//***************************************************************************************
//isRouteDisplayed Method (Override)
@Override
protected boolean isRouteDisplayed()
{
return false;
}//End method
//***************************************************************************************
//private Handler handlerTimer = new Handler();
//***************************************************************************************
//Class Level Variables & Objects
String[] Device_ID = null;
//***************************************************************************************
//onCreate method (Override the built-in method) Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
//handlerTimer.removeCallbacks(taskUpdateStuffOnDialog);
//handlerTimer.postDelayed(taskUpdateStuffOnDialog , 100);
//service = new NewService();
new Thread(taskUpdateStuffOnDialog).start();
// GIve the Server some time for startup
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
}
// Kickoff the Client
// startService(new Intent(this, NewService.class));
new Thread(new NewService()).start();
}//End onCreate
//***************************************************************************************
private Runnable taskUpdateStuffOnDialog = new Runnable()
{
public void run()
{
GlobalVariable appState = ((GlobalVariable)getApplicationContext());
try
{
serverAddr = InetAddress.getByName(SERVERIP);
Log.d("UDP", "S: Connecting...");
/* Create new UDP-Socket */
socket = new DatagramSocket(SERVERPORT, serverAddr);
}
catch (Exception e)
{
Log.e("UDP", "S: Error", e);
}
while(true)
{
// TODO Auto-generated method stub
try
{
/* Retrieve the ServerName */
/* By magic we know, how much data will be waiting for us */
byte[] buf = new byte[72];
/* Prepare a UDP-Packet that can
* contain the data we want to receive */
DatagramPacket packet = new DatagramPacket(buf, buf.length);
Log.d("UDP", "S: Receiving...");
/* Receive the UDP-Packet */
socket.receive(packet);
String id = new String(packet.getData());
//buffer.order(ByteOrder.LITTLE_ENDIAN); // if you want little-endian
String[] tokens = id.split(" ");
String b = tokens[0];
Log.d("Message: ", b + " " );
/***************************** sending device ID *************************************/
else if(b.equals("5"))
{
Device_ID[0] = tokens[1];
runOnUiThread(new Runnable()
{
public void run()
{
String id = tokens[1];
Log.d("UDP", "S: Received Device ID: '" + id + "'");
setID(id);
//positionOverlay.setID(id);
//addEvent(id);
Toast.makeText(MainMapActivity.this, "Your Device ID " + id,Toast.LENGTH_LONG).show();
}
});
//Toast.makeText(MainMapActivity.this, "Your Device ID " + b,Toast.LENGTH_LONG).show();
} // end else if
} // end try
catch (Exception e)
{
Log.e("UDP", "S: Error", e);
} // end catch
//handlerTimer.postDelayed(this, 100);
} // end while condition
//******************开发者_运维知识库*********************************************************************
}//End Run method
//***************************************************************************************
};//End Thread
//***************************************************************************************
}
And exception is null pointer exception at Device_ID[0] = tokens[1];
this line.
That doesn't look like syntactically valid Java to me. You should have seen a compilation error telling you that ClassToken = token;
in not recognized. And if it had gotten past that, another one saying that you haven't declared the run()
method. And there's a missing semicolon.
If you would care to post the real code that is giving the error, we might be able to tell you what the real problem is.
The problem is that you have initialized Device_ID
to null
rather than an array. When you try to index a null
you will get an NPE.
The simple fix is to change the initialization to:
String[] Device_ID = new String[1];
(From your code as given, I can't see why you need to use an array at all. Why not just make Device_ID
a String
? OTOH, perhaps this is just an artefact of your demonstration code ...)
It seems that you never initialized array deviceId[] to anything except null - so you clearly get null pointer exception
精彩评论