Saturday, April 28, 2012

Re: [android-developers] Re: Fragment problems (converting an app)

These are just compile problems.

The general rule is: start fixing them top to bottom.

As you do that, some of the latter ones may go away, because the compiler's comprehension of your file can get thrown off by earlier errors and it never recovers.

This is especially true for mismatched curly braces, because they signal an end to a lot of things: classes, methods, control statements.

Eclipse has a really good "fix suggestions" thing, where you position the cursor at an error in the source window and press Ctrl+1 and get a detailed explanation and a few suggested fixes.

Use a Java guide when in doubt about what's wrong. Don't be afraid to Google for the exact error, or for a similar code fragment.

Stay focused, take breaks, don't drink too much coffee, practice yoga three times a week (ok, just kidding).

Now to your source. You didn't indicate which lines are marked with errors, but I *think* I see at least two:

1) The giant blob of code starting with:

     .setItems(Facebook, new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int item) {

should end with:

     default:
     return;
     }
     }).create();

Count the curly brackets and parens.

Find some examples on anonymous inner classes, count the curly brackets and parens there, should give you an idea.

2) In this code fragment:

     return new AlertDialog.Builder(getActivity())
     .setTitle(title)
     s.setItems(Twitter,

What in the world is "s." and what is it doing here?

Should be just:

     return new AlertDialog.Builder(getActivity())
     .setTitle(title)
     .setItems(Twitter,

And this brings me to another suggestion: when you can't figure out what's wrong with a long, long, long compound statement, start breaking it down into more simple pieces by using temporary variables.

The first code fragment could be rewritten like this:


DialogInterface.OnClickListener() listener =  {
@Override
public void onClick(DialogInterface dialog, int item) {
.. same thing as before
}
};

Context context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(context);

builder.setTitle(title);
builder.setItems(Facebook, listener);

return builder.create();

This is easier to read and to ensure that there are no mismatched tokens, that every statement is terminated with a semicolon, etc.

-- K

28 апреля 2012 г. 22:05 пользователь Bas Verhoog <basverhoog95@gmail.com> написал:
No ideas? :(

I really need some help here...

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate