0

我在这个 Android 应用程序中遇到问题。当我按下 button_x 时,它总是崩溃。该程序如下所列。我使用 BufferedReader 从 Internet 读取内容。

public class MainActivity extends Activity {

    Button button_x;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button_x = (Button)findViewById(R.id.button_x);

        button_x.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                URL yahoo = null;
                try {
                    yahoo = new URL("http://www.google.com.tw/");
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                BufferedReader in = null;
                try {
                    in = new BufferedReader(
                                new InputStreamReader(
                                yahoo.openStream()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                String inputLine;

                try {
                    while ((inputLine = in.readLine()) != null)
                        button_x.setBackgroundColor(Color.RED);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });


    }

    @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;
    }

}

我以为是上网权限引起的,所以我在manifest文件上加上了上网权限。

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />"

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.euro.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

谁能告诉我为什么这个程序会崩溃?

4

1 回答 1

1

您不能在 Android 的 UI 线程上进行网络连接。您需要创建一个新的Thread或使用一个AsyncTask. 根据文档中的连接到网络

网络操作可能涉及不可预知的延迟。为防止这导致糟糕的用户体验,请始终在与 UI 不同的线程上执行网络操作。AsyncTask 类提供了一种从 UI 线程触发新任务的最简单方法。

于 2013-10-29T18:12:02.663 回答