MACM Posted March 24, 2014 Report Share Posted March 24, 2014 Ola ... Bom dia Tenho alguma luzes em programação como pascal e Visual Basic, e resolvi arriscar agora em Android para passar o tempo e fazer algumas apps relacionadas com a fotografia. Estou a ler uns livros e a fazer os exemplo mas logo no 2º exemplo não consigo por o programar a funcionar. o Exemplo é o básico... a calculadora que soma dois números... mas ao escrever o seguinte código btsoma.setonclickListener(new View.onclickListener(){ public void onclick (View arg0){ double num1 = Double.parseDouble(ednumero1.getText().toString()); double num2 = Double.parseDouble(ednumero2.getText().toString()); double res = num1 + num2; AlertDialog.Builder dialogo = new AlertDialog.Builder(AppSoma.this); dialogo.setTitle("Aviso"); dialogo.setMessage("Soma: "); dialogo.setNeutralButton("Ok", null); dialogo.show(); o emulador apenas me dá a seguinte mensagem "Unfortunaly, Somanumeros has stopped". Se retirar a linha de código funciona mas o botão não tem a utilidade dele.... Alguém me pode ajudar? Link to comment Share on other sites More sharing options...
KTachyon Posted March 26, 2014 Report Share Posted March 26, 2014 (edited) Ok... só agora reparei que parece existir um bug no forum. Confirma que escreveste On Click Listener, iniciados com maiúsculas. Edited March 26, 2014 by KTachyon “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 More sharing options...
MACM Posted March 26, 2014 Author Report Share Posted March 26, 2014 KTachyon Ok... só agora reparei que parece existir um bug no forum. Confirma que escreveste On Click Listener, iniciados com maiúsculas. está escrito assim "btsoma.setonclickListener(new View.onclickListener(){" Já me disseram que o erro é por usar a versão 4.3 do SDK... mas pesquisei pelo android.develop e nada Link to comment Share on other sites More sharing options...
KTachyon Posted March 26, 2014 Report Share Posted March 26, 2014 On Click Listener, sem espaços e com as maiúsculas, ou tal e qual como está escrito no teu post? Isto porque o fórum está a apresentar onclickListener mesmo escrevendo com maiúsculas. “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 More sharing options...
MACM Posted March 26, 2014 Author Report Share Posted March 26, 2014 On Click Listener, sem espaços e com as maiúsculas, ou tal e qual como está escrito no teu post? Isto porque o fórum está a apresentar onclickListener mesmo escrevendo com maiúsculas. É como dissestes.... o forum muda o que digitei..... que cena On Click Listener, sem espaços e com as maiúsculas, ou tal e qual como está escrito no teu post? Isto porque o fórum está a apresentar onclickListener mesmo escrevendo com maiúsculas. é como dissetes ...... o forum esta a mudar o que escrevo...... que cena... mas o codigo é como tu escrevestes Link to comment Share on other sites More sharing options...
KTachyon Posted March 26, 2014 Report Share Posted March 26, 2014 O ideal é veres o que te diz no output, para se perceber o erro que te está a dar. “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 More sharing options...
MACM Posted March 27, 2014 Author Report Share Posted March 27, 2014 03-27 04:59:25.550 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/Effect_Tick.ogg03-27 04:59:25.550 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/Effect_Tick.ogg 03-27 04:59:25.550 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/Effect_Tick.ogg 03-27 04:59:25.580 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/Effect_Tick.ogg 03-27 04:59:25.590 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/Effect_Tick.ogg 03-27 04:59:25.590 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/KeypressStandard.ogg 03-27 04:59:25.590 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/KeypressSpacebar.ogg 03-27 04:59:25.590 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/KeypressDelete.ogg 03-27 04:59:25.590 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/KeypressReturn.ogg 03-27 04:59:25.590 392-436/system_process E/SoundPool﹕ error loading /system/media/audio/ui/KeypressInvalid.ogg 03-27 04:59:49.660 769-769/com.android.settings E/BluetoothAdapter﹕ Bluetooth binder is null 03-27 04:59:49.690 769-769/com.android.settings E/BluetoothAdapter﹕ Bluetooth binder is null 03-27 04:59:50.350 769-769/com.android.settings E/BluetoothAdapter﹕ Bluetooth binder is null 03-27 04:59:55.870 898-907/com.android.mms E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. java.lang.Throwable: Explicit termination method 'release' not called at dalvik.system.CloseGuard.open(CloseGuard.java:184) at android.drm.DrmManagerClient.<init>(DrmManagerClient.java:258) at com.google.android.mms.pdu.PduPersister.<init>(PduPersister.java:288) at com.google.android.mms.pdu.PduPersister.getPduPersister(PduPersister.java:296) at com.android.mms.transaction.TransactionService.onNewIntent(TransactionService.java:224) at com.android.mms.transaction.TransactionService$ServiceHandler.handleMessage(TransactionService.java:621) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.os.HandlerThread.run(HandlerThread.java:61) 03-27 05:02:13.104 1027-1027/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory) 03-27 05:02:13.104 1027-1027/? E/android.os.Debug﹕ failed to load memtrack module: -2 03-27 05:02:22.874 1083-1083/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory) 03-27 05:02:22.874 1083-1083/? E/android.os.Debug﹕ failed to load memtrack module: -2 03-27 05:02:24.794 1093-1093/pt.android.app E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: pt.android.app, PID: 1093 java.lang.RuntimeException: Unable to start activity ComponentInfo{pt.android.app/pt.android.app.AppSoma}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at pt.android.app.AppSoma.onCreate(AppSoma.java:41) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) 03-27 05:02:33.554 392-406/system_process E/WindowManager﹕ Starting window AppWindowToken{b2fef880 token=Token{b3040740 ActivityRecord{b2fa4bf8 u0 pt.android.app/.AppSoma t2}}} timed out Da isto.... Link to comment Share on other sites More sharing options...
KTachyon Posted March 27, 2014 Report Share Posted March 27, 2014 Aquilo que precisas de saber daí são estas duas linhas: Caused by: java.lang.NullPointerException at pt.android.app.AppSoma.onCreate(AppSoma.java:41) Algures na linha 41 do ficheiro AppSoma.java estás a utilizar um objecto que não foi inicializado. “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 More sharing options...
MACM Posted March 27, 2014 Author Report Share Posted March 27, 2014 Aquilo que precisas de saber daí são estas duas linhas: Caused by: java.lang.NullPointerException at pt.android.app.AppSoma.onCreate(AppSoma.java:41) Algures na linha 41 do ficheiro AppSoma.java estás a utilizar um objecto que não foi inicializado. A linha 41 é: "btsomar.setonclickListener(new View.onclickListener(){" Isto está a dar-me muita volta a cabeça porque não tenho um ficheiro "main.xlm" como nos exemplos mas sim dois ficheiros um chamado "activity_app_soma.xml" e um chamado "fragment_app_soma.xml", e os objectos... botão e caixa de textos estão no "fragment" mas se mudar os objectos para o outro ficheiro dá erro logo no inicio quando defino o botão ( Button btsomar = (Button)findViewById(R.id.btsomar)😉. Link to comment Share on other sites More sharing options...
KTachyon Posted March 27, 2014 Report Share Posted March 27, 2014 Porque o botão não existe. Já carregaste o activity_app_soma? “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 More sharing options...
MACM Posted March 28, 2014 Author Report Share Posted March 28, 2014 Porque o botão não existe. Já carregaste o activity_app_soma? Button btsomar = (Button)findViewById(R.id.btsomar) Isto activa o butão e não dá erro setContentView(R.layout.activity_main); Este código carrega o activity_app_soma? Link to comment Share on other sites More sharing options...
KTachyon Posted March 28, 2014 Report Share Posted March 28, 2014 o setContentView recebe uma View, daí que terias que a carregar tal como fazes com o botão. Mas ao fazeres o setContentView estarias a substituir a que lá tinhas. “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 More sharing options...
MACM Posted March 31, 2014 Author Report Share Posted March 31, 2014 mas há algo que me escapa..... pelo seguinte, ao fazer estes codigos: ednumero1 = (EditText) findViewById(R.id.numero1); ednumero2 = (EditText) findViewById(R.id.numero2); Button btsoma = (Button) findViewById(R.id.btsomar); o programa corre sem dar erros Ao escrever: btsoma.setonclickListener(new View.onclickListener(){ public void onclick (View arg0){ double num1 = Double.parseDouble(ednumero1.getText().toString()); double num2 = Double.parseDouble(ednumero2.getText().toString()); double res = num1 + num2; AlertDialog.Builder dialogo = new AlertDialog.Builder(MainActivity.this); dialogo.setTitle("Aviso"); dialogo.setMessage("Soma: " + res); dialogo.setNeutralButton("OK", null ); dialogo.show(); } }); ou mesmo só ter esta linha: double num1 = Double.parseDouble(ednumero1.getText().toString()); double num2 = Double.parseDouble(ednumero2.getText().toString()); dá erro...... acho que me escapa alguma comunicação entre o xml e o Java Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now