【问题标题】:Declaring buttons in android studio在 android studio 中声明按钮
【发布时间】:2016-02-22 12:01:37
【问题描述】:

在 Android Studio java 类中声明按钮的最佳方式是什么?例如,我有按钮和文本字段:

TextView t = (TextView) findViewById(R.id.Question);
TextView A = (TextView) findViewById(R.id.AnswerA);
TextView B = (TextView) findViewById(R.id.AnswerB);
TextView C = (TextView) findViewById(R.id.AnswerC);
TextView D = (TextView) findViewById(R.id.AnswerD);
Button next_test = (Button)findViewById(R.id.Next_Text);
Button b = (Button)findViewById(R.id.button);

当我像这样在类下面声明它们时,Android Studio 会抛出错误:

public class main extends AppCompatActivity {

TextView t = (TextView) findViewById(R.id.Question);
TextView A = (TextView) findViewById(R.id.AnswerA);
TextView B = (TextView) findViewById(R.id.AnswerB);
TextView C = (TextView) findViewById(R.id.AnswerC);
TextView D = (TextView) findViewById(R.id.AnswerD);
Button next_test = (Button)findViewById(R.id.Next_Text);
Button b = (Button)findViewById(R.id.button);

@Override
protected void onCreate(Bundle savedInstanceState) {..........

当我在它们需要的方法中声明它们时它不会抛出错误,但是我必须多次声明它们并在 oncreate 中声明它们。

有没有办法只声明一次?

【问题讨论】:

  • 你应该declare他们作为类变量,assign在你的onCreate,设置contentView后。

标签: java android android-studio jbutton


【解决方案1】:

您的成员变量应该可以在您的类中全局访问。但是如果 xml 没有被膨胀,你就无法通过 id 找到视图。你的代码应该是这样的:

public class main extends AppCompatActivity {

TextView t;
TextView A; 
TextView B;

@Override
protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.your_layout); // THIS IS VERY IMPORTANT

t = (TextView) findViewById(R.id.Question);
A = (TextView) findViewById(R.id.AnswerA);
B = (TextView) findViewById(R.id.AnswerB);
....
}

findViewById 方法在 R.layout.your_layout 中查找您的视图...如果您不调用 setContentView(),它将找不到任何内容并返回 null。希望这是有道理的:)

【讨论】:

    【解决方案2】:

    声明视图的正确方法:

    public class main extends AppCompatActivity {
    
    TextView tv;
    
    @Override
     super.onCreate(savedInstanceState);
     setContentView(R.layout.yout layout name);
     tv = (TextView) findViewById(R.id.Question);
    }
    

    这适用于所有视图,如按钮、图像视图、线性布局、图像视图等......

    【讨论】:

      【解决方案3】:

      你可以这样做

      public class main extends AppCompatActivity {
      
         private TextView t;
         private TextView A;
         private TextView B;
         private TextView C;
         private TextView D;
         private Button next_test;
         private Button b;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              initialize();
         }
      
         private void initialize(){
              t = (TextView) findViewById(R.id.Question);
              A = (TextView) findViewById(R.id.AnswerA);
              B = (TextView) findViewById(R.id.AnswerB);
              C = (TextView) findViewById(R.id.AnswerC);
              D = (TextView) findViewById(R.id.AnswerD);
              next_test = (Button)findViewById(R.id.Next_Text);
              b = (Button)findViewById(R.id.button);
         }
      

      【讨论】:

        【解决方案4】:

        OnCreate 方法中声明它们

        public class main extends AppCompatActivity {
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity_layout);
        TextView t = (TextView) findViewById(R.id.Question);
        TextView A = (TextView) findViewById(R.id.AnswerA);
        TextView B = (TextView) findViewById(R.id.AnswerB);
        TextView C = (TextView) findViewById(R.id.AnswerC);
        TextView D = (TextView) findViewById(R.id.AnswerD);
        Button next_test = (Button)findViewById(R.id.Next_Text);
        Button b = (Button)findViewById(R.id.button);
        .....
        }
        

        您应该始终在 onCreate 方法中初始化它们并放在后面

         setContentView(R.layout.your_activity_layout);
        

        【讨论】:

        • 这就是OP已经尝试过的,他对结果不满意。
        • 不,不是。查看 vasilis 或 NJ Nilesh J 的答案
        【解决方案5】:

        您必须在onCreate 中声明您的观点。将您的findViewById 方法放入onCreate

        【讨论】:

        • 如果您在 onCreate 中声明,那么它们将无法在 onCreate 之外访问。也许您的意思是分配,而不是声明?
        • 我认为问题不清楚。我没有看到这句话“我想在全球范围内使用我的字段”等。你知道我的意思
        • 问题很明确:It doesn't throw errors when I declare them in the methods they are need in, but then I have to declare them multiple times and also declare final in the oncreate.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 2014-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多