0

私はAndroidに比較的慣れていないので、このチュートリアルに従おうとしています。

次のコードで小さなプロジェクトを作成しました。

package com.example.revivaltimesv1;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final int LENGTH_LONG = 10;
    private TextView message;// = (TextView)findViewById(R.id.message);

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

        message = (TextView)findViewById(R.id.message);

        Log.i("mine", "TESTING ... TESTING!!!");

        ConnectivityManager conManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
        if( networkInfo != null && networkInfo.isConnected() ){
            Toast.makeText(this, "Established Network Connection!", LENGTH_LONG).show();
            String stringURL = "http://178.79.128.76/GTK/node/7";
            new DownloadStuff().execute(stringURL);
//          new test().execute();
            //message.setText("WORKING");
        }else{
            Toast.makeText(this, "No Network Connection!", LENGTH_LONG).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

//}

/*    private class test extends AsyncTask<String, String, String>{
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            return null;
        }
    }
*/
    private class DownloadStuff extends AsyncTask<String, String, String>{

        protected String doInBackground(String... url) {
            // TODO Auto-generated method stub
            Log.i("CHECK", "OUTSIDE");
            try{
                Log.i("CHECK", "SUCCESS: " + url[0]);
                downloadUrl(url[0]);
                Toast.makeText(getApplicationContext(), "WORKING !!!", LENGTH_LONG).show();
            }catch(IOException i){
                Log.i("CHECK", "NOT so successful");
                Toast.makeText(getApplicationContext(), "ERROR: unable to establish contact with URL", LENGTH_LONG).show();     
            }
            return null;
        }

        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
//          super.onPostExecute(result);
            message.setText("Hurray!");
        }

        // Given a URL, establishes an HttpUrlConnection and retrieves
        // the web page content as a InputStream, which it returns as
        // a string.
        private String downloadUrl(String myurl) throws IOException {
            InputStream is = null;
            // Only display the first 500 characters of the retrieved
            // web page content.
            int len = 500;

            try {
                URL url = new URL(myurl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                // Starts the query
                conn.connect();
                int response = conn.getResponseCode();
                Log.d("DEBUG_TAG", "The response is: " + response);
                is = conn.getInputStream();

                // Convert the InputStream into a string
                String contentAsString = readIt(is, len);
                return contentAsString;

            // Makes sure that the InputStream is closed after the app is
            // finished using it.
            } finally {
                if (is != null) {
                    is.close();
                } 
            }
        }
        public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
            Reader reader = null;
            reader = new InputStreamReader(stream, "UTF-8");        
            char[] buffer = new char[len];
            reader.read(buffer);
            return new String(buffer);
        }
    }

}

アプリは短時間動作しますが、クラッシュして次のフィードバックが表示されます。

02-08 03:39:16.316: E/AndroidRuntime(5106): FATAL EXCEPTION: AsyncTask #1
02-08 03:39:16.316: E/AndroidRuntime(5106): java.lang.RuntimeException: An error occured while executing doInBackground()
02-08 03:39:16.316: E/AndroidRuntime(5106):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at java.lang.Thread.run(Thread.java:1102)
02-08 03:39:16.316: E/AndroidRuntime(5106): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-08 03:39:16.316: E/AndroidRuntime(5106):     at android.os.Handler.<init>(Handler.java:121)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at android.widget.Toast.<init>(Toast.java:68)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at android.widget.Toast.makeText(Toast.java:231)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at com.example.revivaltimesv1.MainActivity$DownloadStuff.doInBackground(MainActivity.java:74)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at com.example.revivaltimesv1.MainActivity$DownloadStuff.doInBackground(MainActivity.java:1)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-08 03:39:16.316: E/AndroidRuntime(5106):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-08 03:39:16.316: E/AndroidRuntime(5106):     ... 4 more

エラースタックの一番下を調べましたが、...4つ以上が私のコードに関連する部分を隠しているようです-私は思います。

4

5 に答える 5

1

実際には、エラーはlogcatスタックトレースに明確に示されています。02-08 03:39:16.316: E/AndroidRuntime(5106): at com.example.revivaltimesv1.MainActivity$DownloadStuff.doInBackground(MainActivity.java:74)

/ UIToasts関数は、理想的にはで実行する必要がありonPostExecute()ます。ただし、で行う必要がある場合は、コードに示されているようにdoInBackground()に入れてください。Runnable

Runnable run = new Runnable() {

    @Override
    public void run() {
        Toast.makeText(getApplicationContext(), "WORKING !!!", LENGTH_LONG).show();

    }
}; MainActivity.this.runOnUiThread(run);

AsyncTasksの詳細については、こちら、特に「4つのステップ」というタイトルのセクションをご覧ください。

于 2013-02-08T03:51:11.147 に答える
1

UI以外のスレッドでトーストを表示することはできません。

UIスレッドにメッセージを投稿し、UIスレッドにそのメッセージを処理させることができます。

于 2013-02-08T03:51:42.987 に答える
1

UI以外のスレッドからToastを使用することはできません。トーストはUIスレッドから呼び出されるため、onPostExecute()から表示できますが、独自のスレッドで実行されるため、doInBackground()からは表示できません。

于 2013-02-08T03:53:00.643 に答える
0

Toastバックグラウンドスレッドの内側を表示しようとしないでください。

private class DownloadStuff extends AsyncTask<String, String, String>{

        protected String doInBackground(String... url) {
            // TODO Auto-generated method stub
            Log.i("CHECK", "OUTSIDE");
            try{
                // ....
                Toast.makeText(getApplicationContext(), "WORKING !!!", LENGTH_LONG).show();
            }catch(IOException i){

                Toast.makeText(getApplicationContext(), "ERROR: unable to establish contact with URL", LENGTH_LONG).show();     
            }
            return null;
}
于 2013-02-08T03:50:12.897 に答える
0

非同期タスクを使用しているときは、メインスレッドで機能していないことを確認してください。そうすれば、を表示できなくなりますToast

AsyncTaskクラスには5つのメソッドがあります

  • onPreExecute()-このメソッドは、バックグラウンド処理が開始される前にUIスレッドで実行されます。
  • doInBackground()-このメソッドはバックグラウンドで実行され、実際のすべての作業が実行されます。
  • publishProgress()-このメソッドは、メソッドから呼び出されdoInBackground() 、バックグラウンドプロセスの進行状況についてUIスレッドに定期的に通知します。このメソッドは、UIプロセスに情報を送信します。
  • onProgressUpdate()doInBackground()-このメソッドは、メソッドがを呼び出すたびにUIスレッドで実行され ますpublishProgress()。このメソッドは、バックグラウンドプロセスから情報を受け取ります。
  • onPostExecute()-このメソッドは、バックグラウンド処理が完了するとUIスレッドで実行されます。

したがって、プロセスステータスの表示などの操作を実行する場合はOnProcessUpdate()、メインスレッドと対話できるため、メソッドを使用する必要があります。

于 2013-02-08T03:56:38.937 に答える