Android:振动时的NullpointerException

时间:2012-12-08 11:04:52

标签: android

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

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

我无法找出原因

任何帮助将不胜感激,

1 个答案:

答案 0 :(得分: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);