Java android studio,无法识别按钮对象,

Java android studio,无法识别按钮对象,,java,android,object,button,Java,Android,Object,Button,导入视图和按钮后。 我已经为Button创建了一个对象,在本例中为“thomasButton” 但错误表明,即使我在下一行调用了“thomasButton”字段,也没有使用它 过了一段时间,我发现如果我把这个字段放在另一个作用域中,它就可以被识别。就像这样,但程序仍然不会运行(启动时崩溃)。你们知道为按钮设置LongClickListener的正确方法吗 package com.example.thoma.event; import android.support.v7.app.AppComp

导入视图和按钮后。 我已经为Button创建了一个对象,在本例中为“thomasButton”

但错误表明,即使我在下一行调用了“thomasButton”字段,也没有使用它

过了一段时间,我发现如果我把这个字段放在另一个作用域中,它就可以被识别。就像这样,但程序仍然不会运行(启动时崩溃)。你们知道为按钮设置LongClickListener的正确方法吗

package com.example.thoma.event;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

public void displayMessage(View v){
    TextView thomasText = (TextView)findViewById(R.id.txt_View);
    thomasText.setText(R.string.rsc_Text2);x
}

Button thomasButton = (Button)findViewById(R.id.btn_Change);
// weird but I can only use Button object within an inner scope
{
    thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            TextView thomasText = (TextView) findViewById(R.id.txt_View);
            thomasText.setText("Artificial");
            // it will return false if long click wasnt long enough
            // and normal click will be called
            return true;
        }
    });
}
}

崩溃:不幸的是,应用程序已停止

onCreate()中初始化按钮
检查下面的代码。findViewById代价很高,因此如果您尽可能少地找到这些ID,效果会更好

package com.example.thoma.event;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     Button thomasButton = (Button)findViewById(R.id.btn_Change);

     TextView thomasText = (TextView) findViewById(R.id.txt_View);

     thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            thomasText.setText("Artificial");

            // it will return false if long click wasnt long enough
            // and normal click will be called

            return true;
        }
    });

}

public void displayMessage(View v){
    TextView thomasText = (TextView)findViewById(R.id.txt_View);
    thomasText.setText(R.string.rsc_Text2);
}


}
试试这个

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

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

         Button thomasButton = (Button)findViewById(R.id.btn_Change);
         thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    TextView thomasText = (TextView) findViewById(R.id.txt_View);
                    thomasText.setText("Artificial");
                    return false;
                }
            });
    }


    }

你能分享完整的代码吗?什么是崩溃?分享完整的代码也谢谢你的时间R2R!很高兴帮助你:)
package com.example.thoma.event;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
TextView thomasText;
Button thomasButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    thomasText = (TextView)findViewById(R.id.txt_View);
      thomasButton = (Button)findViewById(R.id.btn_Change);
      thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
             thomasText.setText("Artificial");
            // it will return false if long click wasnt long enough
            // and normal click will be called
            return true;
        }
    });

}

public void displayMessage(View v){
    thomasText.setText(R.string.rsc_Text2);
}



}