-2

在尝试运行我的应用程序时,我注意到一些错误声称无法解析许多变量,即使在代码中声明我将其更改为以下代码,但是一旦我进入应用程序,它就会崩溃:

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() 始终返回一个空字符串。

4

2 回答 2

0

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

编辑

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

}
于 2013-10-30T12:30:32.467 回答
0

定义:

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

作为全局变量,例如:

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.

...
}
...
}

更新:

如果您想View对 Click 事件做出响应,则需要确保您已设置View可点击。

既然你这样做了:

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

我不知道R.id.Btn1是真的 aButton还是只是 a View。如果是Button,请更改为:

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

如果它不是一个按钮,只是一些View,并且您希望它响应您的点击,请在后面添加一行findViewById

v.setClickable(true);

同样,如果您打算v稍后在代码中使用它,则需要将其声明为全局变量,就像您在TextViews上所做的那样

Ref public void setClickable(布尔可点击)

于 2013-10-30T12:33:11.860 回答