我正在尝试使用IRClib(http://moepii.sourceforge.net/irclib/javadoc/org/schwering/irc/lib/package-summary.html) 在我的Android项目中。我在项目的'libs'文件夹中添加了irclib.jar。此外,我已将该文件添加为库,因此它出现在项目的“引用库”部分中。我还在Android清单中添加了“使用互联网”权限..
我有以下初步代码。
package com.shaarad.airc;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;
import org.schwering.irc.lib.*;
public class MainActivity extends Activity {
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IRCConnection connection=new IRCConnection("irc.freenode.net",6667,6667,"myircpass","shaarad","shaarad","Shaarad");
try {
connection.connect();
connection.doJoin("#ubuntu");
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "IOE", Toast.LENGTH_SHORT).show();
}
if (connection.isConnected()) {
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_SHORT).show();
}
text=(EditText)findViewById(R.id.editText1);
text.setText("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
每当我通过将其作为USB调试模式连接在我的设备上运行它时,活动会闪烁一秒钟并立即强制关闭! 这是调试输出:
06-29 12:50:53.211: E/AndroidRuntime(12023): FATAL EXCEPTION: main
06-29 12:50:53.211: E/AndroidRuntime(12023): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shaarad.airc/com.shaarad.airc.MainActivity}: android.os.NetworkOnMainThreadException
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2090)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.app.ActivityThread.access$600(ActivityThread.java:136)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.os.Looper.loop(Looper.java:137)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.app.ActivityThread.main(ActivityThread.java:4800)
06-29 12:50:53.211: E/AndroidRuntime(12023): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 12:50:53.211: E/AndroidRuntime(12023): at java.lang.reflect.Method.invoke(Method.java:511)
06-29 12:50:53.211: E/AndroidRuntime(12023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
06-29 12:50:53.211: E/AndroidRuntime(12023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
06-29 12:50:53.211: E/AndroidRuntime(12023): at dalvik.system.NativeStart.main(Native Method)
06-29 12:50:53.211: E/AndroidRuntime(12023): Caused by: android.os.NetworkOnMainThreadException
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-29 12:50:53.211: E/AndroidRuntime(12023): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-29 12:50:53.211: E/AndroidRuntime(12023): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-29 12:50:53.211: E/AndroidRuntime(12023): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-29 12:50:53.211: E/AndroidRuntime(12023): at java.net.Socket.tryAllAddresses(Socket.java:113)
06-29 12:50:53.211: E/AndroidRuntime(12023): at java.net.Socket.<init>(Socket.java:182)
06-29 12:50:53.211: E/AndroidRuntime(12023): at java.net.Socket.<init>(Socket.java:154)
06-29 12:50:53.211: E/AndroidRuntime(12023): at org.schwering.irc.lib.IRCConnection.connect(IRCConnection.java:295)
06-29 12:50:53.211: E/AndroidRuntime(12023): at com.shaarad.airc.MainActivity.onCreate(MainActivity.java:22)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.app.Activity.performCreate(Activity.java:5008)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-29 12:50:53.211: E/AndroidRuntime(12023): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
06-29 12:50:53.211: E/AndroidRuntime(12023): ... 11 more
答案 0 :(得分:0)
您无法在主线程上执行网络操作,您应该使用单独的线程执行这些类型的活动(例如使用AsyncTask)。
答案 1 :(得分:-1)
正如异常明确指出的那样,Caused by: android.os.NetworkOnMainThreadException
您不应该使用主线程进行网络通信。如果您必须执行此操作,请使用以下代码。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
然而,不鼓励使用此方法,因为它可能导致ANR,活动无响应。因此,您应该更好地使用异步任务方法或服务类。