2012-12-08 4 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 

나는 아래 행에 nullpointer 예외를 얻고있다

((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

예 권한이 있습니다. 나는 다른 방법으로 진동 코드를 입력하여 작업을 시도했지만 실행 방법에서 해당 메서드를 호출했거나 실행 메서드에 해당 코드를 배치하면 해당 함수가 작동하지 않습니다. –

답변

0

mGap가 null 것 같다, 어떤 도움을 이해할 수있을 것이다

원인을 찾을 수 없습니다입니다

이 변수를 코드로 초기화해야합니다.

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

thnx로 응답합니다. 그것을 확인하고 그것이 작동하는지 아니면 회신하는지 –

+0

실제로 내가 이미 mgap..i를 초기화했습니다. 더 많은 코드를 추가 할 것입니다. –

+0

코드가 –

관련 문제