gpt4 book ai didi

java - 在线程中访问类级别变量

转载 作者:行者123 更新时间:2023-12-01 15:45:52 25 4
gpt4 key购买 nike

如何在线程内访问类级别变量,我尝试直接访问变量但发生异常。是否可以访问线程内的类级变量,或者我必须使用处理程序?

这是我的代码。

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
//***************************************************************************************




}

异常是空指针异常,位于 Device_ID[0] = tokens[1];
这一行。

最佳答案

对我来说,这看起来不像语法上有效的 Java。您应该看到一个编译错误,告诉您 ClassToken = token; 无法识别。如果它已经过去了,另一则消息说您尚未声明 run() 方法。并且缺少一个分号。

<小时/>

如果您愿意发布出现错误的真实代码,我们也许能够告诉您真正问题是什么。

<小时/>

问题在于您已将 Device_ID 初始化为 null 而不是数组。当您尝试对 null 建立索引时,您将得到一个 NPE。

简单的解决方法是将初始化更改为:

String[] Device_ID = new String[1];

(从您给出的代码中,我根本不明白为什么您需要使用数组。为什么不直接将 Device_ID 设为 String ?OTOH,也许这只是您的演示代码的产物...)

关于java - 在线程中访问类级别变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7023938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com