Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Android Programming - Weird Problem

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Android Programming - Weird Problem

    I'm making an android program that will generate problems that u can solve. The issue is whenever I try to convert a CharSequence to an Integer, it throws an error. Heres my code:

    package com.android.jackson;
     
    import java.util.Random;
     
    import android.os.Bundle;
    import android.app.Activity;
    import android.text.Editable;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
     
    public class MainActivity extends Activity {
    	//Random
    	Random rand = new Random();
     
    	//Views
    	Button generate, btnAnswer;
    	TextView showingNOP, problem;
    	EditText nop, answer;
     
    	//Integers
    	int realAnswer,yourAnswer, one, two, right, wrong, aop;
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
     
    		//Initalizing IDS
    		generate = (Button) findViewById(R.id.generate);
    		btnAnswer = (Button) findViewById(R.id.bAnswer);
    		showingNOP = (TextView) findViewById(R.id.nop);
    		problem = (TextView) findViewById(R.id.problem);
    		nop = (EditText) findViewById(R.id.amount);
    		answer = (EditText) findViewById(R.id.answer);
     
    		generate.setOnClickListener(new View.OnClickListener() {
     
    			@Override
    			public void onClick(View v) {
     
    				aop = Integer.valueOf(nop.getText().toString());
    				generate.setEnabled(false);
    				nop.setEnabled(false);
    				generateProblem();
    			}
    		});
    	}
     
    	public void generateProblem(){
    		for(int i = 0; i < aop; i++){
     
    			btnAnswer.setOnClickListener(new View.OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//making random numbers
    					one = rand.nextInt((50 - 1) + 1) + 1;
    					two = rand.nextInt((50 - 1) + 1) + 1;
    					//getting answer
    					realAnswer = one + two;
    					yourAnswer = Integer.valueOf(answer.getText().toString());
    					//testing answer
    					if(realAnswer == yourAnswer){
    						right++;
    					}else{
    						wrong++;
    					}
    					//showing number of problems
    					showingNOP.setText("Number: " + aop);
     
    				}
    			});
    		}
    	}
    }

    Here's the error log by LogCat (I highlighted what I though were the main problems):


    02-08 15:31:16.278: D/dalvikvm(1706): Not late-enabling CheckJNI (already on)
    02-08 15:31:22.018: D/gralloc_goldfish(1706): Emulator without GPU emulation detected.
    02-08 15:31:22.198: W/IInputConnectionWrapper(1706): showStatusIcon on inactive InputConnection
    02-08 17:41:20.278: D/dalvikvm(1977): GC_FOR_ALLOC freed 87K, 5% free 3299K/3452K, paused 47ms, total 51ms
    02-08 17:41:20.348: D/gralloc_goldfish(1977): Emulator without GPU emulation detected.
    02-08 17:46:57.108: I/Choreographer(1977): Skipped 46 frames! The application may be doing too much work on its main thread.
    02-08 17:47:01.788: I/Choreographer(1977): Skipped 48 frames! The application may be doing too much work on its main thread.
    02-08 17:58:22.608: D/dalvikvm(2028): GC_FOR_ALLOC freed 90K, 5% free 3295K/3452K, paused 92ms, total 96ms
    02-08 17:58:22.678: D/gralloc_goldfish(2028): Emulator without GPU emulation detected.
    02-08 17:58:57.148: W/IInputConnectionWrapper(2028): showStatusIcon on inactive InputConnection
    02-08 18:04:15.648: D/dalvikvm(2083): GC_FOR_ALLOC freed 94K, 5% free 3291K/3452K, paused 75ms, total 75ms
    02-08 18:04:15.708: D/gralloc_goldfish(2083): Emulator without GPU emulation detected.
    02-08 18:04:25.448: D/AndroidRuntime(2083): Shutting down VM
    02-08 18:04:25.448: W/dalvikvm(2083): threadid=1: thread exiting with uncaught exception (group=0xb1a7bba8)
    02-08 18:04:25.488: E/AndroidRuntime(2083): FATAL EXCEPTION: main
    02-08 18:04:25.488: E/AndroidRuntime(2083): Process: com.android.jackson, PID: 2083
    02-08 18:04:25.488: E/AndroidRuntime(2083): java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.lang.String
    02-08 18:04:25.488: E/AndroidRuntime(2083): at com.android.jackson.MainActivity$1.onClick(MainAct ivity.java:41)
    02-08 18:04:25.488: E/AndroidRuntime(2083): at android.view.View.performClick(View.java:4438)
    02-08 18:04:25.488: E/AndroidRuntime(2083): at android.view.View$PerformClick.run(View.java:18422 )
    02-08 18:04:25.488: E/AndroidRuntime(2083): at android.os.Handler.handleCallback(Handler.java:733 )
    02-08 18:04:25.488: E/AndroidRuntime(2083): at android.os.Handler.dispatchMessage(Handler.java:95 )
    02-08 18:04:25.488: E/AndroidRuntime(2083): at android.os.Looper.loop(Looper.java:136)
    02-08 18:04:25.488: E/AndroidRuntime(2083): at android.app.ActivityThread.main(ActivityThread.jav a:5017)
    02-08 18:04:25.488: E/AndroidRuntime(2083): at java.lang.reflect.Method.invokeNative(Native Method)
    02-08 18:04:25.488: E/AndroidRuntime(2083): at java.lang.reflect.Method.invoke(Method.java:515)
    02-08 18:04:25.488: E/AndroidRuntime(2083): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
    02-08 18:04:25.488: E/AndroidRuntime(2083): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
    02-08 18:04:25.488: E/AndroidRuntime(2083): at dalvik.system.NativeStart.main(Native Method)
    02-08 18:04:33.758: I/Process(2083): Sending signal. PID: 2083 SIG: 9
    02-08 18:09:02.868: D/dalvikvm(2138): GC_FOR_ALLOC freed 97K, 5% free 3288K/3452K, paused 139ms, total 145ms
    02-08 18:09:03.208: D/gralloc_goldfish(2138): Emulator without GPU emulation detected.
    02-08 18:09:14.108: D/AndroidRuntime(2138): Shutting down VM
    02-08 18:09:14.108: W/dalvikvm(2138): threadid=1: thread exiting with uncaught exception (group=0xb1a7bba8)
    02-08 18:09:14.138: E/AndroidRuntime(2138): FATAL EXCEPTION: main
    02-08 18:09:14.138: E/AndroidRuntime(2138): Process: com.android.jackson, PID: 2138
    02-08 18:09:14.138: E/AndroidRuntime(2138): java.lang.NumberFormatException: Invalid int: ""
    02-08 18:09:14.138: E/AndroidRuntime(2138): at java.lang.Integer.invalidInt(Integer.java:137)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at java.lang.Integer.parseInt(Integer.java:358)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at java.lang.Integer.parseInt(Integer.java:331)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at
    com.android.jackson.MainActivity$2.onClick(MainAct ivity.java:61)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at android.view.View.performClick(View.java:4438)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at android.view.View$PerformClick.run(View.java:18422 )
    02-08 18:09:14.138: E/AndroidRuntime(2138): at android.os.Handler.handleCallback(Handler.java:733 )
    02-08 18:09:14.138: E/AndroidRuntime(2138): at android.os.Handler.dispatchMessage(Handler.java:95 )
    02-08 18:09:14.138: E/AndroidRuntime(2138): at android.os.Looper.loop(Looper.java:136)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at android.app.ActivityThread.main(ActivityThread.jav a:5017)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at java.lang.reflect.Method.invokeNative(Native Method)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at java.lang.reflect.Method.invoke(Method.java:515)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
    02-08 18:09:14.138: E/AndroidRuntime(2138): at dalvik.system.NativeStart.main(Native Method)
    02-08 18:12:54.268: D/dalvikvm(2200): GC_FOR_ALLOC freed 101K, 5% free 3285K/3452K, paused 163ms, total 167ms
    02-08 18:12:54.508: D/gralloc_goldfish(2200): Emulator without GPU emulation detected.
    02-08 18:16:12.188: D/dalvikvm(2259): GC_FOR_ALLOC freed 104K, 5% free 3282K/3452K, paused 50ms, total 54ms
    02-08 18:16:12.428: D/gralloc_goldfish(2259): Emulator without GPU emulation detected.
    02-08 18:16:21.368: D/AndroidRuntime(2259): Shutting down VM
    02-08 18:16:21.378: W/dalvikvm(2259): threadid=1: thread exiting with uncaught exception (group=0xb1a7bba8)
    02-08 18:16:21.378: E/AndroidRuntime(2259): FATAL EXCEPTION: main
    02-08 18:16:21.378: E/AndroidRuntime(2259): Process: com.android.jackson, PID: 2259
    02-08 18:16:21.378: E/AndroidRuntime(2259): java.lang.NumberFormatException: Invalid int: ""
    02-08 18:16:21.378: E/AndroidRuntime(2259): at java.lang.Integer.invalidInt(Integer.java:137)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at java.lang.Integer.parseInt(Integer.java:358)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at java.lang.Integer.parseInt(Integer.java:331)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at java.lang.Integer.valueOf(Integer.java:489)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at
    com.android.jackson.MainActivity$2.onClick(MainAct ivity.java:59)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at android.view.View.performClick(View.java:4438)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at android.view.View$PerformClick.run(View.java:18422 )
    02-08 18:16:21.378: E/AndroidRuntime(2259): at android.os.Handler.handleCallback(Handler.java:733 )
    02-08 18:16:21.378: E/AndroidRuntime(2259): at android.os.Handler.dispatchMessage(Handler.java:95 )
    02-08 18:16:21.378: E/AndroidRuntime(2259): at android.os.Looper.loop(Looper.java:136)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at android.app.ActivityThread.main(ActivityThread.jav a:5017)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at java.lang.reflect.Method.invokeNative(Native Method)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at java.lang.reflect.Method.invoke(Method.java:515)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
    02-08 18:16:21.378: E/AndroidRuntime(2259): at dalvik.system.NativeStart.main(Native Method)
    02-08 18:16:27.948: I/Process(2259): Sending signal. PID: 2259 SIG: 9
    02-08 18:18:39.978: D/dalvikvm(2317): GC_FOR_ALLOC freed 107K, 6% free 3278K/3452K, paused 48ms, total 55ms
    02-08 18:18:40.378: D/gralloc_goldfish(2317): Emulator without GPU emulation detected.
    02-08 18:18:48.708: D/AndroidRuntime(2317): Shutting down VM
    02-08 18:18:48.708: W/dalvikvm(2317): threadid=1: thread exiting with uncaught exception (group=0xb1a7bba8)
    02-08 18:18:48.798: E/AndroidRuntime(2317): FATAL EXCEPTION: main
    02-08 18:18:48.798: E/AndroidRuntime(2317): Process: com.android.jackson, PID: 2317
    02-08 18:18:48.798: E/AndroidRuntime(2317): java.lang.NumberFormatException: Invalid int: ""
    02-08 18:18:48.798: E/AndroidRuntime(2317): at java.lang.Integer.invalidInt(Integer.java:137)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at java.lang.Integer.parseInt(Integer.java:358)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at java.lang.Integer.parseInt(Integer.java:331)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at java.lang.Integer.valueOf(Integer.java:489)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at
    com.android.jackson.MainActivity$2.onClick(MainAct ivity.java:59)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at android.view.View.performClick(View.java:4438)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at android.view.View$PerformClick.run(View.java:18422 )
    02-08 18:18:48.798: E/AndroidRuntime(2317): at android.os.Handler.handleCallback(Handler.java:733 )
    02-08 18:18:48.798: E/AndroidRuntime(2317): at android.os.Handler.dispatchMessage(Handler.java:95 )
    02-08 18:18:48.798: E/AndroidRuntime(2317): at android.os.Looper.loop(Looper.java:136)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at android.app.ActivityThread.main(ActivityThread.jav a:5017)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at java.lang.reflect.Method.invokeNative(Native Method)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at java.lang.reflect.Method.invoke(Method.java:515)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
    02-08 18:18:48.798: E/AndroidRuntime(2317): at dalvik.system.NativeStart.main(Native Method)
    02-08 18:18:53.568: I/Process(2317): Sending signal. PID: 2317 SIG: 9
    02-08 18:22:19.858: D/dalvikvm(2379): GC_FOR_ALLOC freed 110K, 6% free 3275K/3452K, paused 47ms, total 50ms
    02-08 18:22:20.458: D/gralloc_goldfish(2379): Emulator without GPU emulation detected.
    02-08 18:22:27.288: D/AndroidRuntime(2379): Shutting down VM
    02-08 18:22:27.288: W/dalvikvm(2379): threadid=1: thread exiting with uncaught exception (group=0xb1a7bba8)
    02-08 18:22:27.298: E/AndroidRuntime(2379): FATAL EXCEPTION: main
    02-08 18:22:27.298: E/AndroidRuntime(2379): Process: com.android.jackson, PID: 2379
    02-08 18:22:27.298: E/AndroidRuntime(2379): java.lang.NumberFormatException: Invalid int: ""
    02-08 18:22:27.298: E/AndroidRuntime(2379): at java.lang.Integer.invalidInt(Integer.java:137)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at java.lang.Integer.parseInt(Integer.java:358)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at java.lang.Integer.parseInt(Integer.java:331)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at java.lang.Integer.valueOf(Integer.java:489)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at
    com.android.jackson.MainActivity$2.onClick(MainAct ivity.java:59)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at android.view.View.performClick(View.java:4438)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at android.view.View$PerformClick.run(View.java:18422 )
    02-08 18:22:27.298: E/AndroidRuntime(2379): at android.os.Handler.handleCallback(Handler.java:733 )
    02-08 18:22:27.298: E/AndroidRuntime(2379): at android.os.Handler.dispatchMessage(Handler.java:95 )
    02-08 18:22:27.298: E/AndroidRuntime(2379): at android.os.Looper.loop(Looper.java:136)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at android.app.ActivityThread.main(ActivityThread.jav a:5017)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at java.lang.reflect.Method.invokeNative(Native Method)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at java.lang.reflect.Method.invoke(Method.java:515)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
    02-08 18:22:27.298: E/AndroidRuntime(2379): at dalvik.system.NativeStart.main(Native Method)
    02-08 18:22:29.968: I/Process(2379): Sending signal. PID: 2379 SIG: 9


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Android Programming - Weird Problem

    The errors seem to be self explanatory. Which one(s) don't you understand?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Android Programming - Weird Problem

    All of them, I think im using the right methods, but its throwing an error. Can u give me the reasons why its catching an error?! Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Android Programming - Weird Problem

    java.lang.NumberFormatException: Invalid int: ""
    An empty String is not a valid int value.

    android.text.SpannableStringBuilder cannot be cast to java.lang.String
    android.text.SpannableStringBuilder cannot be cast to java.lang.String
    I don't see any casting in the posted code. Where is line 59?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 05:48 PM
  2. Weird problem with my linkedlist
    By clydefrog in forum Collections and Generics
    Replies: 19
    Last Post: February 22nd, 2012, 06:47 PM
  3. I'm having a weird problem with my browsers
    By javapenguin in forum Computer Support
    Replies: 11
    Last Post: August 18th, 2011, 03:41 AM
  4. Jsp weird problem
    By johniem in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: February 5th, 2010, 06:46 AM