简体   繁体   English

无法加载适用于Android应用的Java编程

[英]Java Programming For Android App Won't Load

I've just been making an android app using the android developer kit and I created a layout with a medium text and 1 button 我刚刚使用android开发人员工具包制作了一个android应用程序,并且创建了一个带有中等文字和1个按钮的布局

it is meant to show some text in a dialogue when you click the button but when I run the app on my device to test it freezes and doesn't show the button or text I added then my whole device freezes. 它的意思是当您单击按钮时在对话框中显示一些文本,但是当我在设备上运行该应用程序进行测试时,它冻结了,并且不显示我添加的按钮或文本,则整个设备冻结了。

I can't figure out what I've don't wrong I'm am a complete noob at this so I'm hoping someone can help me figure it out. 我无法弄清楚我没记错的地方,我是一个完全的菜鸟,所以我希望有人可以帮助我解决这个问题。

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();
    }

}

In the button you are referring to the android resources, and that doesn't have your button. 在按钮中,您指的是android资源,但没有按钮。 Remove the android. 删除android. and it will work! 它会工作!

If there is anything in the logcat, that would be helpful. 如果logcat中有任何内容,那将很有帮助。 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 假设您在activity_main.xml具有androidid="@+id/button1"Button ,则此行应为

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 . 当您使用名称空间android.R.someReference您正在告诉它寻找预定义的Android resource But here the resource is one that you have declared yourself so you leave off the android. 但是这里的resource是您已经声明自己的resource ,因此您无需使用android.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM