gpt4 book ai didi

java - Async Task runtimeexception 执行 doInBackground() 时发生错误

转载 作者:行者123 更新时间:2023-11-29 21:07:08 25 4
gpt4 key购买 nike

当我朗读我的应用程序时,它会给我错误

主 Activity

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);现在我做错了什么

最佳答案

getText(int host)

为主机、端口、服务、用户名和密码返回空值。那不会带你走多远。

关于java - Async Task runtimeexception 执行 doInBackground() 时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24205443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com