-2

While trying to run my app, I noticed that a few errors claiming that many variables can not be resolved, even though declared in the code i changed it to the following code, but once I enter the app, it collapses:

public String GetErr(){

        String error="";
        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.toString().equals(""))
            error+="Name not entered/n";
        if(Id.toString().contains("[a-zA-Z]+") || Id.toString().equals(""))
            error+="Id entered is invalid/n";
        if(Pass.toString().length()<5 || Pass.toString().equals(""))
            error+="Passwords must contain 5 or more digits";
    //  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;
}

EDIT: Now the geterr() returns an empty string at all times.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user44088
  • 7
  • 3

2 Answers2

0

You are declaring variables in the onCreate() method, so that is their scope. You can not use them outside this function. So when you use them in the GetErr() method, you get an error. You can solve this by moving the variables you need in multiple methods to global variables (so declare them in the class instead of in the method.

Edit

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.DatePicker;

import android.widget.TextView;

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


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


        Btn1.setOnClickListener(this);
        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="";
        if(Facebook_name==null && Facebook_chk.isChecked())//check with title if not available.
        {
            error+="facebook account not entered/n";//also check if not available
        }
        if(Name==null)
            error+="Name not entered/n";
        if(Id.toString().contains("[a-zA-Z]+") || Id==null)
            error+="Id entered is invalid/n";
        if(Pass.toString().length()<5)
            error+="Passwords must contain 5 or more digits";
        //  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) {
        if(v == Btn1){
            String err = GetErr();
            if(err != ""){
                Txterr.setText(err);
            }
        }
    }

}
Jeffrey Klardie
  • 3,020
  • 1
  • 18
  • 23
  • I declared the vars outside,but now the app collapses once launched.i edited the question with my recent code.any solution? – user44088 Oct 30 '13 at 12:43
  • You should paste the log of the crash so we can see what goes wrong. – Jeffrey Klardie Oct 30 '13 at 12:49
  • the emulator shows "unforutnately,app_name has stopped",once i move to the 2nd intent(2nd window button) – user44088 Oct 30 '13 at 12:53
  • Again, please paste the crash that causes the app to stop. You can find it in the log (You can either use `adb logcat` from commandline, or open the logcat view in Eclipse). – Jeffrey Klardie Oct 30 '13 at 12:55
  • it exceeds the character limit(260k characters).any other solution? – user44088 Oct 30 '13 at 13:08
  • I'm pretty sure that is not the exact error message. This way no one can help. We just need the EXACT error message with the stacktrace. – Jeffrey Klardie Oct 30 '13 at 13:11
  • solved by moving the variables.i updated the code,as at the moment the button does not work.any thoughts? – user44088 Oct 30 '13 at 13:35
  • Updated my answer, that should do the trick (Made global var for button, fixed onClick() method). – Jeffrey Klardie Oct 30 '13 at 13:41
  • hi,i did change it according to the edition,but the button still does not operate.i checked it using an else condition on the end that prints a different text,yet it does not appear to function at all. – user44088 Oct 30 '13 at 14:00
  • any idea why this might be? – user44088 Oct 30 '13 at 14:50
  • Did you check if the GetErr() method returns an expected value? If it is "" than nothing will happen. Also, you can try to replace `if(v == Btn1){` with `if(v.equals(Btn1)){`, but the first one should work as they should be the same objects. – Jeffrey Klardie Oct 30 '13 at 14:59
  • something in the geterr() does not work,as the returned string is always empty:i updated my question,any idea? – user44088 Oct 30 '13 at 15:29
  • 1
    Please create a new question. This is not related to the title anymore. – Jeffrey Klardie Oct 30 '13 at 15:37
0

define:

CheckBox Facebook_chk
TextView Facebook_name
TextView Name
TextView Id 
TextView Txterr
TextView Pass 

As Global Variables, like:

public class Second extends Activity implements OnClickListener {

    CheckBox Facebook_chk;
    TextView Facebook_name;
    TextView Name;
    TextView Id;
    TextView Txterr;
    TextView Pass;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.second);

         Facebook_chk = (CheckBox)findViewById(R.id.Cfbook);//Represents the facebook checkbox.
         Facebook_name = (TextView)findViewById(R.id.Face);//represents the facebook text.
         Name = (TextView)findViewById(R.id.Name);//represents the Name text.
         Id = (TextView)findViewById(R.id.Id);//represents the Id text.
         Txterr = (TextView)findViewById(R.id.Txterr);//represents the Id text.
         Pass = (TextView)findViewById(R.id.Pass);//represents the Pass text.

...
}
...
}

UPDATE:

If you would like to make a View to respond for Click Event, you'll need to make sure you've set that View clickable.

Since you did:

View v = findViewById(R.id.Btn1);
v.setOnClickListener((OnClickListener) this);

I've no idea R.id.Btn1 is really a Button or just a View. If it is a Button, please change to:

Button button = findViewById(R.id.Btn1);
button.setOnClickListener(this);

If it's not a button, just some View and you want it to respond to your Click, please add one line after findViewById

v.setClickable(true);

Again, if you intend to use this v sometime later in your code, you need to declare it as a global variable just like you did on your TextViews

Ref public void setClickable (boolean clickable)

dumbfingers
  • 7,001
  • 5
  • 54
  • 80
  • I declared the vars outside,but now the app collapses once launched.i edited the question with my recent code.any solution? – user44088 Oct 30 '13 at 12:42
  • @user44088 please change your code like the example I gave you in my answer. – dumbfingers Oct 30 '13 at 13:09
  • @user44088 they reason why u got a "collapse" is because you're trying to `findViewById` before the `View` is loaded and that will cause a `NullPointerException`. `findViewById` should be called, in your case, after `setContentView`. – dumbfingers Oct 30 '13 at 13:10
  • Changed according yo your code,thanks.now the app does run,but the code does not work(the button click does not respond,and the error string does not appear).any thoughts? – user44088 Oct 30 '13 at 13:16
  • @user44088 would you please post your newest code as an update to your question? – dumbfingers Oct 30 '13 at 13:27
  • hi,i updated it to Button v = (Button) findViewById(R.id.Btn1); v.setOnClickListener((OnClickListener) this); yet it still does not function.any thoughts? – user44088 Oct 30 '13 at 13:50
  • @user44088 change your `Button` to global variable, get rid of `(OnClickListener)` inside your `v.setOnClickListener((OnClickListener) this); ` like: `v.setOnClickListener(this); ` – dumbfingers Oct 30 '13 at 13:58
  • okay,done.i edited my question:the txtview does not change according to the if conditions,but only to the last else .any thoughts? – user44088 Oct 30 '13 at 14:21
  • any idea why this might be? – user44088 Oct 30 '13 at 14:50
  • @user44088 to tell if two `String`s are same, you should use `String.equals` not `==`. e.g.:http://stackoverflow.com/questions/767372/java-string-equals-versus – dumbfingers Oct 30 '13 at 14:54
  • something in the geterr() does not work,as the returned string is always empty:i updated my question,any idea? – user44088 Oct 30 '13 at 15:31