【问题标题】:Android variable availability (can not be resolved)Android 变量可用性(无法解决)
【发布时间】:2013-10-30 12:00:33
【问题描述】:

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

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

【问题讨论】:

  • 移植你的错误日志,错误发生的地方
  • 您发布了两次 GetErr
  • 如果您希望其他人能够有效地帮助您,您需要提供有关确切错误消息的信息。
  • arg0 在哪里初始化?
  • 请遵循 java 命名约定...

标签: java android


【解决方案1】:

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

}

【讨论】:

  • 我在外面声明了变量,但是现在应用程序一旦启动就崩溃了。我用我最近的代码编辑了这个问题。有什么解决方案吗?
  • 您应该粘贴崩溃日志,以便我们查看问题所在。
  • 模拟器显示“不幸的是,app_name 已停止”,一旦我移动到第二个意图(第二个窗口按钮)
  • 再次,请粘贴导致应用停止的崩溃。您可以在日志中找到它(您可以在命令行中使用adb logcat,也可以在 Eclipse 中打开 logcat 视图)。
  • 请创建一个新问题。这与标题无关了。
【解决方案2】:

定义:

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 来响应点击事件,您需要确保您已将View 设置为可点击。

既然你这样做了:

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

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

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

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

v.setClickable(true);

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

参考public void setClickable (boolean clickable)

【讨论】:

  • 我在外面声明了变量,但是现在应用程序一旦启动就崩溃了。我用我最近的代码编辑了这个问题。有什么解决方案吗?
  • @user44088 请按照我在回答中给您的示例更改您的代码。
  • @user44088 他们之所以会出现“崩溃”是因为您在加载View 之前尝试findViewById,这将导致NullPointerException。在您的情况下,findViewById 应该在 setContentView 之后调用。
  • 根据您的代码更改,谢谢。现在应用程序可以运行,但代码不起作用(按钮单击没有响应,并且错误字符串没有出现)。有什么想法吗?跨度>
  • @user44088 您能否发布您的最新代码以更新您的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
相关资源
最近更新 更多