我正在使用下面的代碼,但它在振動時給予nullpointer異常。Android:NullpointerException在振動
public Server(DroidGap gap, WebView view) //constructor
{
mAppView = view;
mGap = gap;
}
public Server(Context context) //constructor
{
mcontext=context;
}
public void run() //run method
{
try {
InetAddress serveradd = InetAddress.getByName(serveraddress);
}
catch (UnknownHostException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
DatagramPacket packet=new DatagramPacket(buf,buf.length);
try
{
c.socket.receive(packet);
dat=new String(packet.getData());
if(dat!=null)
{
((Vibrator)mGap.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(800);
cordovaExample.activity.sendJavascript("displayreciever('"+dat+"')");
}
Log.d("UDP", "s: ReceivedJI: '" + new String(packet.getData()) + "'");
} //end of try block
catch (IOException e)
{
e.printStackTrace();
}
//end of catch block
} //end of run method
我得到空指針異常的followingline
((Vibrator)mGap.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(800);
我無法找出
任何幫助可以理解的原因,
ü沒有添加振動器許可清單文件:
在崩潰前行? – nayab
是的是有permission.I嘗試通過將振動代碼放在不同的方法然後它的工作,但是當我從run方法調用該方法,或者當我將該代碼放入run方法然後它不工作 –