Fórum ImageButton (Android) #486090
20/07/2014
0
Estou com um pequena problema em uma aplicação que não consegui identificar. Meu código não enxerga o ID dos meus imageButtons, e não consegui adivinhar qual é o problema. Fiz umas pesquisas no google e tentei resolver mais nada. Já aconteceu isso com mais alguém? Segue abaixo os códigos:
Classe que não enxerga os ID
package br.livro.android.cap14.banco;
import br.livro.android.cap14.banco.Carro.Carros;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
public class EditarCarro extends Activity {
static final int RESULT_SALVAR = 1;
static final int RESULT_EXCLUIR = 2;
// Campos texto
private EditText campoNome;
private EditText campoPlaca;
private EditText campoAno;
private Long id;
protected void OnCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.form_editar_carro);
campoNome = (EditText) findViewById(R.id.campoNome);
campoPlaca = (EditText) findViewById(R.id.campoPlaca);
campoAno = (EditText) findViewById(R.id.campoAno);
id = null;
Bundle extras = getIntent().getExtras();
// Se for para editar, recuperar os valores...
if (extras != null) {
id = extras.getLong(Carros._ID);
if (id != null) {
// é uma edição, busca o carro...
Carro c = buscarCarro(id);
campoNome.setText(c.nome);
campoPlaca.setText(c.placa);
campoAno.setText(String.valueOf(c.ano));
}
}
ImageButton btCancelar = (ImageButton) findViewById(R.id.btCancelar);
btCancelar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setResult(RESULT_CANCELED);
// fecha a janela
finish();
}
});
// Listener para salvar o carro
ImageButton btSalvar = (ImageButton) findViewById(R.id.btSalvar);
btSalvar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
salvar();
}
});
ImageButton btExcluir = (ImageButton) findViewById(R.id.btExcluir);
if (id == null) {
// se id está nulo, não pode excluir
btExcluir.setVisibility(View.INVISIBLE);
} else {
// Listenner para excluir o carro
btExcluir.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
excluir();
}
});
}
}
protected void onPause() {
super.onPause();
// Cancela para não ficar nada na tela pendente
setResult(RESULT_CANCELED);
// fecha a tela
finish();
}
public void salvar() {
int ano = 0;
try {
ano = Integer.parseInt(campoAno.getText().toString());
} catch (NumberFormatException e) {
// tratar isto em aplicações reais
}
Carro carro = new Carro();
if (id != null) {
// É uma atualização
carro.id = id;
}
carro.nome = campoNome.getText().toString();
carro.placa = campoPlaca.getText().toString();
carro.ano = ano;
// salvar
salvarCarro(carro);
// OK
setResult(RESULT_OK, new Intent());
// fecha a janela
finish();
}
public void excluir() {
if (id != null) {
excluirCarro(id);
}
// ok
setResult(RESULT_OK, new Intent());
// fecha a janela
finish();
}
// buscar carro pelo ID
protected Carro buscarCarro(long id) {
return CadastroCarros.repositorio.buscarCarro(id);
}
// salvar o carro
protected void salvarCarro(Carro carro) {
CadastroCarros.repositorio.salvar(carro);
}
// Excluir o carro
protected void excluirCarro(Long id) {
CadastroCarros.repositorio.deletar(id);
}
}
Layouy com os imageButtons
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000044" >
<TableRow>
<TextView
android:id="@+id/text1"
android:text="@string/lblNome"
android:textColor="#ffffff" />
<EditText
android:id="@+id/campoNome"
android:width="240dp" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/text2"
android:text="@string/lblPlaca"
android:textColor="#ffffff" />
<EditText android:id="@+id/campoPlaca" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/text3"
android:text="@string/lblAno"
android:textColor="#ffffff" />
<EditText
android:id="@+id/campoAno" />
</TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<ImageButton
android:id="@+id/btCancelar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cancelar"
android:contentDescription="@string/description"
/>
<ImageButton
android:id="@+id/btSalvar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/salvar"
android:contentDescription="@string/description"
/>
<ImageButton
android:id="@+id/btExcluir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/excluir"
android:contentDescription="@string/description"
/>
</LinearLayout>
</TableLayout>as linhas que contem algum imagebutton como a: [codeImageButton btExcluir = (ImageButton) findViewById(R.id.btExcluir);][/code] não o encontra nem a pal! Quando eu dou ctrl + espaço também não aparece.
Fábio Carvalho
Curtir tópico
+ 0Posts
20/07/2014
Eduardo Pessoa
Gostei + 0
20/07/2014
Fábio Carvalho
Sim, não entendi. Quando voltei aqui e liguei o PC funcionou tudo certo. Só que meu projeto não ta abrindo, a aplicação ta travando. No logcat aparece:
tentando descobrir o porque ta acontecendo isso. Se alguém quiser ajudar eu vou deixar o código completo aqui, é uma aplicação de cadastro de veículos com banco do SQLite
Aplicação de cadastro de veículo - Android
Gostei + 0
20/07/2014
Eduardo Pessoa
Gostei + 0
20/07/2014
Fábio Carvalho
Gostei + 0
20/07/2014
Eduardo Pessoa
Gostei + 0
20/07/2014
Fábio Carvalho
Gostei + 0
20/07/2014
Eduardo Pessoa
Gostei + 0
21/07/2014
Fábio Carvalho
07-20 23:14:46.512: I/ARMAssembler(51): generated scanline__00000077:03010104_00008001_00000000 [ 89 ipp] (110 ins) at [0xb5fa5460:0xb5fa5618] in 0 ns
07-20 23:14:46.672: E/SQLiteLog(1095): (1) no such table: carro
07-20 23:14:46.682: E/livro(1095): Erro ao buscar os carros: android.database.sqlite.SQLiteException: no such table: carro (code 1): , while compiling: SELECT _id, nome, placa, ano FROM carro
07-20 23:14:46.772: D/AndroidRuntime(1095): Shutting down VM
07-20 23:14:46.772: W/dalvikvm(1095): threadid=1: thread exiting with uncaught exception (group=0xb2ababa8)
[b]07-20 23:14:46.812: E/AndroidRuntime(1095): FATAL EXCEPTION: main[/b]
07-20 23:14:46.812: E/AndroidRuntime(1095): Process: br.livro.android.cap14.banco, PID: 1095
07-20 23:14:46.812: E/AndroidRuntime(1095): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.livro.android.cap14.banco/br.livro.android.cap14.banco.CadastroCarros}: java.lang.NullPointerException
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.os.Handler.dispatchMessage(Handler.java:102)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.os.Looper.loop(Looper.java:136)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-20 23:14:46.812: E/AndroidRuntime(1095): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 23:14:46.812: E/AndroidRuntime(1095): at java.lang.reflect.Method.invoke(Method.java:515)
07-20 23:14:46.812: E/AndroidRuntime(1095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-20 23:14:46.812: E/AndroidRuntime(1095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-20 23:14:46.812: E/AndroidRuntime(1095): at dalvik.system.NativeStart.main(Native Method)
07-20 23:14:46.812: E/AndroidRuntime(1095): Caused by: java.lang.NullPointerException
07-20 23:14:46.812: E/AndroidRuntime(1095): at br.livro.android.cap14.banco.RepositorioCarro.listarCarros(RepositorioCarro.java:135)
07-20 23:14:46.812: E/AndroidRuntime(1095): at br.livro.android.cap14.banco.CadastroCarros.atualizarLista(CadastroCarros.java:30)
07-20 23:14:46.812: E/AndroidRuntime(1095): at br.livro.android.cap14.banco.CadastroCarros.onCreate(CadastroCarros.java:25)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.app.Activity.performCreate(Activity.java:5231)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-20 23:14:46.812: E/AndroidRuntime(1095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-20 23:14:46.812: E/AndroidRuntime(1095): ... 11 more
07-20 23:14:46.842: W/ActivityManager(385): Force finishing activity br.livro.android.cap14.banco/.CadastroCarros
07-20 23:14:46.852: I/Choreographer(548): Skipped 171 frames! The application may be doing too much work on its main thread.
07-20 23:14:47.112: I/Choreographer(548): Skipped 32 frames! The application may be doing too much work on its main thread.
07-20 23:14:47.172: I/WindowManager(385): Screenshot max retries 4 of Token{b3096e28 ActivityRecord{b2ff3578 u0 br.livro.android.cap14.banco/.CadastroCarros t3 f}} appWin=Window{b30085b8 u0 Starting br.livro.android.cap14.banco} drawState=4
07-20 23:14:47.172: W/WindowManager(385): Screenshot failure taking screenshot for (480x800) to layer 21010
07-20 23:14:47.422: D/dalvikvm(385): GC_FOR_ALLOC freed 433K, 23% free 5936K/7620K, paused 79ms, total 82ms
07-20 23:14:47.422: I/dalvikvm-heap(385): Grow heap (frag case) to 6.482MB for 635812-byte allocation
07-20 23:14:47.522: D/dalvikvm(385): GC_FOR_ALLOC freed 1K, 21% free 6556K/8244K, paused 98ms, total 98ms
07-20 23:14:47.922: W/ActivityManager(385): Activity pause timeout for ActivityRecord{b2ff3578 u0 br.livro.android.cap14.banco/.CadastroCarros t3 f}
07-20 23:14:48.092: I/Choreographer(385): Skipped 52 frames! The application may be doing too much work on its main thread.
07-20 23:14:48.262: I/Choreographer(548): Skipped 55 frames! The application may be doing too much work on its main thread.
07-20 23:14:48.342: I/Choreographer(385): Skipped 55 frames! The application may be doing too much work on its main thread.
07-20 23:14:48.792: I/ActivityManager(385): Killing 1049:com.svox.pico/u0a37 (adj 15): empty for 2828s
07-20 23:14:48.932: I/Choreographer(385): Skipped 37 frames! The application may be doing too much work on its main thread.
07-20 23:14:49.162: I/Choreographer(385): Skipped 33 frames! The application may be doing too much work on its main thread.
07-20 23:14:49.312: I/Choreographer(385): Skipped 36 frames! The application may be doing too much work on its main thread.
07-20 23:14:49.482: I/Choreographer(385): Skipped 42 frames! The application may be doing too much work on its main thread.
07-20 23:14:50.722: I/Process(1095): Sending signal. PID: 1095 SIG: 9
07-20 23:14:50.862: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:50.862: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:50.922: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:50.932: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:50.992: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:50.992: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:51.002: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:51.012: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:51.012: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:51.012: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-20 23:14:51.092: E/SoundPool(385): error loading /system/media/audio/ui/KeypressStandard.ogg
07-20 23:14:51.092: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
07-20 23:14:51.132: E/SoundPool(385): error loading /system/media/audio/ui/KeypressSpacebar.ogg
07-20 23:14:51.132: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
07-20 23:14:51.182: E/SoundPool(385): error loading /system/media/audio/ui/KeypressDelete.ogg
07-20 23:14:51.182: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
07-20 23:14:51.212: E/SoundPool(385): error loading /system/media/audio/ui/KeypressReturn.ogg
07-20 23:14:51.212: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
07-20 23:14:51.222: E/SoundPool(385): error loading /system/media/audio/ui/KeypressInvalid.ogg
07-20 23:14:51.222: W/AudioService(385): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
07-20 23:14:51.222: W/AudioService(385): onLoadSoundEffects(), Error -1 while loading samples
07-20 23:14:51.422: I/Choreographer(385): Skipped 167 frames! The application may be doing too much work on its main thread.
07-20 23:14:51.762: I/Choreographer(385): Skipped 70 frames! The application may be doing too much work on its main thread.
07-20 23:14:52.102: I/Choreographer(385): Skipped 71 frames! The application may be doing too much work on its main thread.
07-20 23:14:52.492: W/InputMethodManagerService(385): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b2ea2d88 attribute=null, token = android.os.BinderProxy@b3065de8
07-20 23:14:52.812: I/ActivityManager(385): Process br.livro.android.cap14.banco (pid 1095) has died.
07-20 23:15:01.002: D/ConnectivityService(385): Sampling interval elapsed, updating statistics ..
07-20 23:15:01.152: D/ConnectivityService(385): Done.
07-20 23:15:01.152: D/ConnectivityService(385): Setting timer for 720seconds
Gostei + 0
21/07/2014
Andre Santos..
em Project -> clean
ai vai la de novo
Project -> build Project
Gostei + 0
21/07/2014
Fábio Carvalho
em Project -> clean
ai vai la de novo
Project -> build Project
Também não deu, tinha só uns import's que não estavam sendo utilizados.
Gostei + 0
21/07/2014
Fábio Carvalho
Gostei + 0
21/07/2014
Eduardo Pessoa
[img]http://arquivo.devmedia.com.br/forum/imagem/310862-20140721-131129.jpg[/img]
Gostei + 0
21/07/2014
Fábio Carvalho
[img]http://arquivo.devmedia.com.br/forum/imagem/310862-20140721-131129.jpg[/img]
Ela é uma biblioteca de suporte que o eclipse incluí automaticamente.
Gostei + 0
21/07/2014
Eduardo Pessoa
Gostei + 0
21/07/2014
Fábio Carvalho
Não, ele vai criar está pasta só uma vez. Não estou entendendo bem o que, que a google tem feito com a SDK , antigamente não tinha essa appcombat, aí colocaram ela, só que toda vez que criava um projeto era criado uma nova appcombat. Mas nesta ultima versão agora, ele tem criado só uma appcombat para todos os projetos.
Sei apenas que o intuíto do appcompat_v7 vem para alinhar a compatibilidade de versões antigas de aplicativos para Androids mais novos.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)