Download Android Studio Activities and Errors

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
www.programmingmoney.com
Android Studio, Activities, and Errors
1
Android Studio, Activities, and Errors by Anthony Popp
(Programming Man)
This post is dedicated to documenting the frustrations about Android Studio, Android Activities, and
the errors they can produce.
For what seemingly looks like a how-to guide for Android Studio, becomes what I think most
programmers fear, never-ending errors. Chalk full of pictures, text, and possibly meaningful code,
light sarcasm will get us through this together That said,
NEVER LET ANYTHING GET IN YOUR WAY!
Error: Build Tools Not Installed
It started out with a simple Android Studio project, after I updated to 1.0.2. I was going to use the
Android L preview... or the app design interface which was so nicely integrated into Android Studio.
There was an error. I needed to install the latest build tools.
A tiny innocent message in Android Studio, asking me to update the Build Tools
I opened up the Android SDK Manager, and I realized that "I have not been here in a while".
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Android Studio, Activities, and Errors
Android SDK Manager. I guess I had to install some recent updates. 32 Packages worth of updates
I press the install button, and there was a slow internet connection at the time 439KB/s.
Here is the message log, as the update progresses.
The next day, everything was finished installing. Awesome, I started-up Android Studio, and
everything is working fine.
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
2
www.programmingmoney.com
Android Studio, Activities, and Errors
I restarted Android Studio.
Error: Android L Layout Library Needs to be Updated
I go ahead and create a new project in Android Studio. I call it AwesomeProgram which is cool. I
make the default activity and ActionBar Activity.
NEVER LET ANYTHING GET IN YOUR WAY!
Everything is fine until I am presented with a message informing me that Android L is outdated.
"Alright, I guess that I still need to accept the Android L License agreement". I do.
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
3
www.programmingmoney.com
Android Studio, Activities, and Errors
Yup, I have the build tools, but not Android L. I still need to do more updates.
Here is the Android L License Agreement, which needs to be accepted before I can use it.
Everything is fixed. This is what the initial layout in Android Studio looks like. Hurrah.
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
4
www.programmingmoney.com
Android Studio, Activities, and Errors
5
Okay. This is nice. Now I can begin to explain what was going to be an educational blog post. Rather,
it is a guide about possible ways to fix the following errors in Android Studio regarding Activities.
Android Studio, Activities, and Errors
When you create a new activity in Android Studio, the following screen will appear:
To create a new Blank Activity, right-click in your project file navigator and select New > Activity > Blank Activity

Activity Name: The java class file name that goes in the app/java/packageName folder

Layout Name: The xml file name that goes in the app/res/layout folder

Title: The activity's title. Android Studio defaults it to the activity name.

Menu Recourse Name: The xml file name that goes in the app/res/menu folder.

[_] Launcher Activity: Check this box if the new activity is going to be the start-up activity.

Hierarchical Parent: This sets the parentActivityName attribute in your Manifest.xml file.

Package name: This is your package name of your project.
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Android Studio, Activities, and Errors
6
Android Manifest
After creating the new Activity, the manifest xml file automatically updates. Here is what my code
looked like
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".exampleActivity"
android:label="@string/title_activity_example" >
</activity>
Java Class
In this java class, which has been created for you, there is the usual onCreate method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
}
My android project had an ActionBarCompact, and some new code which I have never seen before. I
know what it does now. This function inflates the ActionBar. It sounds funny. Basically, when you
press the action bar button, the menu pops up.
@Override
public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items
to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Android Studio, Activities, and Errors
7
Warning: Remove Hard-coded Text
Hard coding text attributes is bad. Forgetting to change all of the corresponding values could cause
the activity to convey the wrong message. Having all of the activities point to one string saves time
and confusion. The best part, Android studio makes it easy for you. If this does occur, you will most
likely be presented with the following message.
In my first case, the warning applied to the second bullet: When creating configuration variations (for example for landscape or
portrait) you have to repeat the actual text (and keep it up to date when making changes)
Inside of the layout activity XML file, you might have notice that Android Studio auto generated the
following code. If you hover over them, if may suggest to put these numbers in the strings.xml file. I
happened to do this by accident.
I think it does it automatically was because I placed the TextView using the Android L Layout Library.
If I programed it myself, I would need to manually add the strings (see section below).
android:paddingLeft ="16dp"
android:paddingRight ="16dp"
android:paddingTop
="16dp"
android:paddingBottom="16dp"
Once the string has been added, and the message pops up next to the hardcoded number, click on
the value. Android Studio will change it for you. This is what it looks like after Android Studio does the
work. Side Note: This I what I pasted, even though I copied the text above. I wonder what sort of
background stuff if going on???
android:paddingLeft ="@dimen/activity_horizontal_margin"
android:paddingRight ="@dimen/activity_horizontal_margin"
android:paddingTop
="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
You will also notice that “Hello World” has also been created for you inside you black activity.
NEVER LET ANYTHING GET IN YOUR WAY!
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Android Studio, Activities, and Errors
8
Error: Invalid Menu Title and Name
Fixing the Item Title
Android Studio will give you another green box, if the name of a menu item is wrong. This ActionBar is not doing much actions.
Android Studio might give you this message. While trying to make a string, do not do what is shown
above. @string refers to the strings.xml file. Put the string there, and set the text to it. Only then, can
spaces can be added.
As mentioned above, sometimes these values need to be added manually to the strings.xml file.
Inside your strings.xml file, manually add the text “Look at me”. Back in
your res/menu/example_menu.xml file for your activity, the id can be set.
Fixing the Item Id
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Android Studio, Activities, and Errors
9
Yet another error in Android Studio. This time it deals with duplicate Item ID's. This is an easy fix because it was caused by a simple
copy and paste.
If you try to copy and paste the previous Item, you will get the above error. The id is a reference point.
For example, if the item is clicked, it allows the program to identity what item was clicked. Duplicate
ids cause errors.
Also, you declare the name of the id on that line by adding a plus '+' in front of it. I give it the same as
name as the Item title. If the plus '+' is not added, it would just be a reference to it.
What? The Menu is Out of Order
Another simple error. But it actually happened. I call it the learning process.
The line android:orderInCategory="100” determines the order in which menu items appear (as shown
in the Android L preview pane). It does not matter what the lowest number is above 0.
Smaller numbered items appear higher up in the menu. Lower numbered items appear lower down in
the menu. If two items have the same orderInCategory number, the later coded item appears lower
down.
NEVER LET ANYTHING GET IN YOUR WAY!
Back to exampleActivity.java
Do we just want a pretty menu to show up? Not really. We should make it functional. This is done in
the onOptionsItemSelected function. Be warned, even more error are documented. Scroll up, or face
reading about more errors in Android Studio.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Android Studio, Activities, and Errors
10
if (id == R.id.action_settings ||
id == R.id.action_look_at_me) {
//Menu item id checked here
return true;
}
return super.onOptionsItemSelected(item);
}
Fragments and Aggravation
At this point of yet another article on Android Studio, I experimented with fragments, and that is where
the issue final issue is. I know why the error exists, but fixing it is out of question. Instead, I have
logged everything to that point in the program.
NEVER LET ANYTHING GET IN YOUR WAY!
What is an Android Fragment?
A fragment is like an external piece of an activity. Rather than programming everything to the activity,
fragments can be used. An example of when a fragment would be a good option is if your app has
screen rotation. In the Android L layout library, this is automatically done for you when you press the
screen orientation button. That is located somewhere at the top..
In regards to the ActionBarActivity, fragments can be used to hide and show different tabs.
See ActionBar.Tags
Errors Galore
Changing activates with findViewById
I have been exploring this area. I found that if you need to get the id of something in your android
application, just call this function, and cast it to your desired type of choice. Then you set an
onClickListener. My apologies if this is elementary, or if any of this Android Studio information is. It is
important for me to document it.
As an example, this is a made up button, programmed inside the Menu.xml file:
id="@+id/BUTTON_ID_HERE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/BUTTON_ID_HERE " />
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Java class imports:
Android Studio, Activities, and Errors
11
import android.widget.Button;
import android.view.View.OnClickListener;
Inside the java class's OnCreate method:
Button button;
button =(Button)findViewById(R.id.BUTTON_ID_HERE);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View veiw) {
//Do Stuff Here
}
}); //Remember to type this. I got an error
Syntax Failure
Another error with my onClickListener. I wish this would listen to me. It ends up that I forgot not just the evil semicolon of doom, but
the partner in crime as well, a closing parentheses.
After I fixed that error, another one popped up. Hopefully you do not get as many errors as I
do. This answer on stack overflow helped me.
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Android Studio, Activities, and Errors
12
Even though my intent was to change activities, my constructor's intent says otherwise: Cannot resolve constructor
'Intent(android.view.View.OnClickListener..."
All of the errors are fixed! Android Studio is being nice... until I ran the program. I received this error.
Process: com.poppgames.awesomeprogram, PID: 20200
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.poppgames.awesomeprogram/com.poppgames.awesomeprogram.MainActivity}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2497)
at android.app.ActivityThread.access$900(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5678)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
… and so on for about 50,000 years
I put a breakpoint right before onCreateActivity, and it ends up that id/Button is null.
Typical
NEVER LET ANYTHING GET IN YOUR WAY!
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames
www.programmingmoney.com
Android Studio, Activities, and Errors
13
My error resulted because the main activity does not have a button. The button is located inside a
fragment.
Looking at this blog post by Mike James, I was able to make my button say “Clicked”.
The author explains why this does not permanently change the text of the button, but rather
temporarily change it. When I ran app, the fragment reset itself, just like the author described.
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button button =(Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((Button)view).setText("Clicked");
}
});
return rootView;
}
We should stop here for today, and call it a happy ending.
I am going try to read through Mike’s entire series. It is higher detailed, and to the point. In addition, it
is very recent and up to date. I also have some Android books. Perhaps I find C++, Java, and C#
enjoyable. It is the Android Framework that is very confusing. LibGDX on the other hand, is right up
my ally.
Copyright 2015 © by Anthony Popp All rights reserved
poppgames.wix.com/poppgames