Java Android运行时错误:无法执行活动的方法

Java Android运行时错误:无法执行活动的方法,java,android,eclipse,android-intent,Java,Android,Eclipse,Android Intent,并表明: public void downdown(View view) { try { URL url = new URL("http://myserver.com:7005/media/databases/myfile.db"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMet

并表明:

public void downdown(View view) {
    try {

        URL url = new URL("http://myserver.com:7005/media/databases/myfile.db");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
        File SDCardRoot = Environment.getExternalStorageDirectory();
        File file = new File(SDCardRoot,"myfile.db");
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        int totalSize = urlConnection.getContentLength();
        int downloadedSize = 0;
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {

            fileOutput.write(buffer, 0, bufferLength);
            downloadedSize += bufferLength;
            //actualizaProgreso(downloadedSize, totalSize);
        }
        fileOutput.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
错误:

当我按下“下载”按钮时,此按钮调用downdown函数:此函数从服务器下载文件,我得到以下错误:
java.lang.IllegalStateException:无法执行活动的方法

主线程中不能有网络操作

研究异步任务和多线程。

如果你真的想提高你的技能,我建议不要使用线程实例来启动你的进程。我建议使用


以下是如何使用

发布完整StackTrace的指南:它旨在调试内容异常。。。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.downloader"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
public void downdown(View view) {
    new Thread() {
              public void run() {
                 downdown();
              }
      }.start();
   }
   public void downdown(){
 try {

    URL url = new URL("http://myserver.com:7005/media/databases/myfile.db");
    HttpURLConnection urlConnection = (HttpURLConnection)      url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.connect();
    File SDCardRoot = Environment.getExternalStorageDirectory();
    File file = new File(SDCardRoot,"myfile.db");
    FileOutputStream fileOutput = new FileOutputStream(file);
    InputStream inputStream = urlConnection.getInputStream();
    int totalSize = urlConnection.getContentLength();
    int downloadedSize = 0;
    byte[] buffer = new byte[1024];
    int bufferLength = 0;
    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {

        fileOutput.write(buffer, 0, bufferLength);
        downloadedSize += bufferLength;
        //actualizaProgreso(downloadedSize, totalSize);
    }
    fileOutput.close();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}