I can't seem to get my radiogroups to populate based on the below code. Once my cursor runs against the database I get the below output:
Cursor data = db.getReadableDatabase().query(db.myTable, myColumns, db.USERID + "= ?",
new String[]{String.valueOf(user1)}, null, null, null);
data.moveToPosition(position);
checkx = data.getInt(data.getColumnIndex(db.VALUE));
Log.d("checkedid", "checkedid: " + checkx);
1
1
1
1
1
Based on this I need the first radiobutton to be checked each time. Here is my current switch statement, but nothing gets checked.
if(checkx > 0) {
switch (checkx) {
case 1:
group.check(R.id.a1);
break;
case 2:
group.check(R.id.a2);
break;
case 3:
group.check(R.id.a3);
break;
case 4:
group.check(R.id.a4);
break;
}
}
programmatically, you can use the setChecked method defined in the checkable interface:
if(checkx > 0) {
switch (checkx) {
case 1:
RadioButton b1 = (RadioButton) findViewById(R.id.option1);
b1.setChecked(true);
break;
case 2:
RadioButton b2 = (RadioButton) findViewById(R.id.option2);
b2.setChecked(true);
break;
--------- etc------------
}
Related
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
Why is the code breaking?
Intro: trying to make a "Choose Your Own Adventure" game.
Layout is composed by:
bPage = button saying what page the user is on. Click to manually change page (not implemented yet)
bOne: go through first path
bTwo: go through second path
tvPageTitle: page title, at the top
mainText: main text body (story explained here)
there's also a splash screen, which works perfectly, but when you click on "start", it loads this layout, which hangs (possible infinite loop)
Can someone tell me why? Tried to put as many comments as I could, so it should be self explanatory
Please ask as many questions as you want :)
package com.assignement.cyoa;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Remember extends Activity {
// Declaring variables
TextView mainText;
int page = 1;
MediaPlayer pageflip;
Boolean gameOver = false;
int currentPage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
// Enable manual scrolling text
mainText = (TextView) findViewById(R.id.mainText);
mainText.setMovementMethod(new ScrollingMovementMethod());
// Enable sound when choosing path
pageflip = MediaPlayer.create(Remember.this, R.raw.pageflip);
//TODO: customise story to insert player name. Remember to import method
//EditText userName = LoginActivity.userName;
// Link XML Views with Java
final TextView mainText = (TextView) findViewById(R.id.mainText);
final TextView tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
final Button bOne = (Button) findViewById(R.id.bOne);
final Button bTwo = (Button) findViewById(R.id.bTwo);
final Button bPage = (Button) findViewById(R.id.bPage);
// Array creation
final String[] storyText = new String[22];
final String[] pageTitle = new String[22];
final CharSequence[] pathA = new CharSequence[22];
final CharSequence[] pathB = new CharSequence[22];
///// MAIN TEXT AND POSSIBLE PATHS ARRAYS \\\\\
// Page 0 - Error
storyText[0] = "Path broken";
pageTitle[0] = "Path broken";
pathA[0]= "Path broken";
pathB[0]= "Path broken";
// Page 1
storyText[1] = "\tYou lift open your eyes. All you can see is a bright light. You get up and take a look around you. You are in a very tight room, with no windows and one door. Everything is painted in white. Beside the door is a small walkie talkie which looks brand new.\n\tYou begin to think, where are you? Who are you? You steady yourself and stumble towards the door. You tightly grab the knob and begin to twist it open. It's locked. Damn. You take another spin around the place. You don't know where you are, you don't know who you are, and you are trapped inside this small room. You pace slowly around the room with your mind thinking intensely on what to do.\n\tYou pick up the walkie talkie. You press the button for the microphone and speak.\n\t''Hello? Is anyone there?''\n\tThe room remains silent until a voice springs out of the device.\n\t''Ahh yes, hello!'' The voice is very high pitched, seemingly happy, yet, sinister. ''Do you remember who you are?''\n\tYou think once more, trying to remember who you are, but nothing manages to spring up.\n\t''Hello? Do you remember-''\n\t''No,'' you answer unhappily. ''I don't''\n\tSilence fills the room once more, but the voice comes back.\n\t''Good, this should make things much more interesting.''\n\tYou don't know whether you should be scared or happy. Questions pop up in your mind.";
pageTitle[1] = "Your New Home";
pathA[1]= "You brush it off and go anyway";
pathB[1]= "Decide it's not worth it and leave";
// Page 2
storyText[2] = "Test";
pageTitle[2] = "Not worth it";
pathA[2]= "Go back";
pathB[2]= "";
// Page 3
storyText[3] = "Test";
pageTitle[3] = "You brush it off";
pathA[3]= "Go upstairs and explore the mysterious shadow";
pathB[3]= "Go to the kitchen to start unpacking";
// Page 4
storyText[4] = "Test";
pageTitle[4] = "You go upstairs";
pathA[4]= "Check it out";
pathB[4]= "Go to the kitchen to start unpacking";
// Page 5 - End
storyText[5] = "Test";
pageTitle[5] = "Checking it out";
pathA[5]= "Try Again";
pathB[5]= "";
// Page 6
storyText[6] = "Test";
pageTitle[6] = "The Kitchen";
pathA[6]= "It's probably just a cat, continue unpacking";
pathB[6]= "Check it out";
// Page 7
storyText[7] = "Test";
pageTitle[7] = "Unpacking";
pathA[7]= "Check out the basement";
pathB[7]= "";
// Page 8
storyText[8] = "Test";
pageTitle[8] = "Checking it out";
pathA[8]= "Check out the basement";
pathB[8]= "";
// Page 9
storyText[9] = "Test";
pageTitle[9] = "The Basement";
pathA[9]= "Forget the light! Get out of here!";
pathB[9]= "Turn the light on anyways";
// Page 10 - End
storyText[10] = "Test";
pageTitle[10] = "Leaving";
pathA[10]= "Try Again";
pathB[10]= "";
// Page 11
storyText[11] = "Test";
pageTitle[11] = "Lights on";
pathA[11]= "Chase the mysterious shadow";
pathB[11]= "Scream and run upstairs";
// Page 12
storyText[12] = "Test";
pageTitle[12] = "The Mysterious Shadow";
pathA[12]= "News report";
pathB[12]= "";
// Page 13 - END
storyText[13] = "Test";
pageTitle[13] = "News Report";
pathA[13]= "Try Again";
pathB[13]= "";
// Page 14
storyText[14] = "Test";
pageTitle[14] = "Upstairs";
pathA[14]= "Just stay where you are";
pathB[14]= "Answer the door";
// Page 15 - End
storyText[15] = "Test";
pageTitle[15] = "Stay where you are";
pathA[15]= "Try Again";
pathB[15]= "";
// Page 16
storyText[16] = "Test";
pageTitle[16] = "The Door";
pathA[16]= "Go to the Attic";
pathB[16]= "";
// Page 17
storyText[17] = "Test";
pageTitle[17] = "The Attic";
pathA[17]= "Leave it alone";
pathB[17]= "Play with the Ouija Board";
// Page 18
storyText[18] = "Test";
pageTitle[18] = "Left Alone";
pathA[18]= "Jump out of the window";
pathB[18]= "Accept your fate";
// Page 19
storyText[19] = "Test";
pageTitle[19] = "The Ouija Board";
pathA[19]= "Jump out of the window";
pathB[19]= "Accept your fate";
// Page 20 - End
storyText[20] = "Test";
pageTitle[20] = "Out the Window";
pathA[20]= "Try Again";
pathB[20]= "";
// Page 21 - End
storyText[21] = "Test";
pageTitle[21] = "Fate Accepted";
pathA[21]= "Try Again";
pathB[21]= "";
// Loop until Game Over
do {
// Populate fields and play page flip sound
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
/*
TODO: Enable debug. Manually change page when clicking on the page button
bPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
???
}
});
*/
// Operate first path
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
case 4: page = 5;
break;
case 5: page = 1;
break;
case 6: page = 7;
break;
case 7: page = 9;
break;
case 8: page = 9;
break;
case 9: page = 10;
break;
case 10: page = 1;
break;
case 11: page = 12;
break;
case 12: page = 13;
break;
case 13: page = 1;
break;
case 14: page = 15;
break;
case 15: page = 1;
break;
case 16: page = 17;
break;
case 17: page = 18;
break;
case 18: page = 20;
break;
case 19: page = 20;
break;
case 20: page = 1;
break;
case 21: page = 1;
break;
default: page = 0;
}
}
});
// Operate second path
bTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 2;
break;
case 2: page = 0;
break;
case 3: page = 6;
break;
case 4: page = 6;
break;
case 5: page = 0;
break;
case 6: page = 8;
break;
case 7: page = 0;
break;
case 8: page = 0;
break;
case 9: page = 11;
break;
case 10: page = 0;
break;
case 11: page = 14;
break;
case 12: page = 0;
break;
case 13: page = 0;
break;
case 14: page = 16;
break;
case 15: page = 0;
break;
case 16: page = 0;
break;
case 17: page = 19;
break;
case 18: page = 21;
break;
case 19: page = 21;
break;
case 20: page = 0;
break;
case 21: page = 0;
break;
default: page = 0;
}
}
});
} while (gameOver == false);
//TODO: Game Over screen
}
}
EDIT2: adopted new suggestion, made according changes. now layout loads, but clicking a button makes the app crash
package com.assignement.cyoa;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Remember extends Activity {
// Declaring variables
int page;
int currentPage;
MediaPlayer pageflip;
TextView mainText;
TextView tvPageTitle;
Button bOne;
Button bTwo;
Button bPage;
// Array creation
String[] storyText = new String[22];
String[] pageTitle = new String[22];
CharSequence[] pathA = new CharSequence[22];
CharSequence[] pathB = new CharSequence[22];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
// Link XML Views with Java
TextView mainText = (TextView) findViewById(R.id.mainText);
TextView tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
Button bOne = (Button) findViewById(R.id.bOne);
Button bTwo = (Button) findViewById(R.id.bTwo);
Button bPage = (Button) findViewById(R.id.bPage);
// Enable manual scrolling text
mainText = (TextView) findViewById(R.id.mainText);
mainText.setMovementMethod(new ScrollingMovementMethod());
// Enable sound when choosing path
pageflip = MediaPlayer.create(Remember.this, R.raw.pageflip);
///// MAIN TEXT AND POSSIBLE PATHS ARRAYS \\\\\
// Page 0 - Error
storyText[0] = "Path broken";
pageTitle[0] = "Path broken";
pathA[0]= "Path broken";
pathB[0]= "Path broken";
// Page 1
storyText[1] = "\tYou lift open your eyes. All you can see is a bright light. You get up and take a look around you. You are in a very tight room, with no windows and one door. Everything is painted in white. Beside the door is a small walkie talkie which looks brand new.\n\tYou begin to think, where are you? Who are you? You steady yourself and stumble towards the door. You tightly grab the knob and begin to twist it open. It's locked. Damn. You take another spin around the place. You don't know where you are, you don't know who you are, and you are trapped inside this small room. You pace slowly around the room with your mind thinking intensely on what to do.\n\tYou pick up the walkie talkie. You press the button for the microphone and speak.\n\t''Hello? Is anyone there?''\n\tThe room remains silent until a voice springs out of the device.\n\t''Ahh yes, hello!'' The voice is very high pitched, seemingly happy, yet, sinister. ''Do you remember who you are?''\n\tYou think once more, trying to remember who you are, but nothing manages to spring up.\n\t''Hello? Do you remember-''\n\t''No,'' you answer unhappily. ''I don't''\n\tSilence fills the room once more, but the voice comes back.\n\t''Good, this should make things much more interesting.''\n\tYou don't know whether you should be scared or happy. Questions pop up in your mind.";
pageTitle[1] = "Your New Home";
pathA[1]= "You brush it off and go anyway";
pathB[1]= "Decide it's not worth it and leave";
// Page 2
storyText[2] = "Test";
pageTitle[2] = "Not worth it";
pathA[2]= "Go back";
pathB[2]= "";
// Page 3
storyText[3] = "Test";
pageTitle[3] = "You brush it off";
pathA[3]= "Go upstairs and explore the mysterious shadow";
pathB[3]= "Go to the kitchen to start unpacking";
// Page 4
storyText[4] = "Test";
pageTitle[4] = "You go upstairs";
pathA[4]= "Check it out";
pathB[4]= "Go to the kitchen to start unpacking";
// Page 5 - End
storyText[5] = "Test";
pageTitle[5] = "Checking it out";
pathA[5]= "Try Again";
pathB[5]= "";
// Page 6
storyText[6] = "Test";
pageTitle[6] = "The Kitchen";
pathA[6]= "It's probably just a cat, continue unpacking";
pathB[6]= "Check it out";
// Page 7
storyText[7] = "Test";
pageTitle[7] = "Unpacking";
pathA[7]= "Check out the basement";
pathB[7]= "";
// Page 8
storyText[8] = "Test";
pageTitle[8] = "Checking it out";
pathA[8]= "Check out the basement";
pathB[8]= "";
// Page 9
storyText[9] = "Test";
pageTitle[9] = "The Basement";
pathA[9]= "Forget the light! Get out of here!";
pathB[9]= "Turn the light on anyways";
// Page 10 - End
storyText[10] = "Test";
pageTitle[10] = "Leaving";
pathA[10]= "Try Again";
pathB[10]= "";
// Page 11
storyText[11] = "Test";
pageTitle[11] = "Lights on";
pathA[11]= "Chase the mysterious shadow";
pathB[11]= "Scream and run upstairs";
// Page 12
storyText[12] = "Test";
pageTitle[12] = "The Mysterious Shadow";
pathA[12]= "News report";
pathB[12]= "";
// Page 13 - END
storyText[13] = "Test";
pageTitle[13] = "News Report";
pathA[13]= "Try Again";
pathB[13]= "";
// Page 14
storyText[14] = "Test";
pageTitle[14] = "Upstairs";
pathA[14]= "Just stay where you are";
pathB[14]= "Answer the door";
// Page 15 - End
storyText[15] = "Test";
pageTitle[15] = "Stay where you are";
pathA[15]= "Try Again";
pathB[15]= "";
// Page 16
storyText[16] = "Test";
pageTitle[16] = "The Door";
pathA[16]= "Go to the Attic";
pathB[16]= "";
// Page 17
storyText[17] = "Test";
pageTitle[17] = "The Attic";
pathA[17]= "Leave it alone";
pathB[17]= "Play with the Ouija Board";
// Page 18
storyText[18] = "Test";
pageTitle[18] = "Left Alone";
pathA[18]= "Jump out of the window";
pathB[18]= "Accept your fate";
// Page 19
storyText[19] = "Test";
pageTitle[19] = "The Ouija Board";
pathA[19]= "Jump out of the window";
pathB[19]= "Accept your fate";
// Page 20 - End
storyText[20] = "Test";
pageTitle[20] = "Out the Window";
pathA[20]= "Try Again";
pathB[20]= "";
// Page 21 - End
storyText[21] = "Test";
pageTitle[21] = "Fate Accepted";
pathA[21]= "Try Again";
pathB[21]= "";
/*
TODO: Enable debug. Manually change page when clicking on the page button
bPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
???
}
});
*/
// Populate fields according to first page
page = 1;
currentPage = page;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
// Operate first path
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
case 4: page = 5;
break;
case 5: page = 1;
break;
case 6: page = 7;
break;
case 7: page = 9;
break;
case 8: page = 9;
break;
case 9: page = 10;
break;
case 10: page = 1;
break;
case 11: page = 12;
break;
case 12: page = 13;
break;
case 13: page = 1;
break;
case 14: page = 15;
break;
case 15: page = 1;
break;
case 16: page = 17;
break;
case 17: page = 18;
break;
case 18: page = 20;
break;
case 19: page = 20;
break;
case 20: page = 1;
break;
case 21: page = 1;
break;
default: page = 0;
}
setPage(page);
}
});
// Operate second path
bTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 2;
break;
case 2: page = 0;
break;
case 3: page = 6;
break;
case 4: page = 6;
break;
case 5: page = 0;
break;
case 6: page = 8;
break;
case 7: page = 0;
break;
case 8: page = 0;
break;
case 9: page = 11;
break;
case 10: page = 0;
break;
case 11: page = 14;
break;
case 12: page = 0;
break;
case 13: page = 0;
break;
case 14: page = 16;
break;
case 15: page = 0;
break;
case 16: page = 0;
break;
case 17: page = 19;
break;
case 18: page = 21;
break;
case 19: page = 21;
break;
case 20: page = 0;
break;
case 21: page = 0;
break;
default: page = 0;
}
}
});
}
private void setPage(int curPage)
{
// Populate fields and play page flip sound
page = curPage;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
}
}
Part of your problem is definitely an infinite loop. You have
} while (gameOver == false);
but you never make gameOver == true. But I would reconsider your design. Instead of having this in a loop, it looks like you could have a function that sets the text of your TextViews and do that work and call that from your onClicks passing the function the int that it needs to select the right pages, etc... There is no reason to continually set your OnClickListeners in a loop. It can be done once in onCreate().
Edit
private void setPage(int curPage)
{
page = curPage;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
}
then in your onClick() call the function
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
...
}
setPage(page);
Declare your variables as member variables before (onCreate()) but initialize them in onCreate()
public class Remember extends Activity {
TextView mainText;
TextView tvPageTitle;
Button bOne;
Button bTwo;
Button bPage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
mainText = (TextView) findViewById(R.id.mainText);
tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
bOne = (Button) findViewById(R.id.bOne);
bTwo = (Button) findViewById(R.id.bTwo);
bPage = (Button) findViewById(R.id.bPage);
You can't initialize your Views before you have inflated the layout with setContentView()
You have
TextView mainText = (TextView) findViewById(R.id.mainText);
inside onCreate(). Change that to
mainText = (TextView) findViewById(R.id.mainText);
remove the TextView otherwise you are creating a local variable and your member variables don't have a value so you will get NPE. Do the same with all of your Views.
If i followed your code correctly, it seems you start on page 1, and then just loop.
Your seem to be relying on break; to exit the do-while, but you are just setting the onclicklistener. Are you actually clicking anything? bOne or bTwo i mean. If you dont click them, infinite loop.
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;
}
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.