I have two classes one its an Activity named OficioActivity and the other its a Service named Player, the OficioActivity starts the Service Player and then it calls the methods playPause(), adiantaAudio() and retrocedeAudio() which uses MediaPlayer class, the problem its if it runs on foreground (when screen is off) theres no problem the Player has a onCompletionListener() that plays the next song, but if I click on a button on OficioActivity to go to next song it crashes with a nullpointException.
OficioActivity
public class OficioActivity extends ActionBarActivity implements View.OnClickListener {
private ImageButton btAdianta,btRetrocede,btPlayPause;
private TextView tvOficio;
private Player player;
private Notificacao notification;
private int NOTIFICACAO_ID =1;
private Intent intentService;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oficio);
btAdianta = (ImageButton) findViewById(R.id.ibtn_adiantar);
btRetrocede = (ImageButton) findViewById(R.id.ibtn_retroceder);
btPlayPause = (ImageButton) findViewById(R.id.ibtn_play);
tvOficio = (TextView) findViewById(R.id.tv_oficio);
btAdianta.setOnClickListener(this);
btPlayPause.setOnClickListener(this);
btRetrocede.setOnClickListener(this);
intentService = new Intent(OficioActivity.this,Player.class);
startService(intentService);
}
#Override
protected void onResume() {
if(player==null)
player = new Player();
notification = new Notificacao(OficioActivity.this);
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_oficio, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**Serve para alterar um TextView em tempo de execução
*
* #param tv TextView
* #param texto String
*/
public void runOnThread(final TextView tv, final String texto){
OficioActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
tv.setText(texto);
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ibtn_adiantar:
runOnThread(tvOficio,player.adianta());
break;
case R.id.ibtn_retroceder:
runOnThread(tvOficio,player.retrocede());
break;
case R.id.ibtn_play:
notification.geraNotificacao(NOTIFICACAO_ID,OficioActivity.this);
runOnThread(tvOficio, player.playPause());
break;
}
}
#Override
protected void onDestroy() {
// player.paraAudio();
super.onDestroy();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
player.paraAudio();
stopService(intentService);
finish();
notification.cancelaNotificacao(NOTIFICACAO_ID);
}
return super.onKeyDown(keyCode, event);
}
}
public class Player extends Service implements MediaPlayer.OnCompletionListener {
private final int SALVE_MESA = R.raw.salve_mesa;
private final int AGORA_LABIOS = R.raw.agora_labios_meus;
private final int HUMILDES_OFERECEMOS = R.raw.humildes_oferecemos;
private final int INTRO = R.raw.intro;
private final int ORACAO = R.raw.oracao;
private final int SALVE_RELOGIO = R.raw.salve_relogio;
private final int SALVE_VIRGEM_IMACULADA = R.raw.salve_virgem_imaculada;
private final int SALVE_VIRGEM_TRINDADE = R.raw.salve_virgem_trindade;
private final int SEDE_EM_MEU_FAVOR = R.raw.sede_em_meu_favor;
private final int SALVE_TRONO = R.raw.salve_trono;
private final int SALVE_CIDADE = R.raw.salve_cidade;
private final int ROGAI_A_DEUS = R.raw.rogai_a_deus_vos;
private final int INICIAR_PAUSAR = 0;
private final int ADIANTAR = 1;
private final int RETROCEDER = 2;
private static MediaPlayer media;
private int audioAtual = 1;
private HashMap<Integer, Integer> hashMapAudio, hashMapRetrocede;
private String texto;
private HashMap<Integer, Integer> musicas = new HashMap<>();
public Player() {
hashMapAudio = new HashMap<>();
hashMapAudio.put(1, INTRO);
hashMapAudio.put(2, AGORA_LABIOS);
hashMapAudio.put(3, ORACAO);
hashMapAudio.put(4, SEDE_EM_MEU_FAVOR);
hashMapAudio.put(5, SALVE_MESA);
hashMapAudio.put(6, ORACAO);
hashMapAudio.put(7, SEDE_EM_MEU_FAVOR);
hashMapAudio.put(8, SALVE_TRONO);
hashMapAudio.put(9, ORACAO);
hashMapAudio.put(10, SEDE_EM_MEU_FAVOR);
hashMapAudio.put(11, SALVE_VIRGEM_TRINDADE);
hashMapAudio.put(12, ORACAO);
hashMapAudio.put(13, SEDE_EM_MEU_FAVOR);
hashMapAudio.put(14, SALVE_CIDADE);
hashMapAudio.put(15, ORACAO);
hashMapAudio.put(16, SEDE_EM_MEU_FAVOR);
hashMapAudio.put(17, SALVE_RELOGIO);
hashMapAudio.put(18, ORACAO);
hashMapAudio.put(19, ROGAI_A_DEUS);
hashMapAudio.put(20, SALVE_VIRGEM_IMACULADA);
hashMapAudio.put(21, ORACAO);
hashMapAudio.put(22, HUMILDES_OFERECEMOS);
}
/**
* Retorna o texto equivalente ao audio tocado
*
* #return String
*/
private String recuperaTexto() {
switch (audioAtual) {
case 1:
texto = OficioEnum.INTRO.texto;
break;
case 2:
texto = OficioEnum.AGORA_LABIOS_MEUS.texto;
break;
case 3:
texto = OficioEnum.ORACAO.texto;
break;
case 4:
texto = OficioEnum.SEDE_EM_MEU_FAVOR.texto;
break;
case 5:
texto = OficioEnum.MESA.texto;
break;
case 6:
texto = OficioEnum.ORACAO.texto;
break;
case 7:
texto = OficioEnum.SEDE_EM_MEU_FAVOR.texto;
break;
case 8:
texto = OficioEnum.TRONO.texto;
break;
case 9:
texto = OficioEnum.ORACAO.texto;
break;
case 10:
texto = OficioEnum.SEDE_EM_MEU_FAVOR.texto;
break;
case 11:
texto = OficioEnum.VIRGEM_DA_TRINDADE.texto;
break;
case 12:
texto = OficioEnum.ORACAO.texto;
break;
case 13:
texto = OficioEnum.SEDE_EM_MEU_FAVOR.texto;
break;
case 14:
texto = OficioEnum.CIDADE.texto;
break;
case 15:
texto = OficioEnum.ORACAO.texto;
break;
case 16:
texto = OficioEnum.SEDE_EM_MEU_FAVOR.texto;
break;
case 17:
texto = OficioEnum.RELOGIO.texto;
break;
case 18:
texto = OficioEnum.ORACAO.texto;
break;
case 19:
texto = OficioEnum.ROGAI_A_DEUS.texto;
break;
case 20:
texto = OficioEnum.VIRGEM_IMACULADA.texto;
break;
case 21:
texto = OficioEnum.ORACAO.texto;
break;
case 22:
texto = OficioEnum.HUMILDES_OFERECEMOS.texto;
break;
}
return texto;
}
/**
* Retorna o audio dependendo da acao se a acao for adiantar retorna proximo audio representado por um Integer
*
* #param acao
* #return int
*/
private int verificaMusica(int acao) {
/*
* Sequencia do oficio
* 1 - intro
* 2 - agora labios meus
* 3 - oracao
* 4 - sede em meu favor
* 5 - salve mesa
* 6 - oracao
* 7 - sede em meu favor
* 8 - salve trono
* 9 - oracao
* 10 -sede em meu favor
* 11 -salve virgem da trindade
* 12 -oracao
* 13 -sede em meu favor
* 14 -salve cidade
* 15 -oracao
* 16 -sede em meu favor
* 17 -salve relogio
* 18 -oracao
* 19 -rogai a Deus vos
* 20 -salve virgem imaculada
* 21 -oracao
* 22 -humildes oferecemos
* */
if (acao == ADIANTAR) {
incrementaAudioAtual();
return hashMapAudio.get(audioAtual);
} else {
decrementaAudioAtual();
return hashMapAudio.get(audioAtual);
}
}
/**
* Incrementa o audio com + 1 até 22
*/
private void incrementaAudioAtual() {
if (audioAtual < 22) {
audioAtual++;
}
}
/**
* DEcrementa o audio com -1 até 1
*/
private void decrementaAudioAtual() {
if (audioAtual > 1) {
audioAtual--;
}
}
public MediaPlayer getMedia() {
return media;
}
/**
* Retorna se o audio está tocando
*
* #return true ou false
*/
public boolean isPlaying() {
return media.isPlaying();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
media = MediaPlayer.create(getApplicationContext(), INTRO);
media.setOnCompletionListener(this);
super.onCreate();
}
/**
* Adianta o audio
*/
public String adianta() {
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(getApplicationContext(), verificaMusica(ADIANTAR));
media.setOnCompletionListener(this);
media.start();
}
return recuperaTexto();
}
/**
* Retrocede o audio
*
* #return String texto
*/
public String retrocede() {
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(getApplicationContext(), verificaMusica(RETROCEDER));
media.start();
}
return recuperaTexto();
}
/**
* Para o audio e define a media como null para liberar memoria
*/
public void paraAudio() {
if (media != null) {
if (media.isPlaying()) {
media.stop();
media.release();
media = null;
} else {
media.release();
media = null;
}
}
}
/**
* Se o audio não estiver tocando toca, caso contrario pausa a musica
*
* #return
*/
public String playPause() {
if (media == null) {
media = MediaPlayer.create(getApplicationContext(), verificaMusica(INTRO));
media.start();
} else {
if (media.isPlaying()) {
media.pause();
} else {
media.start();
}
}
return recuperaTexto();
}
#Override
public void onCompletion(MediaPlayer mp) {
adianta();
}
}
03-17 10:50:12.840: E/AndroidRuntime(1898): FATAL EXCEPTION: main
03-17 10:50:12.840: E/AndroidRuntime(1898): Process: com.renanwillamy.devocionario, PID: 1898
03-17 10:50:12.840: E/AndroidRuntime(1898): java.lang.NullPointerException
03-17 10:50:12.840: E/AndroidRuntime(1898): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
03-17 10:50:12.840: E/AndroidRuntime(1898): at entidades.Player.adianta(Player.java:260)
03-17 10:50:12.840: E/AndroidRuntime(1898): at com.renanwillamy.devocionario.OficioActivity.onClick(OficioActivity.java:103)
03-17 10:50:12.840: E/AndroidRuntime(1898): at android.view.View.performClick(View.java:4438)
03-17 10:50:12.840: E/AndroidRuntime(1898): at android.view.View$PerformClick.run(View.java:18422)
03-17 10:50:12.840: E/AndroidRuntime(1898): at android.os.Handler.handleCallback(Handler.java:733)
03-17 10:50:12.840: E/AndroidRuntime(1898): at android.os.Handler.dispatchMessage(Handler.java:95)
03-17 10:50:12.840: E/AndroidRuntime(1898): at android.os.Looper.loop(Looper.java:136)
03-17 10:50:12.840: E/AndroidRuntime(1898): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-17 10:50:12.840: E/AndroidRuntime(1898): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 10:50:12.840: E/AndroidRuntime(1898): at java.lang.reflect.Method.invoke(Method.java:515)
03-17 10:50:12.840: E/AndroidRuntime(1898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-17 10:50:12.840: E/AndroidRuntime(1898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-17 10:50:12.840: E/AndroidRuntime(1898): at dalvik.system.NativeStart.main(Native Method)
Thanks everyone I use a ServiceConnection and Binder like #Squonk said! – Renan Willamy
Related
The problem SoundPool when I call it in fragments onCreateView, onStart or onResume application to slow down.
And when I try to get the ID by pressing the button, the sound does not appear Logcat writes "SoundPool: sample 1 not READY"
please help me,Any ideas? Thanks!
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_carnivore, container, false);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Sound.createOldSoundPool();
} else {
Sound.createNewSoundPool();
}
mAlligatorSound = Sound.loadSound(getContext(),"alligator.wav");
mBatSound = Sound.loadSound(getContext(), "bat.mp3");
mBearSound = Sound.loadSound(getContext(),"bear.wav");
mBobcatSound=Sound.loadSound(getContext(),"bobcat.mp3");
mCatSound= Sound.loadSound(getContext(),"cat.wav");
mCheetahSound= Sound.loadSound(getContext(),"cheetah.wav");
mCoyoteSound= Sound.loadSound(getContext(),"coyote.wav");
mCrocodileSound= Sound.loadSound(getContext(),"crocodile.wav");
mDogSound= Sound.loadSound(getContext(),"dog.wav");
mFoxSound= Sound.loadSound(getContext(),"fox.mp3");
mHyenaSound= Sound.loadSound(getContext(),"hyena.wav");
mJaguarSound= Sound.loadSound(getContext(),"jaguar.wav");
mKittenSound= Sound.loadSound(getContext(),"kitten.wav");
mLeopardSound= Sound.loadSound(getContext(),"leopard.wav");
mLionSound= Sound.loadSound(getActivity(),"lion.wav");
mPuppySound= Sound.loadSound(getActivity(),"puppy.wav");
mRattleSnakeSound= Sound.loadSound(getActivity(),"rattlesnake.mp3");
mSnakeSound= Sound.loadSound(getActivity(),"snake.wav");
mTigerSound= Sound.loadSound(getActivity(),"tiger.wav");
mWolfSound= Sound.loadSound(getActivity(),"wolf.wav");
int idList[]={R.id.imageButtonAlligator,R.id.imageButtonBat,R.id.imageButtonBear,R.id.imageButtonBobCat,R.id.imageButtonCat,
R.id.imageButtonCheetah,R.id.imageButtonCoyote,R.id.imageButtonCrocodile,R.id.imageButtonDog,R.id.imageButtonFox,
R.id.imageButtonHyena,R.id.imageButtonJaguar,R.id.imageButtonKitten,R.id.imageButtonLeopard,R.id.imageButtonLion,
R.id.imageButtonPuppy,R.id.imageButtonRattlesnake,R.id.imageButtonSnake,R.id.imageButtonTiger,R.id.imageButtonWolf};
for (int id :idList){
ImageButton imageButton = (ImageButton)view.findViewById(id);
imageButton.setOnClickListener(onClickListener);
}
return view;
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonAlligator:
Sound.playSound(mAlligatorSound);
Toast.makeText(view.getContext(),R.string.Alligator, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBat:
Sound.playSound(mBatSound);
Toast.makeText(view.getContext(),R.string.Bat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBear:
Sound.playSound(mBearSound);
Toast.makeText(view.getContext(),R.string.Bear, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBobCat:
Sound.playSound(mBobcatSound);
Toast.makeText(view.getContext(),R.string.BobCat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCat:
Sound.playSound(mCatSound);
Toast.makeText(view.getContext(),R.string.Cat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCheetah:
Sound.playSound(mCheetahSound);
Toast.makeText(view.getContext(),R.string.Cheetah, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCoyote:
Sound.playSound(mCoyoteSound);
Toast.makeText(view.getContext(),R.string.Coyote, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCrocodile:
Sound.playSound(mCrocodileSound);
Toast.makeText(view.getContext(),R.string.Crocodile, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonDog:
Sound.playSound(mDogSound);
Toast.makeText(view.getContext(),R.string.Dog, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonFox:
Sound.playSound(mFoxSound);
Toast.makeText(view.getContext(),R.string.Fox, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonHyena:
Sound.playSound(mHyenaSound);
Toast.makeText(view.getContext(),R.string.Hyena, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonJaguar:
Sound.playSound(mJaguarSound);
Toast.makeText(view.getContext(),R.string.Jaguar, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonKitten:
Sound.playSound(mKittenSound);
Toast.makeText(view.getContext(),R.string.Kitten, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonLeopard:
Sound.playSound(mLeopardSound);
Toast.makeText(view.getContext(),R.string.Leopard, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonLion:
Sound.playSound(mLionSound);
Toast.makeText(view.getContext(),R.string.Lion, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonPuppy:
Sound.playSound(mPuppySound);
Toast.makeText(view.getContext(),R.string.Puppy, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonRattlesnake:
Sound.playSound(mRattleSnakeSound);
Toast.makeText(view.getContext(),R.string.RattleSnake, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonSnake:
Sound.playSound(mSnakeSound);
Toast.makeText(view.getContext(),R.string.Snake, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonTiger:
Sound.playSound(mTigerSound);
Toast.makeText(view.getContext(),R.string.Tiger, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonWolf:
Sound.playSound(mWolfSound);
Toast.makeText(view.getContext(),R.string.Wolf, Toast.LENGTH_SHORT).show();
break;
}
}
};
When you click on the sound does not work when the method is called Sound.loadSound (..., ...), and Logcat writes "SoundPool: sample 1 not READY"
switch (v.getId()) {
case R.id.imageButtonAlligator:
mAlligatorSound = Sound.loadSound(getContext(),"alligator.wav");
Sound.playSound(mAlligatorSound);
Toast.makeText(view.getContext(),R.string.Alligator, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBat:
mBatSound = Sound.loadSound(getContext(), "bat.mp3");
Sound.playSound(mBatSound);
Toast.makeText(view.getContext(),R.string.Bat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBear:
mBearSound = Sound.loadSound(getContext(),"bear.wav");
Sound.playSound(mBearSound);
Toast.makeText(view.getContext(),R.string.Bear, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBobCat:
mBobcatSound=Sound.loadSound(getContext(),"bobcat.mp3");
Sound.playSound(mBobcatSound);
Toast.makeText(view.getContext(),R.string.BobCat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCat:
mCatSound= Sound.loadSound(getContext(),"cat.wav");
Sound.playSound(mCatSound);
Toast.makeText(view.getContext(),R.string.Cat, Toast.LENGTH_SHORT).show();
break;
this is th Static Method Sound
public class Sound {
public static SoundPool mSoundPool;
public static AssetManager mAssetManager;
public static int mStreamID;
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void createNewSoundPool() {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSoundPool = new SoundPool.Builder()
.setAudioAttributes(attributes)
.build();
}
#SuppressWarnings("deprecation")
public static void createOldSoundPool() {
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
}
public static int playSound(int sound) {
if (sound > 0) {
mStreamID = mSoundPool.play(sound, 1, 1, 1, 0, 1);
}
return mStreamID;
}
public static int loadSound(Context context , String fileName) {
mAssetManager = context.getAssets();
AssetFileDescriptor afd;
try {
afd = mAssetManager.openFd(fileName);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Не могу загрузить файл " + fileName,
Toast.LENGTH_SHORT).show();
return -1;
}
return mSoundPool.load(afd, 1);
}
}
Before doing anything with the soundpool, after you instantiate it, you need to set a listener to know when its actually ready to be used, it takes time to load the sound into memory: do this:
boolean loaded = false;//class variable
//in onCreate:
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
then before you use the soundPool you need to check if Loaded is true.
For example:
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
public class ClassHoler {
public static List<Car> carsHolder;
}
In an Activity I used like this.
#Override
public void onClick(View v) {
ClassHoler.carsHolder = null;
}
In my app I got a java.lang.NullPointerException on ClassHoler.carsHolder = null;
How could be this ?
LogCat:
java.lang.NullPointerException
com.souche.fengche.ui.activity.workbench.customer.AddCustomerActivity.void onClick(android.view.View)(SourceFile:282)
android.view.View.performClick(View.java:4472)
android.view.View$PerformClick.run(View.java:18795)
android.os.Handler.handleCallback(Handler.java:808)
android.os.Handler.dispatchMessage(Handler.java:103)
android.os.Looper.loop(Looper.java:193)
android.app.ActivityThread.main(ActivityThread.java:5332)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
dalvik.system.NativeStart.main(Native Method)
Source File:
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(name.getWindowToken(), 0);
switch (v.getId()) {
case R.id.gentle_man:
check(gentleManRadio);
unCheck(ladyRadio);
break;
case R.id.lady:
check(ladyRadio);
unCheck(gentleManRadio);
break;
case R.id.source:
getSources();
mPicker.showAtLocation(parent, Gravity.CENTER, 0, 0);
break;
case R.id.customer_requirement_remark:
Intent intent = new Intent(this, AddRequireRemarkActivity.class);
intent.putExtra(Constant.USER_ID, userID);
startActivityForResult(intent, ADD_REMARK);
break;
case R.id.yes:
check(yesRadio);
unCheck(noRadio);
watchedCars.setVisibility(View.VISIBLE);
line.setVisibility(View.VISIBLE);
break;
case R.id.no:
check(noRadio);
unCheck(yesRadio);
watchedCars.setVisibility(View.GONE);
line.setVisibility(View.GONE);
break;
case R.id.watched_cars:
intent = new Intent(this, AddWatchedCarActivity.class);
intent.putExtra(Constant.CAR_ID, mWatchedCarsString);
startActivity(intent);
break;
case R.id.level:
getLevels();
break;
case R.id.next_visit_time:
mSelectDate.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
break;
case R.id.save_my_cusotmer:
if (name.getText().toString().trim().length() > 5) {
name.setError("名字不能超过五个字");
return;
}
//clear class holder
282 ------> ClassHoler.carsHolder = null;
mLoadingDialog.show();
saveMyCustomer();
break;
}
}
only use is here:
private void initView() {
mLoadingDialog = new LoadingDialog(this);
List<Car> cars;
if (ClassHoler.carsHolder == null) {
cars = new ArrayList<>();
} else {
cars = ClassHoler.carsHolder;
ClassHoler.carsHolder = null;
}
}
Is there some extreme condition my code got NullPointerException ?
I am working on the Bluetooth based project.I am using tabs for including different pages.While including button onclick listener the app is crashed. I referred bluetooth chat from sdk. In Setupchat() I am trying to identify buttons and assigning onclicklisteners. But the app crashed at the onclicklistner. I checked logcat Its showing null pointer exception. Here is the logcat output.
08-11 16:25:08.003: E/AndroidRuntime(16481): FATAL EXCEPTION: main
08-11 16:25:08.003: E/AndroidRuntime(16481): Process: com.dispenser, PID: 16481
08-11 16:25:08.003: E/AndroidRuntime(16481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dispenser/com.dispenser.MainActivity}: java.lang.NullPointerException
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.app.ActivityThread.access$800(ActivityThread.java:139)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.os.Looper.loop(Looper.java:136)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.app.ActivityThread.main(ActivityThread.java:5086)
08-11 16:25:08.003: E/AndroidRuntime(16481): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 16:25:08.003: E/AndroidRuntime(16481): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 16:25:08.003: E/AndroidRuntime(16481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-11 16:25:08.003: E/AndroidRuntime(16481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-11 16:25:08.003: E/AndroidRuntime(16481): at dalvik.system.NativeStart.main(Native Method)
08-11 16:25:08.003: E/AndroidRuntime(16481): Caused by: java.lang.NullPointerException
08-11 16:25:08.003: E/AndroidRuntime(16481): at com.dispenser.MainActivity.setupChat(MainActivity.java:223)
08-11 16:25:08.003: E/AndroidRuntime(16481): at com.dispenser.MainActivity.onStart(MainActivity.java:195)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.app.Activity.performStart(Activity.java:5258)
08-11 16:25:08.003: E/AndroidRuntime(16481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
08-11 16:25:08.003: E/AndroidRuntime(16481): ... 11 more
I know when this nullpointerexception comes. if i use the button without identify the button, The nullpointerexception raises. But I really don't know what is this. Please help me out.
This is the 195th line and 223
package com.dispenser;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.dispenser.adapter.TabsPagerAdapter;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener, OnClickListener {
private ViewPager viewPager;
private TabsPagerAdapter mTabAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Diagnostic", "Set" ,"Settings"};
// Debugging
private static final String TAG = "Main";
private static final boolean D = true;
// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
// Key names received from the BluetoothChatService Handler
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";
// Intent request codes
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;
// Layout Views
//private TextView mTitle;
private EditText mPS1,mPS2,mPS3,mFT,mFW,valve1,valve2,sPS1,sPS2,sPS3;
private Button mSendButtonOn,mEnable,mDisable,mSet;
private Button mSendButtonOff;
// Name of the connected device
private String mConnectedDeviceName;
// String buffer for outgoing messages
private StringBuffer mOutStringBuffer;
// Local Bluetooth adapter
private BluetoothAdapter mBluetoothAdapter;
// Member object for the chat services
private ChatService mChatService;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (D) {
Log.e(TAG, "+++ ON CREATE +++");
}
// Set up the window layout
//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
// R.layout.custom_title);
// Set up the custom title
// mTitle = (TextView) findViewById(R.id.title_left_text);
// mTitle.setText(R.string.app_name);
// mTitle = (TextView) findViewById(R.id.title_right_text);
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available",
Toast.LENGTH_LONG).show();
finish();
return;
}
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mTabAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mTabAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.scan:
// Launch the DeviceListActivity to see devices and do scan
Intent serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
return true;
case R.id.discoverable:
// Ensure this device is discoverable by others
ensureDiscoverable();
return true;
}
return false;
}
#Override
public void onStart() {
super.onStart();
if (D) {
Log.e(TAG, "++ ON START ++");
}
// If BT is not on, request that it be enabled.
// setupChat() will then be called during onActivityResult
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the chat session
} else {
if (mChatService == null) {
setupChat();
}
}
}
private void setupChat() {
mSet=(Button) findViewById(R.id.setBtn);
Log.d(TAG, "setupChat()");
// Diagnostic
mPS1=(EditText) findViewById(R.id.ps1);
mPS2=(EditText) findViewById(R.id.ps2);
mPS3=(EditText) findViewById(R.id.ps3);
mFT=(EditText) findViewById(R.id.ft);
mFW=(EditText) findViewById(R.id.fw);
valve1=(EditText) findViewById(R.id.valve1);
valve2=(EditText) findViewById(R.id.valve2);
//Set
sPS1=(EditText) findViewById(R.id.pressure1);
sPS2=(EditText) findViewById(R.id.pressure2);
sPS3=(EditText) findViewById(R.id.pressure3);
// mSet.setOnClickListener(MainActivity.this);
// Preference
// mSendButtonOff.setOnClickListener(MainActivity.this);
// Initialize the BluetoothChatService to perform bluetooth connections
mChatService = new ChatService(this, mHandler);
// Initialize the buffer for outgoing messages
mOutStringBuffer = new StringBuffer("");
}
#Override
public synchronized void onResume() {
super.onResume();
if (D) {
Log.e(TAG, "+ ON RESUME +");
}
// Performing this check in onResume() covers the case in which BT was
// not enabled during onStart(), so we were paused to enable it...
// onResume() will be called when ACTION_REQUEST_ENABLE activity
// returns.
if (mChatService != null) {
// Only if the state is STATE_NONE, do we know that we haven't
// started already
if (mChatService.getState() == ChatService.STATE_NONE) {
// Start the Bluetooth chat services
mChatService.start();
}
}
}
#Override
public synchronized void onPause() {
super.onPause();
if (D) {
Log.e(TAG, "- ON PAUSE -");
}
}
#Override
public void onDestroy() {
super.onDestroy();
// Stop the Bluetooth chat services
if (mChatService != null) {
mChatService.stop();
}
if (D) {
Log.e(TAG, "--- ON DESTROY ---");
}
}
private void ensureDiscoverable() {
if (D) {
Log.d(TAG, "ensure discoverable");
}
if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
/**
* Sends a message.
*
* #param message
* A string of text to send.
*/
private void sendMessage(String message) {
// Check that we're actually connected before trying anything
if (mChatService.getState() != ChatService.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT)
.show();
return;
}
// Check that there's actually something to send
if (message.length() > 0) {
// XXX !!!
message = message + "\r\n"; // terminate for pc bluetooth spp server
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
mChatService.write(send);
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
// mOutEditText.setText(mOutStringBuffer);
}
}
// The Handler that gets information back from the BluetoothChatService
private final Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if (D) {
Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
}
switch (msg.arg1) {
case ChatService.STATE_CONNECTED:
Toast.makeText(getBaseContext(), R.string.title_connected_to+" " +mConnectedDeviceName, Toast.LENGTH_LONG).show();
// mTitle.append();
// mConversationArrayAdapter.clear();
break;
case ChatService.STATE_CONNECTING:
Toast.makeText(getBaseContext(), R.string.title_connecting, Toast.LENGTH_LONG).show();
break;
case ChatService.STATE_LISTEN:
case ChatService.STATE_NONE:
Toast.makeText(getBaseContext(), R.string.title_not_connected, Toast.LENGTH_LONG).show();
//mTitle.setText();
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
// mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
final String readMessage = new String(readBuf, 0, 50);
// mConversationArrayAdapter.add(mConnectedDeviceName + ": "
// + readMessage);
Runnable done = new Runnable()
{
public void run()
{
String[] b=readMessage.split(",");
for(int i=0;i<b.length;i++){
// Toast.makeText(getBaseContext(), b[i], Toast.LENGTH_SHORT).show();
if(b[i].contains("PS1")){
String[] c=b[i].split("=");
if(c.length==2){
mPS1.setText(c[1]);
}
}
else if(b[i].contains("PS2")){
String[] c=b[i].split("=");
if(c.length==2){
mPS2.setText(c[1]);
}
}
else if(b[i].contains("PS3")){
String[] c=b[i].split("=");
if(c.length==2){
mPS3.setText(c[1]);
}
}
else if(b[i].contains("LPM")){
String[] c=b[i].split("=");
if(c.length==2){
mFW.setText(c[1]);
}
}
else{
}
// mPS1.append(b[i]);
}
}
};
done.run();
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(),
"Connected to " + mConnectedDeviceName,
Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(),
msg.getData().getString(TOAST), Toast.LENGTH_SHORT)
.show();
break;
}
}
};
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (D) {
Log.d(TAG, "onActivityResult " + resultCode);
}
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String address = data.getExtras().getString(
DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
BluetoothDevice device = mBluetoothAdapter
.getRemoteDevice(address);
// Attempt to connect to the device
mChatService.connect(device);
}
break;
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
setupChat();
} else {
// User did not enable Bluetooth or an error occured
Log.d(TAG, "BT not enabled");
Toast.makeText(this, R.string.bt_not_enabled_leaving,
Toast.LENGTH_SHORT).show();
finish();
}
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.setBtn:
if(sPS1.getText().length()!=0){
float pressure1=Float.parseFloat(sPS1.getText().toString());
if(pressure1>0.0F && pressure1<20.0F)
sendMessage("PS1="+pressure1);
}
else{
sPS1.setError("Enter PS1");
}
if(sPS2.getText().length()!=0){
float pressure2=Float.parseFloat(sPS2.getText().toString());
if(pressure2>0.0F && pressure2<20.0F)
sendMessage("PS2="+pressure2);
}
else{
sPS2.setError("Enter PS2");
}
if(sPS3.getText().length()!=0){
float pressure3=Float.parseFloat(sPS3.getText().toString());
if(pressure3>0.0F && pressure3<20.0F)
sendMessage("PS3="+pressure3);
}
else{
sPS3.setError("Enter PS3");
}
break;
default:
break;
}
}
}
You haven't instantiated your button
To use the onClickListener() of button, you should make it's instance in java file
From question: If i use the button without identify the button
You need to do the following:
Button button = (Button) findViewById(R.id.button_identity);
Bottom Line: You should have identity either from XML or from java code.
Hope it works.
The mistake what I made is I tried to include onclicklistener in Mainactivity Instead of including each tab activity. I removed the onclick listener from MainActivity and identify the button and included onclicklistner in corresponding activity tab. Thats solved my problem.
I'm developing an app based on the BluetoothChat example where I have the main activity with 2 buttons:
1. Find devices
2. Configuration
With the first button I search for devices with the same code as in the mentioned example.
With the second button, I enter to another activity where i have 3 seekbars with textviews under each one showing the current seekbar's value.
What I need is to modify BluetoothChat example's code to send an array with this 3 values instead of text.
This is the code of this activity where I'm trying to develop this.
How can I do this?
/*********************
*
* ONCREATE
*
********************/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration);
bar1 = (SeekBar)findViewById(R.id.fastbar);
bar1.setOnSeekBarChangeListener(this);
progress1 = (TextView)findViewById(R.id.out1);
bar2 = (SeekBar)findViewById(R.id.climbbar);
bar2.setOnSeekBarChangeListener(this);
progress2 = (TextView)findViewById(R.id.out2);
bar3 = (SeekBar)findViewById(R.id.platformbar);
bar3.setOnSeekBarChangeListener(this);
progress3 = (TextView)findViewById(R.id.out3);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
/**Initialize the buffer for outgoing messages*/
GlobalVar.mOutStringBuffer = new StringBuffer("");
}
/*********************
*
* SEEKBAR
*
********************/
/**Method to get each seekbar's value and save it into a textview*/
#Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
switch (bar.getId()) {
case R.id.fastbar:
progress1.setText(progress + "%");
break;
case R.id.climbbar:
progress2.setText(progress + "%");
break;
case R.id.platformbar:
progress3.setText(progress + "%");
break;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setSecondaryProgress(seekBar.getProgress());
}
/*********************
*
* ONRESUME
*
********************/
/**Load seekbar's values from sharedpreferences*/
#Override
protected void onResume() {
super.onResume();
loadedProgress1 = mPreferences.getInt(Key_PROGRESS_1, 50);
bar1.setProgress(loadedProgress1);
loadedProgress2 = mPreferences.getInt(Key_PROGRESS_2, 90);
bar2.setProgress(loadedProgress2);
loadedProgress3 = mPreferences.getInt(Key_PROGRESS_3, 70);
bar3.setProgress(loadedProgress3);
}
/*********************
*
* ONPAUSE
*
********************/
/**Save seekbar's values on sharedfreferences*/
#Override
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = mPreferences.edit();
savedProgress1 = bar1.getProgress();
editor.putInt(Key_PROGRESS_1, savedProgress1);
savedProgress2 = bar2.getProgress();
editor.putInt(Key_PROGRESS_2, savedProgress2);
savedProgress3 = bar3.getProgress();
editor.putInt(Key_PROGRESS_3, savedProgress3);
editor.commit();
}
/*********************
*
* OPTIONS
*
********************/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/**Inflate the menu; this adds items to the action bar if it is present.*/
getMenuInflater().inflate(R.menu.menu_conf, menu);
return true;
}
/**Options menu where I call the send function*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.send:
sendValues();
break;
case R.id.reset:
resetValues(null);
break;
}
return true;
}
/*********************
*
* SEND & RESET
*
********************/
public void sendValues() {
/**Seekbars values*/
String message1 = progress1.getText().toString();
String message2 = progress2.getText().toString();
String message3 = progress3.getText().toString();
/**Check that we're actually connected before trying anything*/
if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
/**
*
*HERE IS WHERE I'M TRYING TO CREATE A FUNCTION TO SEND THE 3 VALUES IN A STRING TYPE MESSAGE
*THIS CODE IS FROM THE SETUPCHAT() FUNCTION, SO IS WRONG IN THIS CASE
*/
/**Get the message bytes and tell the to write*/
byte[] send = message.getBytes();
GlobalVar.mTransmission.write(send);
/**Reset out string buffer to zero*/
GlobalVar.mOutStringBuffer.setLength(0);
}
public void resetValues() {
defaultValue1 = 50;
bar1.setProgress(defaultValue1);
defaultValue2 = 90;
bar2.setProgress(defaultValue1);
defaultValue3 = 70;
bar3.setProgress(defaultValue1);
}
This is the new function created:
public void sendValues() {
/**Send the seekbars values*/
String message = progress1.getText().toString()+":"+progress2.getText().toString()+":"+progress3.getText().toString();
String[] values = message.split(":");
for (String value : values) {
int number = Integer.valueOf(value);
}
/**Check that we're actually connected before trying anything*/
if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
/**Get the message bytes and tell the Transmission to write*/
byte[] send = message.getBytes();
GlobalVar.mTransmission.write(send);
/**Reset out string buffer to zero*/
GlobalVar.mOutStringBuffer.setLength(0);
}
You can send the values as a string with a regularExpression ex. "5:15:33" and then split it when you receive the string on ":" Like this.
String str = bar1.getText().toString()+":"+bar2.getText().toString()+":"+bar3.getText().toString();
Send str String to the reciver and, you can then get the int's out of the String array like this
String[] values = str.split(":");
for (String value : values) {
int number = Integer.valueOf(value);
}
Have a problem while developing an app based in the BluetoothChat example, but divided in 2 activities.
The main with bluetooth actions ()BTActivity
The chat (BTCommunication)
I have the BluetoothChatService too divided in the following files, but they aren't activities:
Transmission: All the Handler actions
ConnectThread
ConnectedThread
AccpetThread
The app finds de device, starts connecting it and then crashes. I'm trying to find out what i'm doing wrong comparing with the BluetoothChat App, but i don't find the problem.
07-23 10:58:43.076: D/AbsListView(17279): unregisterIRListener() is called
07-23 10:58:43.076: D/AbsListView(17279): unregisterIRListener() is called
07-23 10:58:43.086: D/BluetoothUtils(17279): isSocketAllowedBySecurityPolicy start : device null
07-23 10:58:43.086: W/BluetoothAdapter(17279): getBluetoothService() called with no BluetoothManagerCallback
07-23 10:58:43.106: E/SpannableStringBuilder(17279): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-23 10:58:43.106: E/SpannableStringBuilder(17279): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-23 10:58:43.116: D/AbsListView(17279): onDetachedFromWindow
07-23 10:58:43.116: D/AbsListView(17279): unregisterIRListener() is called
07-23 10:58:43.116: D/AbsListView(17279): onDetachedFromWindow
07-23 10:58:43.116: D/AbsListView(17279): unregisterIRListener() is called
07-23 10:58:44.527: D/AndroidRuntime(17279): Shutting down VM
07-23 10:58:44.527: W/dalvikvm(17279): threadid=1: thread exiting with uncaught exception (group=0x41d58ac8)
07-23 10:58:44.537: E/AndroidRuntime(17279): FATAL EXCEPTION: main
07-23 10:58:44.537: E/AndroidRuntime(17279): java.lang.NullPointerException
07-23 10:58:44.537: E/AndroidRuntime(17279): at com.example.btaplication.BTActivity$1.handleMessage(BTActivity.java:288)
07-23 10:58:44.537: E/AndroidRuntime(17279): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 10:58:44.537: E/AndroidRuntime(17279): at android.os.Looper.loop(Looper.java:137)
07-23 10:58:44.537: E/AndroidRuntime(17279): at android.app.ActivityThread.main(ActivityThread.java:5328)
07-23 10:58:44.537: E/AndroidRuntime(17279): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 10:58:44.537: E/AndroidRuntime(17279): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 10:58:44.537: E/AndroidRuntime(17279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-23 10:58:44.537: E/AndroidRuntime(17279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-23 10:58:44.537: E/AndroidRuntime(17279): at dalvik.system.NativeStart.main(Native Method)
07-23 10:58:55.428: I/Process(17279): Sending signal. PID: 17279 SIG: 9
/ Here the Main activity
public class BTActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button1 = (Button) findViewById(R.id.boton1);
final Button button2 = (Button) findViewById(R.id.boton2);
final Button button4 = (Button) findViewById(R.id.boton4);
final Button button5 = (Button) findViewById(R.id.boton5);
button5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
lanzarComunicacion (null);
}
});
GlobalVar.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (GlobalVar.mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LanzarBusqueda(null);
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!GlobalVar.mBluetoothAdapter.isDiscovering()) {
Context context = getApplicationContext();
CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GlobalVar.mBluetoothAdapter.disable();
Context context = getApplicationContext();
CharSequence text = "TURNING OFF BLUETOOTH";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, 15);
toast.show();
}
});
}
#Override
public void onStart() {
super.onStart();
if (!GlobalVar.mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, GlobalVar.REQUEST_ENABLE_BT);
}
else {
if (GlobalVar.mTransmission == null) setupCaller();
}
}
#Override
public void onResume() {
super.onResume();
if (GlobalVar.mTransmission != null) {
/**Only if the state is STATE_NONE, do we know that we haven't started already*/
if (GlobalVar.mTransmission.getState() == GlobalVar.STATE_NONE) {
}
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case GlobalVar.REQUEST_CONNECT_DEVICE:
/**When DeviceListActivity returns with a device to connect*/
if (resultCode == Activity.RESULT_OK) {
connectDevice(data);
}
case GlobalVar.REQUEST_ENABLE_BT:
/**When the request to enable Bluetooth returns*/
if (resultCode == Activity.RESULT_OK) {
/**Bluetooth is now enabled, so set up a chat session*/
setupCaller();
} else {
/**User did not enable Bluetooth or an error occurred*/
Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
finish();
}
break;
}
}
private void connectDevice(Intent data) {
/**Get the device MAC address*/
String address = data.getExtras().getString(DeviceListDialog.EXTRA_DEVICE_ADDRESS);
/**Get the BluetoothDevice object*/
BluetoothDevice device = GlobalVar.mBluetoothAdapter.getRemoteDevice(address);
/**Attempt to connect to the device*/
try{
GlobalVar.mTransmission.connect(device);
}catch(Exception ex) {
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/**Inflate the menu; this adds items to the action bar if it is present.*/
getMenuInflater().inflate(R.menu.bt, menu);
return true;
}
public void lanzarComunicacion (View view) {
Intent i = new Intent(this, BTCommunication.class);
startActivity(i);
}
public void LanzarBusqueda (View view) {
Intent serverintent = new Intent(this, DeviceListDialog.class);
startActivityForResult(serverintent, GlobalVar.REQUEST_CONNECT_DEVICE);
}
private final void setStatus(int resId) {
final ActionBar actionBar = getActionBar();
actionBar.setSubtitle(resId);
}
private final void setStatus(CharSequence subTitle) {
final ActionBar actionBar = getActionBar();
actionBar.setSubtitle(subTitle);
}
/**
* The Handler that gets information back from the Transmission
*/
private final Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case GlobalVar.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case GlobalVar.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, GlobalVar.mConnectedDeviceName));
GlobalVar.mConversationArrayAdapter.clear();
break;
case GlobalVar.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case GlobalVar.STATE_LISTEN:
case GlobalVar.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case GlobalVar.MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
/**construct a string from the buffer*/
String writeMessage = new String(writeBuf);
GlobalVar.mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case GlobalVar.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
/**construct a string from the valid bytes in the buffer*/
String readMessage = new String(readBuf, 0, msg.arg1);
GlobalVar.mConversationArrayAdapter.add(GlobalVar.mConnectedDeviceName+": " + readMessage);
break;
case GlobalVar.MESSAGE_DEVICE_NAME:
/**save the connected device's name*/
GlobalVar.mConnectedDeviceName = msg.getData().getString(GlobalVar.DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to " + GlobalVar.mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case GlobalVar.MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(GlobalVar.TOAST), Toast.LENGTH_SHORT).show();
break;
}
}
};
public void setupCaller() {
/**Initialize the Transmission to perform bluetooth connections*/
GlobalVar.mTransmission = new Transmission(this, mHandler);
}
}
/ The Chat Avtivity
public class BTCommunication extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**Set up the window layout*/
setContentView(R.layout.chat);
/**Start the Bluetooth chat services*/
GlobalVar.mTransmission.start();
setupChat(); //PROBAMOS A LLAMAR AQUI\\
}
public void setupChat() {
/**Initialize the array adapter for the conversation thread*/
GlobalVar.mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
GlobalVar.mConversationView = (ListView) findViewById(R.id.in);
GlobalVar.mConversationView.setAdapter(GlobalVar.mConversationArrayAdapter);
/**Initialize the compose field with a listener for the return key*/
GlobalVar.mOutEditText = (EditText) findViewById(R.id.edit_text_out);
GlobalVar.mOutEditText.setOnEditorActionListener(mWriteListener);
/**Initialize the send button with a listener that for click events*/
GlobalVar.mSendButton = (Button) findViewById(R.id.button_send);
GlobalVar.mSendButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/**Send a message using content of the edit text widget*/
TextView view = (TextView) findViewById(R.id.edit_text_out);
String message = view.getText().toString();
sendMessage(message);
}
});
/**Initialize the Transmission to perform bluetooth connections*/
//Done it in BTActivity in the function "setupCaller()"\\
/**Initialize the buffer for outgoing messages*/
GlobalVar.mOutStringBuffer = new StringBuffer("");
}
#Override
public void onDestroy() {
super.onDestroy();
/**Stop the Bluetooth chat services*/
if (GlobalVar.mTransmission != null) GlobalVar.mTransmission.stop();
}
/**
* Sends a message.
* #param message A string of text to send.
*/
public void sendMessage(String message) {
/**Check that we're actually connected before trying anything*/
if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
/**Check that there's actually something to send*/
if (message.length() > 0) {
/**Get the message bytes and tell the BluetoothChatService to write*/
byte[] send = message.getBytes();
GlobalVar.mTransmission.write(send);
/**Reset out string buffer to zero and clear the edit text field*/
GlobalVar.mOutStringBuffer.setLength(0);
GlobalVar. mOutEditText.setText(GlobalVar.mOutStringBuffer);
}
}
/**The action listener for the EditText widget, to listen for the return key*/
private final TextView.OnEditorActionListener mWriteListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
/**If the action is a key-up event on the return key, send the message*/
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
String message = view.getText().toString();
sendMessage(message);
}
return true;
}
};
}
/The Transmission file:
public class Transmission {
/**
* Constructor. Prepares a new session.
* #param context The UI Activity Context
* #param handler A Handler to send messages back to the UI Activity
*/
public Transmission(Context context, Handler handler) {
GlobalVar.mAdapter = BluetoothAdapter.getDefaultAdapter();
GlobalVar.mState = GlobalVar.STATE_NONE;
GlobalVar.mHandler = handler;
}
/**
* Set the current state of the connection
* #param state An integer defining the current connection state
*/
public synchronized void setState(int state) {
GlobalVar.mState = state;
/**Give the new state to the Handler so the UI Activity can update*/
GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
}
/**
* Return the current connection state.
*/
public synchronized int getState() {
return GlobalVar.mState;
}
/**
* Start the chat service. Specifically start AcceptThread to begin a
* session in listening (server) mode. Called by the Activity onResume()
*/
public synchronized void start() {
/**Cancel any thread attempting to make a connection*/
if (GlobalVar.mConnectThread != null) {GlobalVar.mConnectThread.cancel(); GlobalVar.mConnectThread = null;}
/**Cancel any thread currently running a connection*/
if (GlobalVar.mConnectedThread != null) {GlobalVar.mConnectedThread.cancel(); GlobalVar.mConnectedThread = null;}
setState(GlobalVar.STATE_LISTEN);
/**Start the thread to listen on a BluetoothServerSocket*/
if (GlobalVar.mAcceptThread == null) {
GlobalVar.mAcceptThread = new AcceptThread();
GlobalVar.mAcceptThread.start();
}
}
/**
* Start the ConnectThread to initiate a connection to a remote device.
* #param device The BluetoothDevice to connect
*/
public synchronized void connect(BluetoothDevice device) {
/**Cancel any thread attempting to make a connection*/
if (GlobalVar.mState == GlobalVar.STATE_CONNECTING) {
if (GlobalVar.mConnectThread != null) {GlobalVar.mConnectThread.cancel(); GlobalVar.mConnectThread = null;}
}
/**Cancel any thread currently running a connection*/
if (GlobalVar.mConnectedThread != null) {GlobalVar.mConnectedThread.cancel(); GlobalVar.mConnectedThread = null;}
/**Start the thread to connect with the given device*/
GlobalVar.mConnectThread = new ConnectThread(device);
GlobalVar.mConnectThread.start();
setState(GlobalVar.STATE_CONNECTING);
}
/**
* Start the ConnectedThread to begin managing a Bluetooth connection
* #param socket The BluetoothSocket on which the connection was made
* #param device The BluetoothDevice that has been connected
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
/**Cancel the thread that completed the connection*/
if (GlobalVar.mConnectThread != null) {GlobalVar.mConnectThread.cancel(); GlobalVar.mConnectThread = null;}
/**Cancel any thread currently running a connection*/
if (GlobalVar.mConnectedThread != null) {GlobalVar.mConnectedThread.cancel(); GlobalVar.mConnectedThread = null;}
/**Cancel the accept thread because we only want to connect to one device*/
if (GlobalVar.mAcceptThread != null) {
GlobalVar.mAcceptThread.cancel();
GlobalVar.mAcceptThread = null;
}
/**Start the thread to manage the connection and perform transmissions*/
GlobalVar.mConnectedThread = new ConnectedThread(socket);
GlobalVar.mConnectedThread.start();
/**Send the name of the connected device back to the UI Activity*/
Message msg = GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(GlobalVar.DEVICE_NAME, device.getName());
msg.setData(bundle);
GlobalVar.mHandler.sendMessage(msg);
setState(GlobalVar.STATE_CONNECTED);
}
/**
* Stop all threads
*/
public synchronized void stop() {
if (GlobalVar.mConnectThread != null) {
GlobalVar.mConnectThread.cancel();
GlobalVar.mConnectThread = null;
}
if (GlobalVar.mConnectedThread != null) {
GlobalVar.mConnectedThread.cancel();
GlobalVar.mConnectedThread = null;
}
if (GlobalVar.mAcceptThread != null) {
GlobalVar.mAcceptThread.cancel();
GlobalVar.mAcceptThread = null;
}
setState(GlobalVar.STATE_NONE);
}
/**
* Write to the ConnectedThread in an unsynchronized manner
* #param out The bytes to write
* #see ConnectedThread#write(byte[])
*/
public void write(byte[] out) {
/**Create temporary object*/
ConnectedThread r;
/**Synchronize a copy of the ConnectedThread*/
synchronized (this) {
if (GlobalVar.mState != GlobalVar.STATE_CONNECTED) return;
r = GlobalVar.mConnectedThread;
}
/**Perform the write unsynchronized*/
r.write(out);
}
/**
* Indicate that the connection attempt failed and notify the UI Activity.
*/
public void connectionFailed() {
/**Send a failure message back to the Activity*/
Message msg = GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(GlobalVar.TOAST, "Unable to connect device");
msg.setData(bundle);
GlobalVar.mHandler.sendMessage(msg);
/**tart the service over to restart listening mode*/
Transmission.this.start();
}
/**
* Indicate that the connection was lost and notify the UI Activity.
*/
public void connectionLost() {
/**Send a failure message back to the Activity*/
Message msg = GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(GlobalVar.TOAST, "Device connection was lost");
msg.setData(bundle);
GlobalVar.mHandler.sendMessage(msg);
/**Start the service over to restart listening mode*/
Transmission.this.start();
}
}
/The Accept, connect and connected threads are the same as in BleutoothChat app but each one has it's own file.
07-23 10:58:44.537: E/AndroidRuntime(17279): at com.example.btaplication.BTActivity$1.handleMessage(BTActivity.java:288)
You need more condition in your Handler. It crashes because you have a null pointer in your BTActivity.
I exactly had this problem 2 weeks ago (I was trying to change text of a textview in a Handler). So, in my Handler I just put :
if(mTextView == null) {mTextView = (TextView) findViewById(R.id.tv)}
even if mTextView was define before. In that case you will be sure that your attribute is define, and you will avoid NPE
Hope it helps