我创建了一个使用了.NET集成的Android应用程序,omegle java客户端是一个开源项目形式的github,它有一个控制台客户端示例,在java控制台中工作正常,我试图在Android活动中插入代码,我希望在TextView中显示它并在需要时插入我自己的文本。
这是我的代码
MainActivity.java
package com.example.com.omegle.test;
import org.nikki.omegle.Omegle;
import org.nikki.omegle.core.OmegleException;
import org.nikki.omegle.core.OmegleMode;
import org.nikki.omegle.core.OmegleSession;
import org.nikki.omegle.event.OmegleEventAdaptor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OmegleChat om=new OmegleChat();
om.execute();
}
private class OmegleChat extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
txt=(TextView)findViewById(R.id.omegle);
txt.append("Omegele Connection Started... \n");
}
@Override
protected void onProgressUpdate(Void... values) {
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
OmgeleChatOn();
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
public void OmgeleChatOn()
{
Omegle omegle = new Omegle();
try {
System.out.println("Opening session...");
OmegleSession session = omegle.openSession(OmegleMode.NORMAL, new OmegleEventAdaptor() {
@Override
public void chatWaiting(OmegleSession session) {
System.out.println("Waiting for chat...");
}
@Override
public void chatConnected(OmegleSession session) {
System.out
.println("You are now talking to a random stranger!");
}
@Override
public void chatMessage(OmegleSession session, String message) {
System.out.println("Stranger: " + message);
}
@Override
public void messageSent(OmegleSession session, String string) {
System.out.println("You: " + string);
}
@Override
public void strangerDisconnected(OmegleSession session) {
System.out.println("Stranger disconnected, goodbye!");
//System.exit(0);
}
@Override
public void omegleError(OmegleSession session, String string) {
System.out.println("ERROR! " + string);
System.exit(1);
}
});
} catch (OmegleException e) {
e.printStackTrace();
}
}
}
现在这是错误日志,应用程序在几秒钟后崩溃,陌生人可以回复,并且在他的回复之后应用程序崩溃了这样 - &gt;
07-05 04:05:26.758: I/Process(661): Sending signal. PID: 661 SIG: 9
07-05 04:05:58.358: I/System.out(673): Opening session...
07-05 04:05:59.598: I/global(673): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 04:05:59.618: I/dalvikvm(673): Could not find method org.json.JSONArray.<init>, referenced from method org.nikki.omegle.core.OmegleSession.fireEvent
07-05 04:05:59.618: W/dalvikvm(673): VFY: unable to resolve direct method 3626: Lorg/json/JSONArray;.<init> (Ljava/lang/Object;)V
07-05 04:05:59.618: D/dalvikvm(673): VFY: replacing opcode 0x70 at 0x003e
07-05 04:05:59.628: D/dalvikvm(673): VFY: dead code 0x0041-0041 in Lorg/nikki/omegle/core/OmegleSession;.fireEvent (Lorg/nikki/omegle/core/OmegleEvent;Ljava/lang/Object;)V
07-05 04:05:59.658: I/System.out(673): Waiting for chat...
07-05 04:06:05.208: I/global(673): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 04:06:05.218: I/System.out(673): You are now talking to a random stranger!
07-05 04:06:09.969: I/global(673): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 04:06:17.139: I/global(673): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 04:06:17.149: I/System.out(673): Stranger: hlelo [: .,.
07-05 04:06:21.378: W/KeyCharacterMap(673): No keyboard for id 0
07-05 04:06:21.378: W/KeyCharacterMap(673): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-05 04:06:28.538: D/dalvikvm(673): GC_FOR_MALLOC freed 6183 objects / 399936 bytes in 85ms
07-05 04:06:28.538: W/KeyCharacterMap(673): No keyboard for id 0
07-05 04:06:28.538: W/KeyCharacterMap(673): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-05 04:06:28.588: I/global(673): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 04:06:30.068: I/global(673): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 04:06:35.708: I/global(673): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 04:06:45.478: I/global(673): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 04:06:45.510: W/dalvikvm(673): threadid=9: thread exiting with uncaught exception (group=0x4001d800)
07-05 04:06:45.510: E/AndroidRuntime(673): FATAL EXCEPTION: pool-1-thread-3
07-05 04:06:45.510: E/AndroidRuntime(673): java.lang.NoSuchMethodError: org.json.JSONArray.<init>
07-05 04:06:45.510: E/AndroidRuntime(673): at org.nikki.omegle.core.OmegleSession.fireEvent(OmegleSession.java:250)
07-05 04:06:45.510: E/AndroidRuntime(673): at org.nikki.omegle.core.OmegleSession.checkEvents(OmegleSession.java:204)
07-05 04:06:45.510: E/AndroidRuntime(673): at org.nikki.omegle.Omegle$1.run(Omegle.java:124)
07-05 04:06:45.510: E/AndroidRuntime(673): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-05 04:06:45.510: E/AndroidRuntime(673): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-05 04:06:45.510: E/AndroidRuntime(673): at java.lang.Thread.run(Thread.java:1096)
池1线程-3异常的原因可能是什么。
答案 0 :(得分:3)
我通过执行以下更改解决了这个问题 - &gt;
package com.example.com.omegle.test;
import org.nikki.omegle.Omegle;
import org.nikki.omegle.core.OmegleException;
import org.nikki.omegle.core.OmegleMode;
import org.nikki.omegle.core.OmegleSession;
import org.nikki.omegle.event.OmegleEventAdaptor;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txt;
Button m_btn;
EditText m_edt;
OmegleSession session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_btn = (Button)findViewById(R.id.button);
m_edt = (EditText)findViewById(R.id.type);
m_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new SendMsg().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
new SendMsg().execute(m_edt.getText().toString());
}
txt.append("You:"+m_edt.getText().toString()+"\n");
m_edt.setText("");
}
});
//OmegleChat om=new OmegleChat();
runOnUiThread(new Runnable() {
@Override
public void run() {
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new OmegleChat().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
new OmegleChat().execute();
}
}
});
}
protected void sendIt(View v) {
txt.append("You: "+m_edt.getText().toString());
m_edt.setText("");
}
private class SendMsg extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
@Override
protected Void doInBackground(String... param) {
// TODO Auto-generated method stub
//OmgeleChatOn();
try {
session.send(param[0]);
} catch (OmegleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
private class OmegleChat extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
txt=(TextView)findViewById(R.id.omegle);
txt.append("Omegele Connection Started... \n");
}
@Override
protected void onProgressUpdate(Void... values) {
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
OmgeleChatOn();
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
public void OmgeleChatOn()
{
Omegle omegle = new Omegle();
try {
System.out.println("Opening session...");
session = omegle.openSession(OmegleMode.NORMAL, new OmegleEventAdaptor() {
@Override
public void chatWaiting(OmegleSession session) {
System.out.println("Waiting for chat...");
}
@Override
public void chatConnected(OmegleSession session) {
System.out
.println("You are now talking to a random stranger!");
}
@Override
public void chatMessage(OmegleSession session, final String message) {
System.out.println("Stranger: " + message);
runOnUiThread(new Runnable() {
@Override
public void run() {
txt.append("Stranger:"+message+"\n");
m_btn.setClickable(true);
}
});
}
@Override
public void messageSent(OmegleSession session, final String string) {
System.out.println("You: " + string);
/*runOnUiThread(new Runnable() {
@Override
public void run() {
txt.append("You:"+string+"\n");
}
}); */
}
@Override
public void strangerDisconnected(OmegleSession session) {
System.out.println("Stranger disconnected, goodbye!");
//System.exit(0);
}
@Override
public void omegleError(OmegleSession session, String string) {
System.out.println("ERROR! " + string);
System.exit(1);
}
});
} catch (OmegleException e) {
e.printStackTrace();
}
}
}