0

I have a problem with the call of the two Runnable classes DownloadVideo and ReadVideo2. I want to call them in my Activity class Client but I have an error like that: "Can't create handler inside thread that has not called Looper.prepare()"

I saw answers in other forum but I can not implement it in my situation.

Client :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textView1);

    Connexion connexion = new Connexion();
    connexion.execute();

}

// Connexion avec le serveur via Socket
private class Connexion extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

        ConcurrentLinkedDeque<OutputStream[]> list = new ConcurrentLinkedDeque<>(); //Useless at the moment

        DownloadVideo task = new DownloadVideo(list);
        Thread thread = new Thread(task);
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            //
        }
        ReadVideo2 task2 = new ReadVideo2(list);
        Thread thread2= new Thread(task2);
        thread2.start();

        return null;
    }
}

First Run (Download Video):

public void run() {

    try {
        client = new Socket(IP_SERVER, 9002); // Creating the server socket.

        if (client != null) {
            // Reception video
            InputStream in = client.getInputStream();
            OutputStream out[] = new OutputStream[1];
            out[0] = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Movies/chrono2.mp4");
            byte buf[] = new byte[1024];

            int n;
            while ((n = in.read(buf)) != -1) {
                out[0].write(buf, 0, n);
                //Ajout dans la liste en queue
                list.addLast(out);
                //System.out.println("byte : " + out);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            handler.post(this);
            in.close();
            out[0].close();

            client.close();
        } else {
            System.out.println("Pas de serveur lancé sur le port " + client.getLocalPort() + " ...");
        }
    } catch (UnknownHostException e) {
        System.out.println("Impossible de se connecter au serveur " + IP_SERVER);
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Impossible de se connecter au serveur" + IP_SERVER);
        e.printStackTrace();
    }

Second Run :

View v;
Handler handler = new Handler();
public void run(){
    ReadVideo rv = new ReadVideo();
    rv.launchVideo(v);
    handler.post(this);
}

ReadVideo Activity :

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    launchVideo(v);
}

public void launchVideo(View v) {
    try{
        // Chemin de la vidéo
        Uri videoUri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Movies/chrono2.mp4");

        // Nouvelle activité qui permet de visionner une donnée (dans notre cas, la vidéo)
        Intent intent = new Intent(Intent.ACTION_VIEW);

        // On spécifie la donnée de l'activité ainsi que le MIME (ex: application, text, image, audio, etc.)
        // On veut lancer l'APPLICATION MX Video Player donc le MIME est "application"
        intent.setDataAndType(videoUri, "application/x-mpegURL");

        intent.putExtra(EXTRA_VIDEO_LIST, new Parcelable[] {videoUri});    // Permet d'éviter de lire toutes les vidéos du dossier du chemin envoyé


        //intent.setPackage(MXVP);    // Limite les applications possibles à celle de MX Video Player
        startActivity(intent);
    }
    catch( ActivityNotFoundException e2){
        //  displayToast(getResources().getString(R.string.error_unknownMX)); // Erreur, on affiche un message à l'utilisateur
        // Log.e( "Error", getResources().getString(R.string.error_unknownMX));
    }
}

Thank you and sorry for the code :/

DiR95
  • 49
  • 7
  • 1
    Possible duplicate of [Can't create handler inside thread that has not called Looper.prepare()](http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – shmosel Jun 08 '16 at 00:10
  • 1
    Likely it is a duplicate. It does also look like the RealVideo Activity is being incorrectly started. If RealVideo is an Activity you don't start it like this -> ReadVideo rv = new ReadVideo(); You use a startActivity intent slightly like the one you have in RealVideo - well it has an onCreate so it is incorrect. https://developer.android.com/training/basics/firstapp/starting-activity.html – CmosBattery Jun 08 '16 at 00:29
  • What is this `handler.post(this);` supposed to do? You're in a thread. Make a loop. You can't mix handlers and threads like that. And starting threads doesn't need an asynctask. – zapl Jun 08 '16 at 01:02
  • How can I do this to become correct ? – DiR95 Jun 08 '16 at 05:51

0 Answers0