Jump to content

[Resolvido] App com erros (Android)


Super.D
 Share

Recommended Posts

Boas ppl!

Perdi o meu código antigo (o que funcionava)...

Só que agora estou com um problema relacionado aos botões... Os botões terminam a minha app de forma inesperada... Mas quando eu comento os botões (os listeners) os botões já aparecem...

package com.example.papcasa;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

protected static final int RESULT_SPEECH = 1;
protected static final int REQUEST_OK = 0;

private Button bVoz, bLuz1, bLuz0, bCondicionado1, bCondicionado0, bEstor1, bEstor0;

private String ip = "192.168.1.250";
private int porta = 80;
private int timeout = 5000;

private String arduino = "";

private Socket socket = null;
private DataOutputStream out = null;
private BufferedReader in = null;

private TextToSpeech tts;

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

       if (savedInstanceState == null) {
           getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
       }


       bLuz1 = (Button) findViewById(R.id.bluz1);
       bLuz1.setonclickListener(new View.onclickListener() {
           public void onclick(View v) {
            sendToServer("luz1");
           }
       });

       bCondicionado1 = (Button) findViewById(R.id.bar1);
       bCondicionado1.setonclickListener(new View.onclickListener() {
           public void onclick(View v) {
            sendToServer("ven1");
           }
       });

       bEstor1 = (Button) findViewById(R.id.bestor1);
       bEstor1.setonclickListener(new View.onclickListener() {
           public void onclick(View v) {
            sendToServer("estor1");
           }
       });

       bLuz0 = (Button) findViewById(R.id.bluz0);
       bLuz0.setonclickListener(new View.onclickListener() {
           public void onclick(View v) {
            sendToServer("luz0");
           }
       });

       bCondicionado0 = (Button) findViewById(R.id.bar0);
       bCondicionado0.setonclickListener(new View.onclickListener() {
           public void onclick(View v) {
            sendToServer("ven0");
           }
       });

       bEstor0 = (Button) findViewById(R.id.bestor0);
       bEstor0.setonclickListener(new View.onclickListener() {
           public void onclick(View v) {
            sendToServer("luz1");
           }
       });


       bVoz = (Button) findViewById(R.id.bvoz);
       bVoz.setonclickListener(new View.onclickListener() {
           public void onclick(View v) {


            Thread t = new Thread() {
            public void run() {
            Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "pt-PT");
                     try {
                        startActivityForResult(i, REQUEST_OK);
                    } catch (Exception e) {
                      Toast.makeText(getApplication(), "O programa não suporta reconhecimento de voz.", Toast.LENGTH_LONG).show();
                    }
            }
            }; t.start();
           }
       });

   }


   private void sendToServer(final String msg) {
    Thread conectar = new Thread() {
    public void run() {

    try {

    SocketAddress socketAddress = new InetSocketAddress(ip, porta); 
    socket = new Socket();
socket.connect(socketAddress, timeout);

if (socket.isConnected()) {
out = new DataOutputStream(socket.getOutputStream());
in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));

out.writeBytes(msg);
arduino = in.readLine();
speak();
}

    } catch (Exception e) {
    msgToast(e.getMessage());
    }

    finally {
    try {
    arduino = "";

    if (out != null) {
    out.close();
    }

    if (in != null) {
    in.close();
    }

    if (socket != null) {
    socket.close();
    }

    } catch (Exception e) {
    msgToast(e.getMessage());
    }
    }
    }

    }; conectar.start();
   }

   private void msgToast(String msg) {
    Toast.makeText(this, msg.toString(), Toast.LENGTH_LONG).show();
   }

   private void speak() {
    Thread t = new Thread() {
    public void run() {
    try {
Thread.sleep(800);
tts.speak(arduino, TextToSpeech.QUEUE_FLUSH, null);

} catch (InterruptedException e) {
msgToast(e.getMessage());
}

    }
    }; t.start();
   }

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       if (requestCode==REQUEST_OK  && resultCode==RESULT_OK) {

        ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

               if (text.contains("ligar luz")) {
                sendToServer("luz1");
               } else if(text.contains("desligar luz")) {
                sendToServer("luz0");
               } else if(text.contains("ligar ar condicionado")) {
                sendToServer("ven1");
               } else if(text.contains("desligar ar condicionado")) {
                sendToServer("ven0");
               } else if(text.contains("abrir estor")) {
                sendToServer("estor1");
               } else if(text.contains("fechar estor")) {
                sendToServer("estor0");
               } else if(text.contains("temperatura")) {
                sendToServer("temp");
               } 
       }
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       int id = item.getItemId();
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }

   /**
    * A placeholder fragment containing a simple view.
    */
   public static class PlaceholderFragment extends Fragment {

       public PlaceholderFragment() {
       }

       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
           View rootView = inflater.inflate(R.layout.fragment_main, container, false);
           return rootView;
       }
   }

}
Link to comment
Share on other sites

Vê no log o erro que te dá.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Quando a aplicação crasha existe sempre um stack trace que te é enviado para o log. Sem isso é muito difícil dizer-te qual é o problema.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.