0

次のコードを使用していますが、vibrateで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

次の行でnullpointerExceptionが発生しています

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

理由がわからない

どんな助けでもありがたいです、

4

1 に答える 1

0

mGap が null のようです

コードでこの変数を初期化する必要があります。これ以上の支援はできません...

編集:

次の出力を教えてください:

 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 に答える