Android : Blank TextView occasionally on LG G2 (Android 4.4.2)

on Wednesday, July 9, 2014


I'm writing a simple application where a Japanese character is displayed in the middle of the screen as a TextView with 4 buttons(home, shuffle, sound, and next) on the bottom. When the next button is pressed, the next character will be displayed in the TextView.


The application works perfectly on the eclipse simulator and my Samsung Galaxy S2 (Android 4.0.4). However, on my LG G2 (Android 4.4.2), every now and then (maybe once every 30 chars), I get a blank TextView when I click the next button.


I did the following experiments:


1) I added some debugging code to dump the value of the character on a Toast before and after the call to setText(), and the correct value is displayed on the Toast.


2) I pressed the 'sound' button when the TextView is blank and verified that the sound button is associated with the correct character.


3) I added one second delay right after the call to setText() to see if I was overwriting the TextView after setting the right character. In which case, I expected the correct character to be set in the TextView first, and then it becomes blank after the one second delay. However, I saw the blank TextView as soon as I clicked the next button.


So, I think, maybe prematurely, setText() is clearing the TextView somehow.


Does anybody know what the problem might be?


Part of my code looks like this:



public class KatakanaFragment extends Fragment {
private ImageButton    mNextButton;
private TextView mCharTextView;

private void updateChar() {
int strId;
Katakana[] activeKatakana;

activeKatakana = KatakanaTable.getActiveChars();
if ((activeKatakana == null) || (activeKatakana.length == 0)) {
Toast.makeText(getActivity(), "No Char Selected", Toast.LENGTH_SHORT).show();
return;
}

if (activeKatakana.length <= KatakanaTable.sCurrentIndex) {
Toast.makeText(getActivity(), "Current Index Too Big", Toast.LENGTH_SHORT).show();
return;
}

strId = activeKatakana[KatakanaTable.sCurrentIndex].getCharId();
mCharTextView.setTextSize(getResources().getDimension(R.dimen.textsize));
mCharTextView.setText(strId);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_katakana, parent, false);

mCharTextView = (TextView)v.findViewById(R.id.char_text_view);

mNextButton = (ImageButton)v.findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (KatakanaTable.getActiveChars().length == 0) {
Intent i = new Intent(getActivity(), MainActivity.class);
startActivity(i);
return;
}
KatakanaTable.sCurrentIndex = (KatakanaTable.sCurrentIndex + 1) % KatakanaTable.getActiveChars().length;
updateChar();
}
});

updateChar();
return v;
}
}

0 comments:

Post a Comment