异步任务runtimeexception执行doInBackground()时发生错误

时间:2014-06-13 12:35:22

标签: java android xmpp

当我朗读我的申请时,它会给我错误

MainActivity

public class MainActivity<XMPPConnection> extends Activity {    
private ArrayList<String> messages = new ArrayList();
private Handler mHandler = new Handler();
private SettingsDialog mDialog;
private EditText mRecipient;
private EditText mSendText;
private ListView mList;
private XMPPConnection connection;
public static String setConnection;

/**
 * Called with the activity is first created.
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.i("XMPPClient", "onCreate called");
    setContentView(R.layout.activity_main);

    mRecipient = (EditText) this.findViewById(R.id.recipient);
    Log.i("XMPPClient", "mRecipient = " + mRecipient);
    mSendText = (EditText) this.findViewById(R.id.sendText);
    Log.i("XMPPClient", "mSendText = " + mSendText);
    mList = (ListView) this.findViewById(R.id.listMessages);
    Log.i("XMPPClient", "mList = " + mList);
    setListAdapter();

    // Dialog for getting the xmpp settings
    mDialog = new SettingsDialog(this);
    new MyTask().execute();   
    // Set a listener to show the settings dialog
    Button setup = (Button) this.findViewById(R.id.setup);
    setup.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            new MyTask().execute();
        }
    });

    // Set a listener to send a chat text message
    Button send = (Button) this.findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            String to = mRecipient.getText().toString();
            String text = mSendText.getText().toString();

            Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
            Message msg = new Message(to, Message.Type.chat);
            msg.setBody(text);
            ((org.jivesoftware.smack.XMPPConnection) connection).sendPacket(msg);
            messages.add(((org.jivesoftware.smack.XMPPConnection) connection).getUser() + ":");
            messages.add(text);
            setListAdapter();
        }
    });
}

/**
 * Called by Settings dialog when a connection is establised with the XMPP server
 *
 * @param connection
 */
public void setConnection
(XMPPConnection
        connection) {
 this.connection = connection;
 if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
((Connection) connection).addPacketListener(new PacketListener() {
    public void processPacket(Packet packet) {
        Message message = (Message) packet;
        if (message.getBody() != null) {
            String fromName = StringUtils.parseBareAddress(message.getFrom());
            Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
            messages.add(fromName + ":");
            messages.add(message.getBody());
            // Add the incoming message to the list view
            mHandler.post(new Runnable() {
                public void run() {
                    setListAdapter();
                }
            });
        }
    }
}, filter);
    }
}

private void setListAdapter
        () {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.multi_line_list_item,
            messages);
    mList.setAdapter(adapter);
}   

}

这是我的MyTask类

private MainActivity<XMPPConnection> MainActivity;
private XMPPConnection setConnection;

@Override
protected String doInBackground(String... params) {
    String host = "web.vlivetech.com";                   //getText(R.id.host);                                 
    String port = "5222";                  //                                            
    String service = "web.vlivetech.com";// getText(R.id.service);                                      
    String username ="has12345"; //  getText(R.id.userid);         //                                  
    String password = "123";// getText(R.id.password);      //                                      

    // Create a connection
        ConnectionConfiguration connConfig =
                new ConnectionConfiguration(host, Integer.parseInt(port), service);
        XMPPConnection connection = new XMPPConnection(connConfig);

        try {
            connection.connect();
            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
            MainActivity.setConnection(null);
        }
        try {
            connection.login(username, password);
            Log.i("XMPPClient", "Logged in as " + connection.getUser());

            // Set the status to available
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            **MainActivity.setConnection(connection);**
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
            MainActivity.setConnection(null);
        }
    return null;
}

private String getText(int id) {
        EditText widget = (EditText) this.findViewById(id);
        return widget.getText().toString();
    }

    private EditText findViewById(int id) {
        // TODO Auto-generated method stub
        return null;
    }

}

当我调试我的代码时,这将获得连接值但终止于此行 MainActivity.setConnection(connection); 现在我做错了什么

1 个答案:

答案 0 :(得分:2)

getText(int host)

为主机,端口,服务,用户名和密码返回null。这不会带给你很远的。