My Log line says animalclass is 4 but instead of going to "case 4" it returns default. It works with case 2 though. How is this possible? Thanks in advance.
public int gettile(int animalclass) {
Log.e("gettile", "animalclass = " + animalclass);
switch (animalclass) {
case 1: //
tile=R.drawable.picnictile;
break;
case 2: //
tile=R.drawable.picnictile;
break;
case 3: //
tile=R.drawable.picnictile;
case 4: //
tile=R.drawable.picnictile;
case 5: //
tile=R.drawable.face;
default:
Log.e("gettile", "failed!!!!!!!!!! = " + animalclass);
tile=R.drawable.rainbowtile;
break;
}
Log.e("gettile", "returning = " + tile);
return tile;
}
you need to use break; to stop other case execution because without break it will execute the correct case block statements and also "default" code block
Try adding the Break statemenet after all cases.
case 3: //
tile=R.drawable.picnictile;
break;
case 4: //
tile=R.drawable.picnictile;
break;
case 5: //
tile=R.drawable.face;
break;
If you dont break it after the "thing" the case should do, the switch does not work correct.
You forgot to add a break keyword before the default keyword.
Try this one:
public int gettile(int animalclass) {
Log.e("gettile", "animalclass = " + animalclass);
switch (animalclass) {
case 1: //
tile=R.drawable.picnictile;
break;
case 2: //
tile=R.drawable.picnictile;
break;
case 3: //
tile=R.drawable.picnictile;
break;
case 4: //
tile=R.drawable.picnictile;
break;
case 5: //
tile=R.drawable.face;
break;
default:
Log.e("gettile", "failed!!!!!!!!!! = " + animalclass);
tile=R.drawable.rainbowtile;
break;
}
Log.e("gettile", "returning = " + tile);
return tile;
}
Add break; keyword after each case, otherwise switch will execute default statement also and result will be unexpected.
Related
I want to change my app's background randomly. It's supposed to have a random image from its own folder as background. It works perfectly with some devices but with some of them doesn't. I wonder what could cause this inconsistency. Here is my random background generator class:
public class arkaplanGenerator {
public static int arkaplan(){
int sonucArkaplan = 0;
Random r = new Random();
int sayi = r.nextInt(11);
switch (sayi){
case 0:
sonucArkaplan = R.drawable.bavaria;
break;
case 1:
sonucArkaplan = R.drawable.gorges;
break;
case 2:
sonucArkaplan = R.drawable.more;
break;
case 3:
sonucArkaplan = R.drawable.mountains;
break;
case 4:
sonucArkaplan = R.drawable.pisa;
break;
case 5:
sonucArkaplan = R.drawable.sea;
break;
case 6:
sonucArkaplan = R.drawable.sunset;
break;
case 7:
sonucArkaplan = R.drawable.sunset2;
break;
case 8:
sonucArkaplan = R.drawable.wai;
break;
case 9:
sonucArkaplan = R.drawable.water;
break;
case 10:
sonucArkaplan = R.drawable.waterfall;
break;
}
return sonucArkaplan;
}
And here is how i use it in my main activity:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.activity_main);
linearLayout.setBackground(getResources().getDrawable(arkaplanGenerator.arkaplan()));
Those images are in drawable folder.Thanks in advance.
The method getDrawable from the Resources class has been deprecated. You should use the one from ContextCompat:
linearLayout.setBackground(ContextCompat.getDrawable(getApplicationContext(),arkaplanGenerator.arkaplan()));
So this is what I am doing.The below statement is the last line of an int return type method.
return ContextCompat.getColor(getContext(),magColor);
The error says that i have passed the wrong first argument.
Precisely:
Wrong 1st argument type. Found: 'java.security.AccessControlContext',
required: 'android.content.Context'
Entire Method:
public int getMagnitudeColor(double mag) {
int magColor;
int mag1=(int) mag;
switch (mag1) {
case 1:
magColor = R.color.magnitude1;
break;
case 2:
magColor = R.color.magnitude2;
break;
case 3:
magColor = R.color.magnitude3;
break;
case 4:
magColor = R.color.magnitude4;
;
break;
case 5:
magColor = R.color.magnitude5;
break;
case 6:
magColor = R.color.magnitude6;
break;
case 7:
magColor = R.color.magnitude7;
break;
case 8:
magColor = R.color.magnitude8;
break;
case 9:
magColor = R.color.magnitude9;
break;
default:
magColor = R.color.magnitude10plus;
break;
}
return ContextCompat.getColor(getContext(),magColor);
}
This is because your getContext() in:
return ContextCompat.getColor(getContext(),magColor);
is not android.content.Context.
You need to use context from Activity, Service, Application and View.
If you use custom class, you need to pass the context to the class.
I did this and it worked , 1 single line
view.listViewPalabrasLinearLayout.setBackgroundColor(ContextCompat.getColor(view.context,color))
where "listViewPalabrasLinearLayout" is the id of my layout and "color" is the variable I specify the color
Alright, I'm trying to succinct some code in my app. It works fine, I'm just a bit OCD and want to keep improving performance.
Here's what the code in question looks like now:
switch(ressound){
case R.id.button40:
ressound = R.raw.sound40;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote40));
break;
}
switch(ressound){
case R.id.button900:
ressound = R.raw.sound900;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote900));
break;
}
switch(ressound){
case R.id.button901:
ressound = R.raw.sound901;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote901));
break;
}
It's a soundboard app, and this is regarding the save as feature in it. Is there any way to succinct these multiple statements (some screens have 40+ sounds)? Using a loop looks like an obvious choice, however after looking around, the case statement apparently has to be static and not a variable.
EDIT: Forgot to include the actual function header:
public boolean function1(int ressound){
String soundname = "";
int quoteval=0;
switch(ressound){
case R.id.button40:
ressound = R.raw.sound40;
quoteval =R.string.quote40;
break;
case R.id.button900:
ressound = R.raw.sound900;
quoteval =R.string.quote900;
break;
case R.id.button901:
ressound = R.raw.sound901;
quoteval =R.string.quote901;
break;
}
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(quoteval));
There is a way you can use a loop and eliminate the switch-case statement. Use the Resources#getIdentifier method. Assuming you name your resources with sequential numbers ("sound_filename_1", "sound_filename_2", etc) you can write some code like this:
private static ArrayList<Integer> findSoundResourceIds(Resources res) {
ArrayList<Integer> resIds = new ArrayList<Integer>();
int i = 1;
do {
int resId = res.getIdentifier("sound_filename_"+i, "raw", getPackage().getName());
if (resId == 0) {
break;
}
resIds.add(resId);
i++;
} while (true);
return resIds;
}
public Cursor query(Uri paramUri, String[] paramArrayOfString1, String paramString1,String[] paramArrayOfString2, String paramString2)
{
SQLiteQueryBuilder localSQLiteQueryBuilder = new SQLiteQueryBuilder();
if (paramUri.getPathSegments().size() == 1);
for (StringBuilder localStringBuilder = null; ; localStringBuilder = new StringBuilder(100))
switch (sURIMatcher.match(paramUri))
{
case 0:
case 1:
case 2:
case 3:
default:
throw new IllegalArgumentException("Unknown URI " + paramUri);
}
localSQLiteQueryBuilder.setTables("category");//unreachable code
while (true)
{
Cursor localCursor = localSQLiteQueryBuilder.query(mOpenHelper.getReadableDatabase(), paramArrayOfString1, paramString1, paramArrayOfString2, null, null, paramString2);
localCursor.setNotificationUri(contentResolver, paramUri);
return localCursor;
localSQLiteQueryBuilder.setTables("shop,category");
localSQLiteQueryBuilder.appendWhere("shop_category_id=category._id");
continue;
localSQLiteQueryBuilder.setTables("shop,category");
StringBuilder localStringBuilder;
localStringBuilder.append("shop_category_id=category._id");
localStringBuilder.append(" AND ");
localStringBuilder.append("_id");
localStringBuilder.append('=');
localStringBuilder.append((String)paramUri.getPathSegments().get(1));
localSQLiteQueryBuilder.appendWhere(localStringBuilder.toString());
continue;
localSQLiteQueryBuilder.setTables("shop,category");
localSQLiteQueryBuilder.setDistinct(true);
localStringBuilder.append("shop_category_id=category._id");
localStringBuilder.append(" AND ");
localStringBuilder.append("shop_category_id");
localStringBuilder.append('=');
localStringBuilder.append((String)paramUri.getPathSegments().get(1));
localSQLiteQueryBuilder.appendWhere(localStringBuilder.toString());
paramString2 = "shop._id";
}
}
I get unreachable code error after switch statement and I can't figure it out how to solve it.I tried to delete that line but if I do I get a lot of errors.My code is above.Can anyone help me?Thanks in advance.
The code is really unreachable:
All the cases are fall-through (they have no break statement, so all cases after a match will execute) and end in the default case which throws an Exception. This means the code after throwing the Exception will never be executed.
Perhaps what you meant to do was rahter sth like this:
switch (sURIMatcher.match(paramUri)){
case 0:
// do something
break;
case 1:
// do something
break;
case 2:
// do something
break;
case 3:
// do something
break;
default:
throw new IllegalArgumentException("Unknown URI " + paramUri);
}
I guess it is due to your bad switch...you must use break;
switch (sURIMatcher.match(paramUri))
{
case 0:
//your code
break;
case 1:
//your code
break;
case 2:
//your code
break;
case 3:
//your code
break;
default:
//your code
break;
}
TelephonyManager.getNetworkType() returns one of the constant values.
It appears that the constant values have an integer order, by possible bearer link speed.
I know using constant values used in the following manner is generally bad,
however could one use this to determine a basic cutoff for application functionality and have it work between API levels? (in API-v1 there was nothing above 0x03)
if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_EDGE )
{
return "3G! party on!";
}
else if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_UNKNOWN )
{
return "2G, OK. just don't go nuts!";
}
else
{
return "No data sorry"
}
You cannot assume they are in order because they are not. For example, LTE is 13 while HSPAP (HSPA+) is 15. Those are not in order. I wrote a "speed ranking" piece of code, which assignes each network type its own speed rank
public static int getNetTypeSpeedRank(int t) {
switch (t) {
case -1:
t = -1;
case ContextManager.MDM_NETWORK_TYPE_UNKNOWN:
t = 0;
break;
case ContextManager.MDM_NETWORK_TYPE_IDEN:
t = 1;
break;
case ContextManager.MDM_NETWORK_TYPE_GPRS:
t = 2;
break;
case ContextManager.MDM_NETWORK_TYPE_EDGE:
t = 3;
break;
case ContextManager.MDM_NETWORK_TYPE_UMTS:
t = 4;
break;
case ContextManager.MDM_NETWORK_TYPE_CDMA:
t = 5;
break;
case ContextManager.MDM_NETWORK_TYPE_1xRTT:
t = 6;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_0:
t = 7;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_A:
t = 8;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_B:
t = 9;
break;
case ContextManager.MDM_NETWORK_TYPE_HSDPA:
t = 10;
break;
case ContextManager.MDM_NETWORK_TYPE_HSUPA:
t = 11;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPA:
t = 12;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPAP:
t = 13;
break;
case ContextManager.MDM_NETWORK_TYPE_EHRPD:
t = 14;
break;
case ContextManager.MDM_NETWORK_TYPE_LTE:
t = 15;
break;
default:
t = 16;
}
return t;
}
I really wouldn't count on that behavior.