【问题标题】:button question按钮问题
【发布时间】:2009-10-13 11:12:35
【问题描述】:

我最近开始使用 android。

这段代码我已经写好了

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ArrangeMe extends Activity {
    private Button button1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        this.button1 = (Button)findViewById(R.id.buttonOne);
        this.button1.setOnClickListener(new OnClickListener() {
            //@Override
            public void onClick(View v) {
                finish();
            }
        });
        setContentView(R.layout.main);
    }
}

我的 main.xml 如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="ArrangeMe"/>
<Button android:text="Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonOne"></Button>
</LinearLayout>

但是当我通过这条线时

this.button1 = (Button)findViewById(R.id.buttonOne);

我观察到 button1 = null。但是当我输入 R.id 时。 eclipse 确实建议了一个自动完成 buttonOne(这表明布局 xml 是正确的!)

我哪里错了?

编辑:

有趣的是,我尝试了以下代码,

按钮仍然没有出现。它已经停止崩溃,但按钮没有出现!

   button1 = new Button(getContext());
    button1.setText("1");
    addView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    button1.setOnClickListener(new OnClickListener() {
        //@Override
        public void onClick(View v) {
            finish();
        }
    });

这是报错

我把它们改成

   button1 = new Button(getBaseContext());
    button1.setText("1");
    addContentView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    button1.setOnClickListener(new OnClickListener() {
        //@Override
        public void onClick(View v) {
            finish();
        }
    });

【问题讨论】:

    标签: android button


    【解决方案1】:

    我认为你应该调用 setContentView(R.layout.main);之后,您的活动类应该知道在那里查找视图。

    【讨论】:

      【解决方案2】:

      您在 Activity 中设置 ContentView 之前分配了 button1。修改代码如下,

      import android.app.Activity;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      
      public class ArrangeMe extends Activity {
          private Button button1;
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main); // you misplaced this line in your code
      
      
              this.button1 = (Button)findViewById(R.id.buttonOne);
              this.button1.setOnClickListener(new OnClickListener() {
                  //@Override
                  public void onClick(View v) {
                      finish();
                  }
              });
          }
      }
      

      【讨论】:

        【解决方案3】:

        您必须使用小写字母作为视图的 id,第二个是构建您的项目并清理可能为 R.java 中的按钮生成 id。

        【讨论】:

          猜你喜欢
          • 2022-07-24
          • 2017-03-02
          • 2022-01-05
          • 2015-02-14
          • 2013-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多