开发者

Linking a button click to existing class files

I am trying to incorporate some sample code I pulled from a site into my existing project. I click a button in my project, and I want that to link to the class file that I imported into my project.

Initially, the project I imported automatically launched their own file upon startup, whereas I need my project to automatically launch my main.xml file. I changed around the manifest and the class file to point to a new .xml (rather than main) so as to not point to the wrong xml file.

The program runs fine but when I clic开发者_高级运维k the button that I want to open the imported .java file it tells me: The application...has stopped unexpectadely. Please try again.

Is there something I might be missing in fine tuning the code to have what I imported be a sub-class that can only be accessed on a click rather than the main class?

Thanks for any help.


As Kevin said, an Activity wouldn't be pointing to an XML file, so I'm not sure if I am understanding your question correctly, but try this (if you haven't already): When your button is clicked, create a new Intent object, and code your Intent to launch whatever Activity you have created that will inflate your XML file.

For example, if you want to display a Picture when you click the button, you could create an Activity that shows some pre-loaded bitmap. To launch a PictureDisplay activity, PictureDisplay.java would be your Activity class (arbitrary name, call it whatever you want), and under normal circumstances this Activity would inflate some XML file (let's call it picture_display.xml) in the onCreate method, and this XML file could have an ImageView that gets displayed.

So, setup your Intent like this (when the button is clicked):

Intent intent = new Intent(getApplicationContext(), PictureDisplay.class);
startActivity(intent);

Then, in your PictureDisplay.java class, add code to inflate the XML file in your onCreate function:

setContent(R.layout.picture_display);

Just be sure to add the new class to the Manifest file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜