Activity中的新意图空指针异常

时间:2014-07-14 11:01:29

标签: android android-intent

我正在尝试使用Facebook登录来构建聊天应用程序。登录部分工作正常。我面临的问题是,当我想要移动到面临空指针异常的新活动时,我使用socket.io。请帮我。提前做出来。

public class Second extends Activity {
     static SocketIO socket;
     Object data;
        int t = 0;
     Singleton tmp = Singleton.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    System.out.println("second activity enntered");
    setContentView(R.layout.activity_second);
    final TextView tv = (TextView) findViewById(R.id.textView1);
    System.out.println(getIntent().getExtras().getString("name")+"secondactivity");
    tv.setText("Welcome"+getIntent().getExtras().getString("name"));

final Button bt1 = (Button) findViewById(R.id.button1);
final Button bt2 = (Button) findViewById(R.id.button2);
final EditText et = (EditText) findViewById(R.id.editText1);
bt1.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

                if(bt1.getText().equals("Create Room"))
                {
            System.out.println(bt1.getText());

        et.setVisibility(View.VISIBLE);
        bt2.setVisibility(View.VISIBLE);
        bt1.setText("Cancel Room");
                }
        else    if(bt1.getText().equals("Cancel Room"))
        {
            System.out.println(bt1.getText());

            et.setVisibility(View.INVISIBLE);
            bt2.setVisibility(View.INVISIBLE);
            bt1.setText("Create Room");
        }

    }
});
bt2.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        JSONObject b = new JSONObject();
        try {
            b.put("creator", getIntent().getExtras().getString("name"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            b.put("roomName", et.getText()) ;
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        tmp.emit("createroom", b);

}
});

}

public void setData(Object[] args) {
    // TODO Auto-generated method stub
System.out.println(args[0]);
    data = args[0];
     t = 1;
    System.out.println(Second.this);


             Intent inte = null;
             System.out.println(inte+"asdasdasdasdasdasdsa");
             System.out.println(new Intent(Second.this,Ownroom.class));
             inte= new Intent(Second.this,Ownroom.class);

              inte.putExtra("json",data.toString() );
              startActivity(inte);


}
}

LogCat是

07-14 16:20:52.050: I/System.out(2353): com.example.lastapp1.Second@40f6b508
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

自己的代码

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

}

}

1 个答案:

答案 0 :(得分:1)

您正在打印 NULL

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