2014-07-14 51 views
-2

我試圖建立一個使用Facebook登錄的聊天應用程序。登錄部分工作正常。我面臨的問題是,當我想移動到新的活動im面臨空指針異常,我使用socket.io。請幫幫我。 Thaks提前。在活動中的新的意圖空指針異常

​​

logcat的是

07-14 16:20:52.050: I/System.out(2353): [email protected] 
07-14 16:20:52.060: I/System.out(2353): nullasdasdasdasdasdasdsa 
07-14 16:20:52.070: W/System.err(2353): io.socket.SocketIOException: Exception was thrown in on(String, JSONObject[]). 
07-14 16:20:52.070: W/System.err(2353): Message was: 5:::{"name":"receiveroom","args":[{"roomName":"kj","roomId":"be72be30-0b44-11e4-9f8c-4706467ac6c0","creator":"Saikrishna Rangineni","listofusers":["Saikrishna Rangineni"]}]} 
07-14 16:20:52.080: W/System.err(2353):  at io.socket.IOConnection.transportMessage(IOConnection.java:700) 
07-14 16:20:52.090: W/System.err(2353):  at io.socket.WebsocketTransport.onMessage(WebsocketTransport.java:82) 
07-14 16:20:52.090: W/System.err(2353):  at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:361) 
07-14 16:20:52.090: W/System.err(2353):  at org.java_websocket.WebSocketImpl.deliverMessage(WebSocketImpl.java:565) 
07-14 16:20:52.100: W/System.err(2353):  at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:331) 
07-14 16:20:52.100: W/System.err(2353):  at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:152) 
07-14 16:20:52.120: W/System.err(2353):  at org.java_websocket.client.WebSocketClient.interruptableRun(WebSocketClient.java:247) 
07-14 16:20:52.120: W/System.err(2353):  at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:193) 
07-14 16:20:52.130: W/System.err(2353):  at java.lang.Thread.run(Thread.java:856) 
07-14 16:20:52.130: W/System.err(2353): Caused by: java.lang.NullPointerException 
07-14 16:20:52.140: W/System.err(2353):  at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 
07-14 16:20:52.150: W/System.err(2353):  at android.content.ComponentName.<init>(ComponentName.java:75) 
07-14 16:20:52.150: W/System.err(2353):  at android.content.Intent.<init>(Intent.java:3122) 
07-14 16:20:52.150: W/System.err(2353):  at com.example.lastapp1.Second.setData(Second.java:104) 
07-14 16:20:52.160: W/System.err(2353):  at com.example.server.Singleton$1.on(Singleton.java:71) 
07-14 16:20:52.160: W/System.err(2353):  at io.socket.IOConnection.on(IOConnection.java:907) 
07-14 16:20:52.170: W/System.err(2353):  at io.socket.IOConnection.transportMessage(IOConnection.java:697) 
07-14 16:20:52.170: W/System.err(2353):  ... 8 more 

Ownroom代碼

package com.example.lastapp1; 

import io.socket.IOAcknowledge; 
import io.socket.IOCallback; 
import io.socket.SocketIO; 
import io.socket.SocketIOException; 

import java.security.NoSuchAlgorithmException; 

import javax.net.ssl.SSLContext; 

import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Ownroom extends Activity { 
SocketIO socket; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_ownroom); 
     Button bt1 = (Button) findViewById(R.id.button3); 
     final EditText et = (EditText) findViewById(R.id.editText1); 
     final TextView tv = (TextView) findViewById(R.id.textView1); 
     //System.out.println(getIntent().getExtras().get); 
     try { 
      final JSONObject obj = new JSONObject(getIntent().getStringExtra("json")); 



    tv.setText("Welcome to "+obj.get("roomName")+"room"); 

     bt1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      System.out.println("helllo in click"); 
      // TODO Auto-generated method stub 
      JSONObject a = new JSONObject(); 
      try { 
       a.put("message", et.getText()); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       a.put("name", obj.get("creator")); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       a.put("roomName", obj.get("roomName")); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       a.put("roomId", obj.get("roomId")); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      System.out.println(obj+"in click"); 
      socket.emit("send message",a); 
       } 
    }); 
     } catch (JSONException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
    } 
    } 
} 

單噸級

package com.example.server; 

import io.socket.IOAcknowledge; 
import io.socket.IOCallback; 
import io.socket.SocketIO; 
import io.socket.SocketIOException; 

import java.net.Socket; 
import java.security.NoSuchAlgorithmException; 

import javax.net.ssl.SSLContext; 

import org.json.JSONObject; 

import com.example.lastapp1.MainActivity; 
import com.example.lastapp1.MainActivity.MainFragment; 
import com.example.lastapp1.Second; 

import android.content.Intent; 
import android.sax.StartElementListener; 
import android.util.Log; 

public class Singleton { 
String name = "No name"; 
    private static Singleton Single= new Singleton(); 
    static SocketIO socket; 

    static MainFragment mianee1 = new MainFragment(); 
    static Second mianee2 = new Second(); 
    /* A private Constructor prevents any other 
     * class from instantiating. 
     */ 
     private Singleton(){ } 

     /* Static 'instance' method */ 
     public static Singleton getInstance() { 



      System.out.println("login"); 
      try { 
        SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault()); 
       } catch (NoSuchAlgorithmException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
     try { 
     socket = new SocketIO("http://10.0.2.2:3000/"); 
     System.out.println("asdasdasd"); 
     } catch (Exception e) { 
     //TODO Auto-generated catch block 
     System.out.println(e); 
     e.printStackTrace(); 
     } 

     socket.connect(new IOCallback() { 

     public void on(String event, IOAcknowledge ack, Object... args) { 
      if ("socket1".equals(event) && args.length > 0) { 
       Log.e("received",""+args[0]); 

      mianee1.setData(args); 
      System.out.println("yes yes yes"+args[0]); 


      } 

      if ("receiveroom".equals(event) && args.length > 0) { 
        Log.e("receiveroom",""+args[0]); 

      // mianee2.setData(args); 
       System.out.println("received socket from second room"+args[0]); 


       } 
      } 

     public void onMessage(JSONObject json, IOAcknowledge ack) { 
       System.out.println("Server said:" + json); 
     } 
     public void onMessage(String data, IOAcknowledge ack) { 
       System.out.println("Server said:" + data); 
     } 

     public void onError(SocketIOException socketIOException) { socketIOException.printStackTrace();} 

     public void onDisconnect() { 
     System.out.println("connected sucesfully"); 
     } 

     public void onConnect() { 
       System.out.println("connected"); 
     } 

     }); 

      return Single; 


      } 
     public void run() { 
      // TODO Auto-generated method stub 

     } 

//public static void run() { 
// 
// 
// try { 
//   SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault()); 
//  } catch (NoSuchAlgorithmException e1) { 
//   // TODO Auto-generated catch block 
//   e1.printStackTrace(); 
//  } 
//try { 
//SocketIO socket = new SocketIO("http://10.0.2.2:3000/"); 
//System.out.println("asdasdasd"); 
//} catch (Exception e) { 
//// TODO Auto-generated catch block 
//System.out.println(e); 
//e.printStackTrace(); 
//} 
//SocketIO socket = null; 
//socket.connect(new IOCallback() { 
// 
//public void on(String event, IOAcknowledge ack, Object... args) { 
// if ("socket1".equals(event) && args.length > 0) { 
//  Log.e("received",""+args[0]); 
//  MainFragment mai = new MainFragment(); 
//  mai.nextActvity(args[0]); 
// } 
//} 
// 
//public void onMessage(JSONObject json, IOAcknowledge ack) { 
//  System.out.println("Server said:" + json); 
//} 
//public void onMessage(String data, IOAcknowledge ack) { 
//  System.out.println("Server said:" + data); 
//} 
// 
//public void onError(SocketIOException socketIOException) { socketIOException.printStackTrace();} 
// 
//public void onDisconnect() { 
//System.out.println("connected sucesfully"); 
//} 
// 
//public void onConnect() { 
//  System.out.println("connected"); 
//} 
//}); 
//} 


public void setName(String a) 
{ 
    name = a; 

} 
public String getname() { 
    return name; 

} 

public void emit(String string, String abc) { 
    // TODO Auto-generated method stub 
    socket.emit(string,abc); 
} 

public void emit(String string, JSONObject b) { 
    // TODO Auto-generated method stub 
    socket.emit(string,b); 

} 

} 
+0

發佈您的logcat ... –

+1

必須有一個默認註釋stackoverflow必須提供我們不必鍵入,它必須「請發佈您的logcat」。 – nobalG

+0

這裏是我的logcat日誌對不起 – saikrishna

回答

1

要打印NULL

Intent inte = null; 
System.out.println(inte+"asdasdasdasdasdasdsa"); 
+0

是啊,我正在測試那部分。當我嘗試在代碼的最後部分創建一個新的intent inte = new Intent(this,Ownroom.class);這裏im開空指針異常 – saikrishna

+0

一旦你刪除syso並再次測試 – KOTIOS

+0

我做到了即時獲得相同的異常 – saikrishna