2012-12-08 101 views
0

我正在使用下面的代碼,但它在振動時給予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); 

我無法找出

任何幫助可以理解的原因,

+0

ü沒有添加振動器許可清單文件:

Log.d("UDP", "Is this null? " + mGap); Log.d("UDP", "Is this null? " + mGap.getSystemService(Context.VIBRATOR_SERVICE)); Log.d("UDP", "Is this null? " + ((Vibrator)mGap.getSystemService(Context.VIBRATOR_SERVICE))); 

在崩潰前行? – nayab

+0

是的是有permission.I嘗試通過將振動代碼放在不同的方法然後它的工作,但是當我從run方法調用該方法,或者當我將該代碼放入run方法然後它不工作 –

回答

0

mGap似乎是空

你應該用你的代碼初始化這個變量,我們不能給予更多的幫助...

編輯:

請告訴我的輸出:

((Vibrator)mGap.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(800); 
+0

thnx回覆我將檢查,並會回覆,如果它的工作與否 –

+0

其實我已經初始化mgap..i將在其中添加更多的代碼 –

+0

代碼在問題中添加 –