0

我正在使用以下代码,但它在振动时给出了空指针异常。

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

我在以下行收到空指针异常

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

我无法找出原因

任何帮助将不胜感激,

4

1 回答 1

0

mGap 似乎为空

您应该使用您的代码初始化此变量,我们无法提供更多帮助...

编辑:

请告诉我的输出:

 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)));

就在崩溃线之前:

((Vibrator)mGap.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(800);
于 2012-12-08T11:08:20.130 回答