0

我正在尝试在我的 Android 项目中使用 IRClib( http://moepii.sourceforge.net/irclib/javadoc/org/schwering/irc/lib/package-summary.html )。我在项目的“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
4

2 回答 2

0

您不能在主线程上执行网络操作,您应该使用单独的线程(例如使用AsyncTask)执行这些类型的活动。

于 2013-06-29T07:27:20.520 回答
-1

正如例外明确指出的那样,Caused by: android.os.NetworkOnMainThreadException您不应使用主线程进行网络通信。如果您必须执行它,请使用下面的代码。

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 

但是,不鼓励使用此方法,因为它可能导致 ANR,即 Activity Not Responding。所以你最好使用异步任务方法或服务类。

于 2013-06-29T07:28:07.720 回答