0

我刚刚使用 android 开发人员工具包制作了一个 android 应用程序,并创建了一个带有中等文本和 1 个按钮的布局

它的目的是在您单击按钮时在对话框中显示一些文本,但是当我在设备上运行应用程序以测试它时,它会冻结并且不显示我添加的按钮或文本,然后我的整个设备就会冻结。

我无法弄清楚我没有错在这方面我是一个完全的菜鸟,所以我希望有人能帮我弄清楚。

public class MainActivity extends Activity implements OnClickListener {

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

        Button button1 = (Button)findViewById(android.R.id.button1);
        button1.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Dialog d = new Dialog(this);
        d.setTitle("Success!");
        TextView tv = new TextView(this);
        tv.setText("Some Text Here.");
        d.setContentView(tv);
        d.show();
    }

}
4

2 回答 2

1

In the button you are referring to the android resources, and that doesn't have your button. Remove the android. and it will work!

于 2013-08-13T00:44:00.097 回答
0

If there is anything in the logcat, that would be helpful. But I believe your problem is here

 Button button1 = (Button)findViewById(android.R.id.button1);

Assuming you have a Button in your activity_main.xml with androidid="@+id/button1", this line should be

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

When you use the namespace android.R.someReference you are telling it to look for a pre-defined Android resource. But here the resource is one that you have declared yourself so you leave off the android.

于 2013-08-13T00:40:22.477 回答