Android的变量可用性(无法解析) [英] Android variable availability(can not be resolved)

查看:176
本文介绍了Android的变量可用性(无法解析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图运行我的应用程序,我注意到,声称一些错误,很多变量不能得到解决,即使在code声明
我把它改成下面的code,但一旦我进入应用程序,它崩溃:
公共字符串GetErr(){

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;

编辑:现在geterr()返回在任何时候都一个空字符串。
我会比更乐意接受一个合适的回答或解决问题。

Now the geterr() returns an empty string at all times. i will be more than glad to receive a proper answer or solution to the matter.

推荐答案

您在声明中的的onCreate()方法变量,所以这是它们的范围。您不能将此功能之外使用它们。所以,当你在 GetErr()方法使用它们,你会得到一个错误。您可以通过移动你多种方法来全局变量所需的变量解决这个问题(所以在课堂上,而不是在方法声明它们。

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.

修改

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);
            }
        }
    }

}

这篇关于Android的变量可用性(无法解析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆