我想在我的Android項目中使用IRClib(http://moepii.sourceforge.net/irclib/javadoc/org/schwering/irc/lib/package-summary.html) 。我在項目的'libs'文件夾中添加了irclib.jar。另外,我已經將該文件添加爲庫,以便它出現在項目的「引用庫」部分中。我還在Android清單中添加了「使用互聯網」權限。不能在Android項目中使用IRClib
我有以下初步代碼。
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
ohh .. :(好的..感謝大家的快速幫助!:) – tigerden