Android Timer SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length - java

After looking fo ther error at least at the fourm and found only answers that speak about 3rd party device keyborad or somthing that caused it(Samsung devices at least)(Iam using my one plus One).
Ive setted a timer for 10 seconds and every time it reached it 3rd time of runnig (i refresh the activity) i got that error SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length.
TimerAttack class code:
#SuppressLint("NewApi")
public class TimeAttack extends Activity implements AnimationListener {
String pic;//תמונה של הדגל
Button answer1;//תשובות
Button answer2;
Button answer3;
Button answer4;
Button hint;
TextView guess;
TextView numOfGuess;
TextView score;
TextView scorenum;
TextView textViewTime;
MediaPlayer mpHint;
MediaPlayer mpNext;
MediaPlayer mpWrong;
Animation animationfadein;
Animation animationfadeout;
String fn;
Guesses G;
Score s;
Timer t;
Button [] b = new Button[4];
DatabaseHandler db = new DatabaseHandler(this);
List<String>WrongAnswers=new ArrayList<String>();
int countG=0;//count Guesses
int countA=0;//count right Answers
int countQ=0;//count Questions
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
score =(TextView)findViewById(R.id.score);
scorenum =(TextView)findViewById(R.id.scorenum);
scorenum.setText(String.valueOf(s.score));
guess =(TextView)findViewById(R.id.guesses);
numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
textViewTime = (TextView) findViewById(R.id.textViewTime);
hint =(Button)findViewById(R.id.hint);
hint.setOnClickListener(hintOnClickListener);
mpHint = MediaPlayer.create(this,R.raw.hint_sound);
mpNext = MediaPlayer.create(this, R.raw.next_flag);
mpWrong = MediaPlayer.create(this, R.raw.wrong_answer);
animationfadein = AnimationUtils.loadAnimation(this, R.anim.fade_in);
animationfadeout = AnimationUtils.loadAnimation(this, R.anim.fade_out);
textViewTime.setText(t.time);
final CounterClass timer = new CounterClass(11000, 1000);
timer.start();
Flags f = new Flags();
Random r = new Random();//הדגל שיבחר לשאלה
int num = r.nextInt(160);//Up
f = db.getFlag(num);//הצגת הדגל הרנדומלי שיצא
fn = f.getName().toString();
pic = f.getImage().toString();
pic_view(pic);//מעבר לפונקציה להשמת התמונה של הדגל במשחק
//מערך ארבע כפתורים כנגד ארבע תשובות
b[0] = (Button)findViewById(R.id.button1);
b[1] = (Button)findViewById(R.id.button2);
b[2] = (Button)findViewById(R.id.button3);
b[3] = (Button)findViewById(R.id.button4);
List<String>Answers=new ArrayList<String>();//מערך תשובות
Answers.add(f.getName().toString());//הוספת התשובה הנכונה
for(int i=1;i<4;i++)
{
num = r.nextInt(200);
String valToAdd1 = db.getFlag(num).getName().toString();
if(!Answers.contains(valToAdd1)){
WrongAnswers.add(valToAdd1);
Answers.add(valToAdd1);
}
}
Collections.shuffle(Answers);//ערבוב התשובות
for(int i=0;i<Answers.size();i++)
{
b[i].setText(Answers.get(i));//השמת התשובות מהמהערך למערך הכפתורים
b[i].startAnimation(animationfadein);
}
}//end of OnCreat
public boolean onOptionsItemSelected(MenuItem item){//actionbar activity
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
#SuppressLint("NewApi")
public void resetQuiz()
{
countQ++;
recreate();
}
private OnClickListener hintOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpHint.start();
/*if(Guesses.numOfGuesses==1)
{
G.setNumOfGuesses(3);
finish();//כאשר מספר הניחושים
return;
}
else*/
G.numOfGuesses++;
numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
int invisblecount=0;
for(int i=0;i<b.length;i++){
if(invisblecount<2){
String buttonText = b[i].getText().toString();
if(buttonText.equals(WrongAnswers.get(0))||buttonText.equals(WrongAnswers.get(1)))
{
b[i].startAnimation(animationfadeout);
b[i].setVisibility(View.INVISIBLE);
invisblecount++;
}
}
}
}
};
public void check(View v)
{
Log.d("yes", fn);
Button b = (Button)v;
String text = b.getText().toString();
if(text.equals(fn))
{
mpNext.start();
s.score+=5;
resetQuiz();
}
else
{
mpWrong.start();
Guesses.numOfGuesses++;
countG=Guesses.numOfGuesses;
/*if(Guesses.numOfGuesses==1)
{
//G.setNumOfGuesses(3);
//finish();//כאשר מספר הניחושים
return;
}else
Guesses.numOfGuesses--;*/
if(s.score>0)
s.score-=5;
scorenum.setText(String.valueOf(s.score));
numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
private void pic_view(String pic2) {
// TODO Auto-generated method stub
//גישה לדגל לפי שמו וייבוא התמונה
Log.d("Result from pic function " , pic2);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
String uri ="#drawable/";
uri += pic2;
int imageResource = getResources().getIdentifier(uri, pic2, getPackageName());//הצוות התמונה
Drawable res= getResources().getDrawable(imageResource);//ציור התמונה
imageView.setImageDrawable(res);
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Animatoin Stoped", Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
#SuppressLint("NewApi")
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textViewTime.setText(hms);
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
Log.d("Time", "More" + textViewTime.getText().toString());
Toast toast = Toast.makeText(getApplicationContext(), "Time's up Lets see what you made so far...", Toast.LENGTH_SHORT);
toast.show();
Intent en = new Intent(TimeAttack.this,Person.class);
String extra = scorenum.getText().toString();
en.putExtra("Score",extra);
startActivity(en);
}
}
}
stacktrace:
04-26 11:26:15.590: I/System.out(5670): 00:00:08
04-26 11:26:15.610: I/System.out(5670): 00:00:03
04-26 11:26:16.590: I/System.out(5670): 00:00:07
04-26 11:26:16.620: I/System.out(5670): 00:00:02
04-26 11:26:17.590: I/System.out(5670): 00:00:06
04-26 11:26:17.630: I/System.out(5670): 00:00:01
04-26 11:26:18.590: I/System.out(5670): 00:00:05
04-26 11:26:19.130: D/yes(5670): Finland
04-26 11:26:19.320: D/into(5670): com.example.flagsgame.Flags#42cc7998
04-26 11:26:19.320: D/into(5670): 147
04-26 11:26:19.320: D/Result from pic function(5670): palau
04-26 11:26:19.420: D/into(5670): com.example.flagsgame.Flags#428a6720
04-26 11:26:19.420: D/into(5670): 119
04-26 11:26:19.460: D/into(5670): com.example.flagsgame.Flags#42d8f3d0
04-26 11:26:19.460: D/into(5670): 37
04-26 11:26:19.490: D/into(5670): com.example.flagsgame.Flags#42b7ff38
04-26 11:26:19.490: D/into(5670): 183
04-26 11:26:19.510: E/MediaPlayer(5670): Should have subtitle controller already set
04-26 11:26:19.510: E/MediaPlayer(5670): Should have subtitle controller already set
04-26 11:26:19.510: E/MediaPlayer(5670): Should have subtitle controller already set
04-26 11:26:19.510: I/System.out(5670): 00:00:10
04-26 11:26:19.510: D/Time(5670): More00:00:01
04-26 11:26:19.530: I/Timeline(5670): Timeline: Activity_launch_request id:com.example.flagsgame time:36376629
04-26 11:26:19.710: W/MediaPlayer-JNI(5670): MediaPlayer finalized without being released
04-26 11:26:19.710: W/MediaPlayer-JNI(5670): MediaPlayer finalized without being released
04-26 11:26:19.720: W/MediaPlayer-JNI(5670): MediaPlayer finalized without being released
04-26 11:26:19.720: W/Bundle(5670): Key Score expected Integer but value was a java.lang.String. The default value 0 was returned.
04-26 11:26:19.730: W/Bundle(5670): Attempt to cast generated internal exception:
04-26 11:26:19.730: W/Bundle(5670): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
04-26 11:26:19.730: W/Bundle(5670): at android.os.Bundle.getInt(Bundle.java:1000)
04-26 11:26:19.730: W/Bundle(5670): at android.content.Intent.getIntExtra(Intent.java:4620)
04-26 11:26:19.730: W/Bundle(5670): at com.example.flagsgame.Person.onCreate(Person.java:40)
04-26 11:26:19.730: W/Bundle(5670): at android.app.Activity.performCreate(Activity.java:5231)
04-26 11:26:19.730: W/Bundle(5670): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-26 11:26:19.730: W/Bundle(5670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-26 11:26:19.730: W/Bundle(5670): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
04-26 11:26:19.730: W/Bundle(5670): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-26 11:26:19.730: W/Bundle(5670): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
04-26 11:26:19.730: W/Bundle(5670): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 11:26:19.730: W/Bundle(5670): at android.os.Looper.loop(Looper.java:136)
04-26 11:26:19.730: W/Bundle(5670): at android.app.ActivityThread.main(ActivityThread.java:5146)
04-26 11:26:19.730: W/Bundle(5670): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 11:26:19.730: W/Bundle(5670): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 11:26:19.730: W/Bundle(5670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
04-26 11:26:19.730: W/Bundle(5670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
04-26 11:26:19.730: W/Bundle(5670): at dalvik.system.NativeStart.main(Native Method)
04-26 11:26:19.800: I/System.out(5670): 00:00:04
04-26 11:26:19.910: I/Timeline(5670): Timeline: Activity_idle id: android.os.BinderProxy#42b40b18 time:36377008
04-26 11:26:19.920: E/SpannableStringBuilder(5670): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 11:26:19.920: E/SpannableStringBuilder(5670): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 11:26:19.930: E/SpannableStringBuilder(5670): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 11:26:19.930: E/SpannableStringBuilder(5670): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 11:26:20.510: I/System.out(5670): 00:00:09
04-26 11:26:20.800: I/System.out(5670): 00:00:03
04-26 11:26:21.510: I/System.out(5670): 00:00:08
04-26 11:26:21.800: I/System.out(5670): 00:00:02
04-26 11:26:22.510: I/System.out(5670): 00:00:07
04-26 11:26:22.800: I/System.out(5670): 00:00:01
04-26 11:26:23.510: I/System.out(5670): 00:00:06
04-26 11:26:24.050: D/Time(5670): More00:00:01
04-26 11:26:24.060: I/Timeline(5670): Timeline: Activity_launch_request id:com.example.flagsgame time:36381154
04-26 11:26:24.150: W/Bundle(5670): Key Score expected Integer but value was a java.lang.String. The default value 0 was returned.
04-26 11:26:24.150: W/Bundle(5670): Attempt to cast generated internal exception:
04-26 11:26:24.150: W/Bundle(5670): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
04-26 11:26:24.150: W/Bundle(5670): at android.os.Bundle.getInt(Bundle.java:1000)
04-26 11:26:24.150: W/Bundle(5670): at android.content.Intent.getIntExtra(Intent.java:4620)
04-26 11:26:24.150: W/Bundle(5670): at com.example.flagsgame.Person.onCreate(Person.java:40)
04-26 11:26:24.150: W/Bundle(5670): at android.app.Activity.performCreate(Activity.java:5231)
04-26 11:26:24.150: W/Bundle(5670): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-26 11:26:24.150: W/Bundle(5670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-26 11:26:24.150: W/Bundle(5670): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
04-26 11:26:24.150: W/Bundle(5670): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-26 11:26:24.150: W/Bundle(5670): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
04-26 11:26:24.150: W/Bundle(5670): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 11:26:24.150: W/Bundle(5670): at android.os.Looper.loop(Looper.java:136)
04-26 11:26:24.150: W/Bundle(5670): at android.app.ActivityThread.main(ActivityThread.java:5146)
04-26 11:26:24.150: W/Bundle(5670): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 11:26:24.150: W/Bundle(5670): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 11:26:24.150: W/Bundle(5670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
04-26 11:26:24.150: W/Bundle(5670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
04-26 11:26:24.150: W/Bundle(5670): at dalvik.system.NativeStart.main(Native Method)
04-26 11:26:24.270: E/SpannableStringBuilder(5670): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 11:26:24.270: E/SpannableStringBuilder(5670): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 11:26:24.270: I/Timeline(5670): Timeline: Activity_idle id: android.os.BinderProxy#426f66b0 time:36381361
04-26 11:26:24.520: I/System.out(5670): 00:00:05
04-26 11:26:25.520: I/System.out(5670): 00:00:04
04-26 11:26:26.520: I/System.out(5670): 00:00:03
04-26 11:26:27.520: I/System.out(5670): 00:00:02
04-26 11:26:28.520: I/System.out(5670): 00:00:01
04-26 11:26:30.240: D/Time(5670): More00:00:01
04-26 11:26:30.250: I/Timeline(5670): Timeline: Activity_launch_request id:com.example.flagsgame time:36387346
04-26 11:26:30.320: W/Bundle(5670): Key Score expected Integer but value was a java.lang.String. The default value 0 was returned.
04-26 11:26:30.320: W/Bundle(5670): Attempt to cast generated internal exception:
04-26 11:26:30.320: W/Bundle(5670): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
04-26 11:26:30.320: W/Bundle(5670): at android.os.Bundle.getInt(Bundle.java:1000)
04-26 11:26:30.320: W/Bundle(5670): at android.content.Intent.getIntExtra(Intent.java:4620)
04-26 11:26:30.320: W/Bundle(5670): at com.example.flagsgame.Person.onCreate(Person.java:40)
04-26 11:26:30.320: W/Bundle(5670): at android.app.Activity.performCreate(Activity.java:5231)
04-26 11:26:30.320: W/Bundle(5670): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-26 11:26:30.320: W/Bundle(5670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-26 11:26:30.320: W/Bundle(5670): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
04-26 11:26:30.320: W/Bundle(5670): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-26 11:26:30.320: W/Bundle(5670): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
04-26 11:26:30.320: W/Bundle(5670): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 11:26:30.320: W/Bundle(5670): at android.os.Looper.loop(Looper.java:136)
04-26 11:26:30.320: W/Bundle(5670): at android.app.ActivityThread.main(ActivityThread.java:5146)
04-26 11:26:30.320: W/Bundle(5670): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 11:26:30.320: W/Bundle(5670): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 11:26:30.320: W/Bundle(5670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
04-26 11:26:30.320: W/Bundle(5670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
04-26 11:26:30.320: W/Bundle(5670): at dalvik.system.NativeStart.main(Native Method)
04-26 11:26:30.370: I/Timeline(5670): Timeline: Activity_idle id: android.os.BinderProxy#42b6b638 time:36387461
04-26 11:26:30.370: E/SpannableStringBuilder(5670): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 11:26:30.370: E/SpannableStringBuilder(5670): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 11:27:03.740: W/IInputConnectionWrapper(5670): getExtractedText on inactive InputConnection
04-26 11:27:03.740: W/IInputConnectionWrapper(5670): getTextBeforeCursor on inactive InputConnection
04-26 11:27:03.740: W/IInputConnectionWrapper(5670): getSelectedText on inactive InputConnection
04-26 11:27:03.740: W/IInputConnectionWrapper(5670): getTextAfterCursor on inactive InputConnection
as you can see the timer are still runnig after the error i got.
Error: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

Check in the text fields you enter that there are no blank spaces. The same thing happened to me and it was because sometimes I added words with spaces at the end.

Related

Passing Integer Values Between Activities - Android

Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));

ProgressBar inside of Fragment class causing NullPointerException at setMax method?

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.analogin_fragment,
container, false);
AITVVOLT0 = (TextView) rootView.findViewById(R.id.aintvvolt0);
AITVVOLT1 = (TextView) rootView.findViewById(R.id.aintvvolt1);
AITVVOLT2 = (TextView) rootView.findViewById(R.id.aintvvolt2);
AITVVOLT3 = (TextView) rootView.findViewById(R.id.aintvvolt3);
AITVVOLT4 = (TextView) rootView.findViewById(R.id.aintvvolt4);
AITVVOLT5 = (TextView) rootView.findViewById(R.id.aintvvolt5);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);
AIVOLTBAR0.setMax(100);
AIVOLTBAR1.setMax(100);
AIVOLTBAR2.setMax(100);
AIVOLTBAR3.setMax(100);
AIVOLTBAR4.setMax(100);
AIVOLTBAR5.setMax(100);
AIVOLTBAR0.setProgress(0);
AIVOLTBAR1.setProgress(0);
AIVOLTBAR2.setProgress(0);
AIVOLTBAR3.setProgress(0);
AIVOLTBAR4.setProgress(0);
AIVOLTBAR5.setProgress(0);
return rootView;
}
The AIVOLTBAR#.setMax(); methods are causing a NullPointerException - but the ProgressBars are not null (Tested with a log as well as commenting out the setMax causes the program to work).
Logcat:
08-20 16:23:18.409: E/AndroidRuntime(1332): Process: com.nanospark.androidapp, PID: 1332
08-20 16:23:18.409: E/AndroidRuntime(1332): java.lang.NullPointerException
08-20 16:23:18.409: E/AndroidRuntime(1332): at com.nanospark.androidapp.MainActivity$AnalogInputFragment.onCreateView(MainActivity.java:218)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.os.Handler.handleCallback(Handler.java:733)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.os.Handler.dispatchMessage(Handler.java:95)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.os.Looper.loop(Looper.java:136)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-20 16:23:18.409: E/AndroidRuntime(1332): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 16:23:18.409: E/AndroidRuntime(1332): at java.lang.reflect.Method.invoke(Method.java:515)
08-20 16:23:18.409: E/AndroidRuntime(1332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-20 16:23:18.409: E/AndroidRuntime(1332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-20 16:23:18.409: E/AndroidRuntime(1332): at dalvik.system.NativeStart.main(Native Method)
08-20 16:27:27.557: E/BluetoothAdapter(1387): Bluetooth binder is null
08-20 16:27:27.853: E/OpenGLRenderer(1387): Getting MAX_TEXTURE_SIZE from GradienCache
08-20 16:27:27.853: E/OpenGLRenderer(1387): MAX_TEXTURE_SIZE: 16384
08-20 16:27:27.865: E/OpenGLRenderer(1387): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
08-20 16:27:27.869: E/OpenGLRenderer(1387): MAX_TEXTURE_SIZE: 16384
08-20 16:27:28.409: E/AndroidRuntime(1387): FATAL EXCEPTION: main
08-20 16:27:28.409: E/AndroidRuntime(1387): Process: com.nanospark.androidapp, PID: 1387
08-20 16:27:28.409: E/AndroidRuntime(1387): java.lang.NullPointerException
08-20 16:27:28.409: E/AndroidRuntime(1387): at com.nanospark.androidapp.MainActivity$AnalogInputFragment.onCreateView(MainActivity.java:223)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.os.Handler.handleCallback(Handler.java:733)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.os.Handler.dispatchMessage(Handler.java:95)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.os.Looper.loop(Looper.java:136)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-20 16:27:28.409: E/AndroidRuntime(1387): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 16:27:28.409: E/AndroidRuntime(1387): at java.lang.reflect.Method.invoke(Method.java:515)
08-20 16:27:28.409: E/AndroidRuntime(1387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-20 16:27:28.409: E/AndroidRuntime(1387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-20 16:27:28.409: E/AndroidRuntime(1387): at dalvik.system.NativeStart.main(Native Method)
The problem is that you have some typos. You're setting AIVOLTBAR0 multiple times instead of setting each progress bar individually:
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);
Instead it needs to be this:
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
AIVOLTBAR1 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
AIVOLTBAR2 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
AIVOLTBAR3 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
AIVOLTBAR4 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
AIVOLTBAR5 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);

My activity is crashing after adding a listener

I have this code in my MainActivity class:
public class MainActivity extends ActionBarActivity {
Button mButton;
EditText mEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.btnAsk);
mEdit = (EditText)findViewById(R.id.txtQuestion);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Toast.makeText(getApplicationContext(),
mEdit.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
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;
}
}
}
Everything was just fine, but after adding setOnClickListener() to mButton it started crashing at the very beginning.
Here is the error log:
04-26 14:12:26.255: E/AndroidRuntime(1447): FATAL EXCEPTION: main
04-26 14:12:26.255: E/AndroidRuntime(1447): Process: com.gelasoft.answeringball, PID: 1447
04-26 14:12:26.255: E/AndroidRuntime(1447): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gelasoft.answeringball/com.gelasoft.answeringball.MainActivity}: java.lang.NullPointerException
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.os.Looper.loop(Looper.java:136)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-26 14:12:26.255: E/AndroidRuntime(1447): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 14:12:26.255: E/AndroidRuntime(1447): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 14:12:26.255: E/AndroidRuntime(1447): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-26 14:12:26.255: E/AndroidRuntime(1447): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-26 14:12:26.255: E/AndroidRuntime(1447): at dalvik.system.NativeStart.main(Native Method)
04-26 14:12:26.255: E/AndroidRuntime(1447): Caused by: java.lang.NullPointerException
04-26 14:12:26.255: E/AndroidRuntime(1447): at com.gelasoft.answeringball.MainActivity.onCreate(MainActivity.java:30)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.app.Activity.performCreate(Activity.java:5231)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-26 14:12:26.255: E/AndroidRuntime(1447): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-26 14:12:26.255: E/AndroidRuntime(1447): ... 11 more
What am I missing here? I know that it is something really small, but I can't spot it right now.
P.S Here is my fragment xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.xx.ss.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/sphereIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/magicBallDescr"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/sphereIcon"
android:orientation="vertical" >
<TextView
android:id="#+id/txtAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/txtQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/textHint"
android:inputType="textMultiLine"
android:lines="8"
/>
<Button
android:id="#+id/btnAsk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btnAskQ"
android:paddingEnd="#dimen/activity_vertical_margin"
/>
</LinearLayout>
</RelativeLayout>
Replace
mButton = (Button)findViewById(R.id.btnAsk);
with
mButton = (Button)rootView.findViewById(R.id.btnAsk); // this will go in Fragment onCreateView
And move setOnClicklistener code as well to onCreateView.
Hope it helps.
Move this code :
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Toast.makeText(getApplicationContext(),
mEdit.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
into your fragment's onCreateView() method. It should work then.

Illegal state exception onclick

I am trying to retrieve data by clicking on a button in my activity. I have declared an onclick in activity_second.Here is my code.I am getting am IllegalstateException: method cannot be executed in activity.what is wrong with my code.Why the logcat is showing InvocationTargetException.what exactly this error is and why NullPointerException is there??
public class SecondActivity extends Activity {
private EditText loadinfo;
private Button butn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
loadinfo = (EditText)findViewById(R.id.editText1);
butn1 = (Button)findViewById(R.id.button1);
}
public void apurv1(View view){
File folder = getCacheDir();
File myfile = new File(folder, "mydata1.txt");
String data = readdata(myfile);
if(data!=null){
loadinfo.setText(data);
}
else {
loadinfo.setText("no entry was done");
}
}
private String readdata(File myfile){
FileInputStream fis= null;
try {
fis = new FileInputStream(myfile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int read = -1;
StringBuffer stringbuffer = new StringBuffer();
try {
while((read = fis.read())!=-1){
stringbuffer.append((char)read);
}
return stringbuffer.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
StackTrace is
04-26 00:59:00.545: E/AndroidRuntime(2023): FATAL EXCEPTION: main
04-26 00:59:00.545: E/AndroidRuntime(2023): java.lang.IllegalStateException: Could not execute method of the activity
04-26 00:59:00.545: E/AndroidRuntime(2023): at android.view.View$1.onClick(View.java:3633)
04-26 00:59:00.545: E/AndroidRuntime(2023): at android.view.View.performClick(View.java:4240)
04-26 00:59:00.545: E/AndroidRuntime(2023): at android.view.View$PerformClick.run(View.java:17721)
04-26 00:59:00.545: E/AndroidRuntime(2023): at android.os.Handler.handleCallback(Handler.java:730)
04-26 00:59:00.545: E/AndroidRuntime(2023): at android.os.Handler.dispatchMessage(Handler.java:92)
04-26 00:59:00.545: E/AndroidRuntime(2023): at android.os.Looper.loop(Looper.java:137)
04-26 00:59:00.545: E/AndroidRuntime(2023): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-26 00:59:00.545: E/AndroidRuntime(2023): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 00:59:00.545: E/AndroidRuntime(2023): at java.lang.reflect.Method.invoke(Method.java:525)
04-26 00:59:00.545: E/AndroidRuntime(2023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-26 00:59:00.545: E/AndroidRuntime(2023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-26 00:59:00.545: E/AndroidRuntime(2023): at dalvik.system.NativeStart.main(Native Method)
04-26 00:59:00.545: E/AndroidRuntime(2023): Caused by: java.lang.reflect.InvocationTargetException
04-26 00:59:00.545: E/AndroidRuntime(2023): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 00:59:00.545: E/AndroidRuntime(2023): at java.lang.reflect.Method.invoke(Method.java:525)
04-26 00:59:00.545: E/AndroidRuntime(2023): at android.view.View$1.onClick(View.java:3628)
04-26 00:59:00.545: E/AndroidRuntime(2023): ... 11 more
04-26 00:59:00.545: E/AndroidRuntime(2023): Caused by: java.lang.NullPointerException
04-26 00:59:00.545: E/AndroidRuntime(2023): at com.example.storageexternal.SecondActivity.readdata(SecondActivity.java:62)
04-26 00:59:00.545: E/AndroidRuntime(2023): at com.example.storageexternal.SecondActivity.apurv1(SecondActivity.java:36)
04-26 00:59:00.545: E/AndroidRuntime(2023): ... 14 more
Assuming activity_second is your xml file and you have an attribute android:onClick="readdate" for your button, your readdate method should return void and have an argument of View, such as
public void readdate (View v) {
//add code
}
or via an OnClickListener
Check this post for more details How exactly does the android:onClick XML attribute differ from setOnClickListener?

variables in classes outside activities

Am I doing this right? I'm trying to implement a simple countdown timer just to get my feet of the ground with it.
my timer class is not nested in my activity, and all by itself in it's own java file, for the sake of neatness. here:
public class McatTimer extends CountDownTimer {
private TextView timer_tv;
public McatTimer(long millisInFuture, long countDownInterval, TextView textview) {
super(millisInFuture, countDownInterval);
this.timer_tv = textview;
}
#Override
public void onFinish() {
timer_tv.setText("DONE!");
}
#Override
public void onTick(long millisUntilFinished) {
timer_tv.setText("seconds remaining: " + millisUntilFinished / 1000);
}
}
and here is logcat:
10-03 08:40:36.007: E/AndroidRuntime(2582): FATAL EXCEPTION: main
10-03 08:40:36.007: E/AndroidRuntime(2582): java.lang.NullPointerException
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.mangodeveloper.mcathomie.McatTimer.onTick(McatTimer.java:22)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.Looper.loop(Looper.java:123)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 08:40:36.007: E/AndroidRuntime(2582): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 08:40:36.007: E/AndroidRuntime(2582): at java.lang.reflect.Method.invoke(Method.java:507)
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 08:40:36.007: E/AndroidRuntime(2582): at dalvik.system.NativeStart.main(Native Method)
and my activity has these in their appropriate places:
private McatTimer mcatTimer;
....
mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
mcatTimer.start();
Also is there some trick to reading these logcats? I'm sorry but i'm self-teaching.
Looks like your TextView object is null. Please ensure that you have intialized your TextView before you call the
mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
Call this,
timerTv=(TextView)findViewById(R.id.textview);

Categories