-3

i am new to android,and the following code was designed to return a string called "err" which contains all errors in the input.unfortunately,the code does not work,and returns an empty string.

    package com.example.app;


    //import java.util.Calendar;

    import android.app.Activity;
import android.os.Bundle;

    import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
    //import android.widget.DatePicker;

import android.widget.TextView;


    public class Second extends Activity implements OnClickListener {
        CheckBox Facebook_chk;
        EditText Facebook_name;
        EditText Name;
        EditText Id;
        EditText Txterr;
        EditText Pass;
        Button v;


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.second);
            v = (Button) findViewById(R.id.Btn1);
            v.setOnClickListener( this);
            Facebook_chk = (CheckBox)findViewById(R.id.Cfbook);//Represents the facebook checkbox.
            Facebook_name = (EditText)findViewById(R.id.Face);//represents the facebook text.
            Name = (EditText)findViewById(R.id.Name);//represents the Name text.
            Id = (EditText)findViewById(R.id.Id);//represents the Id text.
            Txterr = (EditText)findViewById(R.id.Txterr);//represents the Id text.
            Pass = (EditText)findViewById(R.id.Pass);//represents the Pass text.



        //     final DatePicker Date = (DatePicker)findViewById(R.id.Datepick);//represents the Pass text.


            Facebook_chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    // TODO Auto-generated method stub
                    if(Facebook_chk.isChecked())
                        Facebook_name.setEnabled(true);
                    else
                        Facebook_name.setEnabled(false);
                                    ;
                            }
                            });

            }


            public String GetErr(){

                String error="a";
                if(Facebook_name.toString().equals("")&& Facebook_chk.isChecked())//check with title if not available.
                {
                error+="facebook account not entered/n";//also check if not available
                }
                if((Name.getText().toString().equals("")))
                {
                    error+="Name not entered\n";


                }
                if(Id.toString().matches("[a-zA-Z]+") || Id.getText().toString().equals(""))
                    error+="Id entered is invalid\n";
                if(Pass.toString().length()<5 || Pass.getText().toString().equals(""))
                    error+="Passwords must contain 5 or more digits\n";
            //  int day= Date.getDayOfMonth();
            //  int month = Date.getMonth();
            //  int year=Date.getYear();
                //Calendar enter = Calendar.getInstance();
            //  Calendar today = Calendar.getInstance();
            //  enter.set(year,month,day);
            //  today.set(Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH);
                //if((enter.getTime().before(today.getTime())))
                //  error+="Date entered either passed or not available.";

                return error;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            {//Returns strings,if not empty w print it and stop.

                if(v.getId()==R.id.Btn1)
                {
                    if(v.getId() == R.id.Btn1){
                    String err = GetErr();//THE STRING OF ERROR RETRIEVED.
                    if(err!="a")
                        Txterr.setText(err);
                    else
                        Txterr.setText("all is well");
                    }

                }


            }
        }

    }





            // TODO Auto-generated method stub

i will be more than glad to receive further instructions on the possible solution for the matter.

the error:

11-09 03:04:21.331: D/dalvikvm(825): GC_FOR_ALLOC freed 39K, 9% free 2885K/3140K, paused 145ms, total 156ms
11-09 03:04:23.240: D/gralloc_goldfish(825): Emulator without GPU emulation detected.
11-09 03:11:33.671: D/dalvikvm(825): GC_FOR_ALLOC freed 164K, 11% free 3222K/3596K, paused 75ms, total 94ms
11-09 03:11:33.873: D/AndroidRuntime(825): Shutting down VM
11-09 03:11:33.880: W/dalvikvm(825): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-09 03:11:33.950: E/AndroidRuntime(825): FATAL EXCEPTION: main
11-09 03:11:33.950: E/AndroidRuntime(825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Second}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.os.Looper.loop(Looper.java:137)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-09 03:11:33.950: E/AndroidRuntime(825):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 03:11:33.950: E/AndroidRuntime(825):  at java.lang.reflect.Method.invoke(Method.java:525)
11-09 03:11:33.950: E/AndroidRuntime(825):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-09 03:11:33.950: E/AndroidRuntime(825):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 03:11:33.950: E/AndroidRuntime(825):  at dalvik.system.NativeStart.main(Native Method)
11-09 03:11:33.950: E/AndroidRuntime(825): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:11:33.950: E/AndroidRuntime(825):  at com.example.app.Second.onCreate(Second.java:41)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.app.Activity.performCreate(Activity.java:5133)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-09 03:11:33.950: E/AndroidRuntime(825):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-09 03:11:33.950: E/AndroidRuntime(825):  ... 11 more
11-09 03:11:37.812: I/Process(825): Sending signal. PID: 825 SIG: 9
11-09 03:11:51.013: D/dalvikvm(868): GC_FOR_ALLOC freed 42K, 9% free 2885K/3144K, paused 48ms, total 51ms
11-09 03:11:51.410: D/gralloc_goldfish(868): Emulator without GPU emulation detected.
11-09 03:11:55.360: D/dalvikvm(868): GC_FOR_ALLOC freed 164K, 11% free 3221K/3596K, paused 60ms, total 65ms
11-09 03:11:55.600: D/AndroidRuntime(868): Shutting down VM
11-09 03:11:55.600: W/dalvikvm(868): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-09 03:11:55.660: E/AndroidRuntime(868): FATAL EXCEPTION: main
11-09 03:11:55.660: E/AndroidRuntime(868): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Second}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.os.Looper.loop(Looper.java:137)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-09 03:11:55.660: E/AndroidRuntime(868):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 03:11:55.660: E/AndroidRuntime(868):  at java.lang.reflect.Method.invoke(Method.java:525)
11-09 03:11:55.660: E/AndroidRuntime(868):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-09 03:11:55.660: E/AndroidRuntime(868):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 03:11:55.660: E/AndroidRuntime(868):  at dalvik.system.NativeStart.main(Native Method)
11-09 03:11:55.660: E/AndroidRuntime(868): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:11:55.660: E/AndroidRuntime(868):  at com.example.app.Second.onCreate(Second.java:41)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.app.Activity.performCreate(Activity.java:5133)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-09 03:11:55.660: E/AndroidRuntime(868):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-09 03:11:55.660: E/AndroidRuntime(868):  ... 11 more
11-09 03:11:58.330: I/Process(868): Sending signal. PID: 868 SIG: 9
11-09 03:17:20.080: D/dalvikvm(948): GC_FOR_ALLOC freed 46K, 9% free 2885K/3148K, paused 58ms, total 65ms
11-09 03:17:20.680: D/gralloc_goldfish(948): Emulator without GPU emulation detected.
11-09 03:24:25.141: D/dalvikvm(948): GC_FOR_ALLOC freed 164K, 11% free 3221K/3596K, paused 85ms, total 99ms
11-09 03:24:25.340: D/AndroidRuntime(948): Shutting down VM
11-09 03:24:25.340: W/dalvikvm(948): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-09 03:24:25.421: E/AndroidRuntime(948): FATAL EXCEPTION: main
11-09 03:24:25.421: E/AndroidRuntime(948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Second}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.os.Looper.loop(Looper.java:137)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-09 03:24:25.421: E/AndroidRuntime(948):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 03:24:25.421: E/AndroidRuntime(948):  at java.lang.reflect.Method.invoke(Method.java:525)
11-09 03:24:25.421: E/AndroidRuntime(948):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-09 03:24:25.421: E/AndroidRuntime(948):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 03:24:25.421: E/AndroidRuntime(948):  at dalvik.system.NativeStart.main(Native Method)
11-09 03:24:25.421: E/AndroidRuntime(948): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-09 03:24:25.421: E/AndroidRuntime(948):  at com.example.app.Second.onCreate(Second.java:41)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.app.Activity.performCreate(Activity.java:5133)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-09 03:24:25.421: E/AndroidRuntime(948):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-09 03:24:25.421: E/AndroidRuntime(948):  ... 11 more
11-09 03:24:28.120: I/Process(948): Sending signal. PID: 948 SIG: 9
doydoy
  • 4,021
  • 3
  • 20
  • 33
user44088
  • 7
  • 3

1 Answers1

1

You need to initialize your variable 'error' to a character. Try using placeholder text e.g. "Error ="

If that is all that is returned you know that none of the if statements are true.

Edit:

You are incorrectly declaring your EditText as a TextView. The compiler does not pick up on this as it is cast as a TextView, something that is perfectly possible.

The reason you are receiving seemingly random text from the TextView is because the TextView method 'getText()' returns a CharSequence.

Edit 2:

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText 11-09 03:11:55.660: E/AndroidRuntime(868): at com.example.app.Second.onCreate(Second.java:41)

Line 41 is Txterr = (EditText)findViewById(R.id.Txterr);//represents the Id text.

So now you have a TextView cast as an EditText. Evaluate which this is meant to be and make the correct change.

doydoy
  • 4,021
  • 3
  • 20
  • 33
  • The issue is that some are true:i entered empty id for example,yet the string is empty(printed for debugging now).i did change error to value. – user44088 Oct 30 '13 at 16:25
  • so we have resolved your first issue. The next would be identifying why your if statement is not equated correctly. Did you enter an empty if or a space character. To test the statement easily set the if statement to evaluate against "user44088" and then enter that into the id box. That can easily highlight the success of your if statements. – doydoy Oct 30 '13 at 16:33
  • it appears that the empty textfield is not empty;it contains the following: android.widget.EditText{3179f87 .....}.i check the content of the string using the .gettext((Name.getText().equals(""))).the following line is somehow added to the Name.gettext().any thoughts?" – user44088 Oct 30 '13 at 16:59
  • anyone able to sort this? – user44088 Oct 30 '13 at 18:00
  • so when you use editText.getText() on an empty edit text what do you get? – doydoy Oct 31 '13 at 08:34
  • i get android.widget.EditText{3179f87 .....} – user44088 Nov 04 '13 at 20:38
  • Where are you declaring your variables? I imagine you are doing so in the onCreate method? you need to declare them globally, initialize them in on create and then you can use them correctly. – doydoy Nov 05 '13 at 08:42
  • I did declare the variables globally.any thought – user44088 Nov 06 '13 at 13:15
  • and editText.getText().toString(); What is the result then? – doydoy Nov 06 '13 at 14:16
  • no matter if the field is empty or not,it returns the message claiming it is empty(in my code it is the name field).the other fields do not function.i edited my question to the latest code,i will be glad if you could check as i have been strolling around this question for over 3 days. – user44088 Nov 06 '13 at 15:33
  • So now we might be getting somewhere. I am guessing that from what your code is trying to achieve you are using an Edit Text widget? I cannot see reference to an EditText anywhere in your code. You will need to change the declaration and initialization to reflect this. Check my edited answer. – doydoy Nov 07 '13 at 08:21
  • i edited the question and code according to your recommendation,but now the app stops working once launched. – user44088 Nov 09 '13 at 08:12
  • What is the logcat output for this crash? – doydoy Nov 11 '13 at 08:47
  • i added the logcat output to my question. – user44088 Nov 11 '13 at 11:31
  • I have now edited my answer for this. – doydoy Nov 11 '13 at 11:39
  • okay,thanks.2 additional questions:i tried checking if a checkbox is checked and an edittext is empty:Facebook_name.toString().length()<1 && Facebook_chk.isChecked().the following code does not function(aka:once the checkbox is checked,it can be empty and it wouldnt matter).additionally,checking an edittext for letters does not work as well(Id.toString().contains("[a-zA-Z]+") does not work).any idea what is wrong? – user44088 Nov 11 '13 at 13:33
  • If your original question has been answered, please accept my answer :). I'd also suggest logging a new question for the new stuff, it will save people having to crawl through all our updates etc. – doydoy Nov 11 '13 at 13:53
  • accepted.any way you can answer this though? – user44088 Nov 11 '13 at 14:02
  • Open a new question with the relevant code provided. – doydoy Nov 11 '13 at 14:45