Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓在类主体中声明edittext(在任何方法之外)_Java_Android_Android Edittext_Declaration - Fatal编程技术网

Java 安卓在类主体中声明edittext(在任何方法之外)

Java 安卓在类主体中声明edittext(在任何方法之外),java,android,android-edittext,declaration,Java,Android,Android Edittext,Declaration,我有编程语言的经验,但对android编程有点陌生 我有一个带有一些字段的程序,这些字段用作标签(textview)、按钮和数据输入(edittext) 每当我在程序开始时从任何方法中声明它们时(当然是在类中),当我启动应用程序时,它会崩溃,模拟会发出“不幸的是,您的程序已停止”警报 Eclipse没有为声明提供任何错误,我使用了相同的方法来定义正则变量,没有任何问题。当我在类主体中声明mediaplayer对象时,它也给出了相同的错误 有人知道它为什么会出错吗? 还有没有其他方法来声明全局对象

我有编程语言的经验,但对android编程有点陌生

我有一个带有一些字段的程序,这些字段用作标签(textview)、按钮和数据输入(edittext)

每当我在程序开始时从任何方法中声明它们时(当然是在类中),当我启动应用程序时,它会崩溃,模拟会发出“不幸的是,您的程序已停止”警报

Eclipse没有为声明提供任何错误,我使用了相同的方法来定义正则变量,没有任何问题。当我在类主体中声明mediaplayer对象时,它也给出了相同的错误

有人知道它为什么会出错吗? 还有没有其他方法来声明全局对象,如edittext、viewtext等。。。在我看来,一遍又一遍地用方法声明它们听起来很奇怪

谢谢

公共课堂培训活动扩展活动{

Button stopTimer = (Button)findViewById(R.id.StopTimer);
Button startTimer = (Button)findViewById(R.id.StartTimer);
EditText totalTime = (EditText)findViewById(R.id.TotalTime);
EditText enterMin = (EditText)findViewById(R.id.EnterMin);
EditText enterSec = (EditText)findViewById(R.id.EnterSec);

private boolean breaker = false;

@Override

public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    startTimer.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Button_StartTimer();
        }
    });

    stopTimer.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Button_StopTimer();
        }
    });
}

您可以发布一些示例代码来说明这个问题吗?可以在类中声明一个成员变量,该变量是EditText或TextView


logcat(在DDMS中)也应该为您提供一些有关错误的信息。如果您使用eclipse,DDMS有一个选项卡,如果没有,您可以从命令行运行DDMS,查看logcat选项卡并启动您的应用程序(当然,您的手机通过usb插入)您应该能够看到报告的实际错误。

您可以在类主体或方法主体内声明这些变量。在前一种情况下,变量是全局的,因此可以在整个类内访问;在后一种情况下,变量是局部的,因此只能在该方法内访问。两者都可以是cOMM仅在程序设计中可见

在Android中,典型的应用程序是在类主体中声明变量并在onCreate()方法中实例化它们

public Class MyClass extends Activity{

     TextView label;// so this variable can be accessed within any methods in this Class
    @Override
    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(Bundle savedInstanceState);
          setContentView(R.layout.main) // load the layout of the activity
          label=(TextView)findViewById(R.id.<the TextView id defined in the layout file>); //this variable get instantiated. From now on you can manipulate it anywhere inside the class.
         Button submit=(Button)findViewById(R.id.<the Button id defined in the layout file>);//you declared and instantiated it, but it could only be used within this method since you declared it here.

}


    }
公共类MyClass扩展活动{
TextView label;//因此可以在此类中的任何方法中访问此变量
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(Bundle savedInstanceState);
setContentView(R.layout.main)//加载活动的布局
label=(TextView)findViewById(R.id.);//此变量将被实例化。从现在起,您可以在类中的任何位置操作它。
Button submit=(Button)findViewById(R.id.);//您声明并实例化了它,但由于您在此处声明了它,因此只能在此方法中使用它。
}
}

如果你只是在类主体中声明一个变量,在大多数情况下,在你实例化它之前,你不能使用它,因为在实例化之前它们是
null
。我认为这就是你有问题的原因。请发布日志,以便我们可以指定真正的问题。

如果没有看到你正在尝试的示例代码,就无法说明问题确切地说(我们不介意在这里阅读)。但让我猜猜,你在做这样的事情吗

public class MyActivity extends Activity {

    TextView tv1; // This is fine.
    TextView tv2 = (TextView) findViewById(R.id.textview2); // Don't do this.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv1 = (TextView) findViewById(R.id.textview1); // This is fine
        tv1.setText("Some text"); // This works

        tv2.setText("Some text"); // NullPointerException here

    }
}
tv2.setText(…)
将失败,因为在调用
setContentView(…)
之前使用了
findViewById(…)
,因此,
tv2
将为空


活动中将小部件声明为实例成员是完全可以接受的,但在设置内容视图之前,不要尝试使用
findViewById(…)

尝试仅在onCreate()方法之外声明小部件对象名称

然后在onCreate()内的setContentView()之后初始化它们


猜得好,巴迪:)我试图使用findViewById。问题是我不想一次又一次地启动相同的对象,以便能够在我的方法中使用它。我相信如果我在OnCreate中启动它们,由于语言限制,它将只在OnCreate中启动,如果我缺少任何内容,请更正。在这种情况下,是否可以全局创建和启动我的xml对象?对于其他想知道的人,是的,Mstersquonk给出的示例代码就是我正在做的。一旦您在
onCreate(…)中调用
findViewById(…)
然后它们将继续指向整个
活动范围内的各种对象。换句话说,上面我的代码中的
tv1
将对
MyActivity
中的任何和所有方法有效,直到
MyActivity
被销毁。很清楚,这基本上允许全局访问它们?
Button stopTimer;
Button startTimer;
EditText totalTime;
EditText enterMin;
EditText enterSec;
setContentView(R.layout.main);
stopTimer = (Button)findViewById(R.id.StopTimer);
startTimer = (Button)findViewById(R.id.StartTimer);
totalTime = (EditText)findViewById(R.id.TotalTime);
enterMin = (EditText)findViewById(R.id.EnterMin);
enterSec = (EditText)findViewById(R.id.EnterSec);