Skip to content Skip to sidebar Skip to footer

Android Variable Availability (can Not Be Resolved)

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 o

Solution 1:

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;

publicclassSecondextendsActivityimplementsOnClickListener {
    CheckBoxFacebook_chk;
    TextViewFacebook_name;
    TextViewName;
    TextViewId;
    TextViewTxterr;
    TextViewPass;
    ButtonBtn1;


    /** Called when the activity is first created. */@OverridepublicvoidonCreate(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(newCompoundButton.OnCheckedChangeListener(){

            @OverridepublicvoidonCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stubif(Facebook_chk.isChecked())
                    Facebook_name.setEnabled(true);
                elseFacebook_name.setEnabled(false);
                ;
            }
        });

    }


    publicStringGetErr(){

        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;
    }
    @OverridepublicvoidonClick(View v) {
        if(v == Btn1){
            String err = GetErr();
            if(err != ""){
                Txterr.setText(err);
            }
        }
    }

}

Solution 2:

define:

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

As Global Variables, like:

publicclassSecondextendsActivityimplementsOnClickListener {

    CheckBox Facebook_chk;
    TextView Facebook_name;
    TextView Name;
    TextView Id;
    TextView Txterr;
    TextView Pass;
    @OverridepublicvoidonCreate(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:

Viewv= 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:

Buttonbutton= 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)

Post a Comment for "Android Variable Availability (can Not Be Resolved)"