简体   繁体   中英

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

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. Remove the android. and it will work!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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