why my playClick and stopClick button is never used? - android

this is my issues, so my button handler on activity stated my method was never used but i'm already create the button on XML
activity:
XML:

In your XML file change the tools:context in the root LinearLayout to the path of your Activity because this data is being used to bind the onClick action
As I can tell from the screenshots it should be changed to
tools:context=".LanderActivity"

Related

How the view are refereed under MainActivity In android

Here is the XML code for a simple TextView:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:id="#+id/test"/>
Then, in MainActivity, it is used like this:
TextView test = (TextView) findViewById(R.id.test);
test.setText("test");
I want to know how the view is accessed in Main class, which is defined in xml layout.
Can anyone explain how it happens?
how the view accessed in Main class
To be honest, i dont know the process in detail. Let me help you on what i know :
The Activity will search the layout XML in setContentView method.
After the layout has been found, we can use findViewById to link the instance (test - in your case) we created to the layout XML.
If the ID is found, the instance (Java) and XML will be linked.
Of course, you can do something like :
TextView test2 = new TextView(this);
Which means the instance is not must exist in XML.
Sorry English is not my native language.
On your Activity, you have:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//...
}
According to Android documentation, setContentView is responsible for linking your XML layout to your Activity:
Set the activity content to an explicit view. This view is placed
directly into the activity's view hierarchy. It can itself be a
complex view hierarchy.
After the Activity's content view is set, you can use findViewById in order to access the views of the XML linked to your Activity:
Finds a view that was identified by the id attribute from the XML that
was processed in onCreate(Bundle).
Hope it helps you to understand this process! :)
I think this is the method
1.when you create or declare a text view or something like that a reference is generated in your R file(JAVA file).
2.which is what you access through
R.id.test
this is how your linking works

One class for each layout (new page)?

I wast just wondering if the standard practice is to create an activity/fragment class for each layout file (new page).
Example:
MainActivity.java
onCreate(){
setContentView(R.layout.**start_page**)
}
And than when the user clicks a button in the action bar (or some other button on the screen):
onOptionItemSelected() {
switch XX -> case XX: setContentView(R.layout.**next_page**)
}
So could i do the above instead of launching a new activity.java (that contains a new layout.xml) with an intent, or inflating the view with a fragment.java (that also contains a new layout.xml).
I can see that the up/back navigation wouldn't work with the above code, but is that the only reason why you basically have to create two files (.java & .xml) for each new page in your app.
Yes you could do it technically but beware that if you already create an instance of view lets say Button and you change the layout button will be null because button is not located in your View and also it will take time to render again the layout. So it is a best practice to start a new activity or just create a fragment.
You can do that, but every view will be on the given Activity, and the event handlers would be in the same class, which isn't really modular. It could get extremely bloated and you'll have a 2000 line superclass because it handles every single button click in arbitrary functions (or even worse, in a single onClick function).

Calling an empty function from Android layout file crashes App

I have a checkbox in the Android layout file. When the checkbox is clicked i call an empty function in the activity class. This causes the app to stop working. Why is this?
I am assuming that you are using the "onclick" attribute in your XML.
If you are receiving a MethodNotFound exception, it could be one of two things:
You have a typo in either your Activity's method name or your XML's method name, or...
The visibility of your method in your Activity is not public.
When specifying onclick values in XML, the method should look like this in your activity:
public void myOnClickMethod(View v) { ... }

Button not fetched from XML? Null Pointer

newSubmitButton = (Button) findViewById(R.id.newPlayerSubmit);
Log.v("heeelp",""+newSubmitButton);
Seems simple enough. I have a global Button variable called newSubmitButton. I fetch the Button from an xml file in the project (I promise, the button exists, i didn't mispell the name, etc.) I output the button in the next line, it is null. I try to give it an onClickListener and it throws a null pointer exception. How is this button null? I just instantiated it the line before!
I just instantiated it the line
before!
No you didn't,
newSubmitButton = (Button) findViewById(R.id.newPlayerSubmit)
does not instantiate anything. It simply retrieves the button from the active view. If the button isn't part of the active view (perhaps the layout hasn't been inflated yet?) then your button reference will be null. Are you calling this code in your Activity's onCreate() method? Have you called setContentView() before executing the code in question?
Besides misspelling the name, you might have left out a call to setContentView().
(If that's not the issue, please post more code.)
Did you remember to setContentView() higher up? Just because R.java contains it doesn't mean it's attached to your view.

Switching Views/layouts

I have a problem that I can't seem to find the solution to.
I have an app that loads the main.xml file on startup, of course. In it are several buttons, and I want the buttons to take me to a different XML file. I just used setContentView(R.layout.newlayout.xml) method for that, and it works great.
The problem comes in after that. If I reference any of the buttons or other objects in the new layout, the app won't even finish loading before it errors out and closes on the emulator. However, if I take all references to objects out, the app runs fine.
I can navigate TO the new layouts, but their buttons can't do anything. Do I need to create a separate Java file for each layout? Or am I doing it all wrong? I'm trying to be as specific as I can. I suppose you could say I need to have different "pages" in my app as a website would.
I think what you are trying to do is best solved using multiple java files, each one defining it's own android Activity.
While it is possible to have multiple layouts/views in a single activity, this will generally make the code more complex and harder to read/debug in the future. By having each 'screen' in its own file, it will be a bit easier to manage all the different views you need to juggle.
The buttons and views only can refer to those mentioned in the current SetContentView() file..
u can test this by creating a button and initialising to an R.id... without setting the content view.. U will get a force close..
so if u change the XML file u shud initialise stuff again....
Ok, for anyone out there with the same problem and haven't figured out how to do it, as I said in my comment on ylebre, my Coworker and I have finally discovered how to do it. First off, we added
implements OnClickListener
to the class, after
extends Activity
then, we created a new java file, and at the beginning of the file it called
setContentView(R.layout.newlayout);
instead of main. Then, we made a button as follows:
Button button1 = (Button) findViewById(R.id.button01;
button1.setOnClickListener(this);
then later in the code:
public void onClick(View v) {
switch(v.getId()) {
case R.id.button01:
startActivity(new Intent(this, NEWJAVAFILE.class));
break;
}
}
And that's it! We just copied and pasted that code into NEWJAVAFILE, changed the names and such, and we were able to navigate freely back and forth. As ylebre said, all of the code for the new activity is in the NEWJAVAFILE.java. OH and don't forget to add the name of the java file to the manifest inside the tags:
<activity android:name=".NEWJAVAFILE">
</activity>
it all seems so simple now!

Categories

Resources