So guys, in my app, i build a menu, with an expandablelistview, and one of the groups have 4 childs, one that open a facebook page, another a website page, a youtube page and a google+ page. But No matter where i click all of them opens the google+ page, and i dont see why. Here's the code:
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
int pos = childPosition;
//clique em contatos
if(groupPosition == 5){
switch(pos) {
//Clique em emails e contatos
case 0:
Intent i = new Intent(MainActivity.this, Contatos.class);
MainActivity.this.startActivity(i);
}
}
//clique em hiperligacoes
if(groupPosition == 3){
switch(pos) {
//click no facebook
case 0:
Intent browserFace = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/moises.transporte.passageiros?fref=ts"));
startActivity(browserFace);
//click no site
case 1:
Intent browserSite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://moises-transportes.pt/"));
startActivity(browserSite);
//click no youtube
case 2:
Intent browserYoutube = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/channel/UCXeHbISNnc0eLCPnTeolxLg"));
startActivity(browserYoutube);
//click no google+
case 3:
Intent browserGoogle = new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/111005531753993560637/about"));
startActivity(browserGoogle);
}
}
Can you guys help me finding why is this happening ?
break.
Use break after every case statement. Orelse all the cases after the correct one is executed one after the other.
if(groupPosition == 3){
switch(pos) {
//click no facebook
case 0:
Intent browserFace = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/moises.transporte.passageiros?fref=ts"));
startActivity(browserFace);
break;
//click no site
case 1:
Intent browserSite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://moises-transportes.pt/"));
startActivity(browserSite);
break;
//click no youtube
case 2:
Intent browserYoutube = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/channel/UCXeHbISNnc0eLCPnTeolxLg"));
startActivity(browserYoutube);
//click no google+
break;
case 3:
Intent browserGoogle = new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/111005531753993560637/about"));
startActivity(browserGoogle);
break;
}
}
You forgot to break your cases in switch. Write break; before you write the next case for each. Also, add a default case.
Related
I'm making a fragment of language game in which will be a matching of the word and the translation. I need to store the previously clicked button and after clicking on another button compare them, and if they match to make them invisible.
But I'm getting an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.Button.getText()' on a null object reference
On the line String text = buf.getText().toString();
All buttons were defined before.
#Override
public void onClick(View v) {
if (button_prev != 9) {
if (v.getId() != button_prev) {
Button buf = (Button) v.findViewById(button_prev);
String text = buf.getText().toString();
if (book_array.indexOf(text) - book_array.indexOf((String) ((Button) v).getText()) != 0) {
// book_array obj contains word and translating
i--;
v.findViewById(v.getId()).setVisibility(View.INVISIBLE);
v.findViewById(button_prev).setVisibility(View.INVISIBLE);
}
}
}
switch (v.getId()) {
// Storage the previously clicked the button in button_prev
case R.id.button1:
button_prev = Button_1.getId();
// button_prev = R.id.button1;
break;
case R.id.button2:
button_prev = Button_2.getId();
break;
case R.id.button3:
button_prev = Button_3.getId();
break;
case R.id.button4:
button_prev = Button_4.getId();
break;
case R.id.button5:
button_prev = Button_5.getId();
break;
case R.id.button6:
button_prev = Button_6.getId();
break;
case R.id.button7:
button_prev = Button_7.getId();
break;
case R.id.button8:
button_prev = Button_8.getId();
break;
default:
break;
Maybe someone could fix the code? Is there any correct way to store clicked buttons ?
You can declare a POJO for lastclicked view like following to store lastClicked view.
public class LastViewClicked {
private View view;
public View getView() {
return view;
}
public void setView(View view) {
this.view = view;
}
}
Now to when you click on any button, update the view Object inside LastClickedView using setView Method and when you need to know what was the last clicked button, you create a switch statement with all Ids as cases and if it matches you can do your operation.
// This Function gets the last clicked item which has opened up the Alert Dialog for
// selection and Updates its UI...
private void UpdateLastClickedView(int position, ArrayAdapter arrayAdapter) {
View v = lastViewClicked.getView();
// cast according to your views...
AppCompatTextView appCompatTextView = (AppCompatTextView) v;
// do your operation...
switch(appCompatTextView.getId()){
case R.id.tv1:
// hide or whatever you want to do...
break;
case R.id.tv2:
// hide or whatever you want to do...
break;
}
}
Your null pointer error may be the result of fragment recreation, which results in the value of button_prev invalid. And, the initial value of button_prev may be the reason.
findViewById is a time consuming function. The view binding should be processed once, and once only, in onCreate()
It's better if using MVC, MVP or MVVM. User a model to describe your game logic. Focus on the data change, and the UI should be changed with it.
Declare button_prev as field in your activity or fragment and use getter and setter to access it inside onclick.
I am developing an android application and have run into a slight problem. On certain questions when a user clicks either yes or no followed by the next button they need to go to a different page, however my code seems to be incorrect can someone please help.
Thanks in advance.
below is my code:
public void Next_Btn3(View view){
boolean checked = ((RadioButton)view).isChecked();
switch (view.getId()){
case R.id.Yes_3:
if (checked)
new Intent(this, Questionnaire4.class);
startActivity(detailIntent);
break;
case R.id.No_3:
if (checked)
new Intent(this, Questionnaire6.class);
startActivity(detailIntent);
break;
}
}
In your code, you declare an Intent without actually assigning it. You also forgot to use brackets around your if body. Lastly, you have casted the clicked Button as a RadioButton, which should instead be found using findViewById(R.id.radio_id_here) ; Try this:
public void Next_Btn3(View view){
boolean checked = ((RadioButton)view).isChecked(); //MUST FIND RadioButton through ID, not cast clicked Button as a Radio Button
switch (view.getId()){
case R.id.Yes_3:
if (checked) {
Intent detailIntent = new Intent(this, Questionnaire4.class);
startActivity(detailIntent);
}
break;
case R.id.No_3:
if (checked) {
Intent detailIntent = new Intent(this, Questionnaire6.class);
startActivity(detailIntent);
}
break;
}
}
I have 50 Buttons. I use the switch statement as shown below. Each case has the same On-Click code. But I make many changes to the code for each Button, and it's really tedious work and very repetitive to change the code the same way 50 times. My code currently looks like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button1:
if(button_list.get(1).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(1).getText().toString());
is_clicked= (String)button_list.get(1).getTag();
if ("0".equals(is_clicked)){
button_list.get(1).setBackgroundResource(R.drawable.button_pressed);
button_list.get(1).setTag("1");
}else{
button_list.get(1).setBackgroundResource(R.drawable.button_normal);
button_list.get(1).setTag("0");
}
break;
case button2:
if(button_list.get(2).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(2).getText().toString());
is_clicked= (String)button_list.get(2).getTag();
if ("0".equals(is_clicked)){
button_list.get(2).setBackgroundResource(R.drawable.button_pressed);
button_list.get(2).setTag("1");
}else{
button_list.get(2).setBackgroundResource(R.drawable.button_normal);
button_list.get(2).setTag("0");
}
break;
case button3:
if(button_list.get(3).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(3).getText().toString());
is_clicked= (String)button_list.get(3).getTag();
if ("0".equals(is_clicked)){
button_list.get(3).setBackgroundResource(R.drawable.button_pressed);
button_list.get(3).setTag("1");
}else{
button_list.get(3).setBackgroundResource(R.drawable.button_normal);
button_list.get(3).setTag("0");
}
break;
case button4:
if(button_list.get(4).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(4).getText().toString());
is_clicked= (String)button_list.get(4).getTag();
if ("0".equals(is_clicked)){
button_list.get(4).setBackgroundResource(R.drawable.button_pressed);
button_list.get(4).setTag("1");
}else{
button_list.get(4).setBackgroundResource(R.drawable.button_normal);
button_list.get(4).setTag("0");
}
break;
case button5:
if(button_list.get(5).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(5).getText().toString());
is_clicked= (String)button_list.get(5).getTag();
if ("0".equals(is_clicked)){
button_list.get(5).setBackgroundResource(R.drawable.button_pressed);
button_list.get(5).setTag("1");
}else{
button_list.get(5).setBackgroundResource(R.drawable.button_normal);
button_list.get(5).setTag("0");
}
break;
case button6:
if(button_list.get(6).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(6).getText().toString());
is_clicked= (String)button_list.get(6).getTag();
if ("0".equals(is_clicked)){
button_list.get(6).setBackgroundResource(R.drawable.button_pressed);
button_list.get(6).setTag("1");
}else{
button_list.get(6).setBackgroundResource(R.drawable.button_normal);
button_list.get(6).setTag("0");
}
break;
case button7:
if(button_list.get(7).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(7).getText().toString());
is_clicked= (String)button_list.get(7).getTag();
if ("0".equals(is_clicked)){
button_list.get(7).setBackgroundResource(R.drawable.button_pressed);
button_list.get(7).setTag("1");
}else{
button_list.get(7).setBackgroundResource(R.drawable.button_normal);
button_list.get(7).setTag("0");
}
break;
case button8:
if(button_list.get(8).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(8).getText().toString());
is_clicked= (String)button_list.get(8).getTag();
if ("0".equals(is_clicked)){
button_list.get(8).setBackgroundResource(R.drawable.button_pressed);
button_list.get(8).setTag("1");
}else{
button_list.get(8).setBackgroundResource(R.drawable.button_normal);
button_list.get(8).setTag("0");
}
break;
case button9:
if(button_list.get(9).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(9).getText().toString());
is_clicked= (String)button_list.get(9).getTag();
if ("0".equals(is_clicked)){
button_list.get(9).setBackgroundResource(R.drawable.button_pressed);
button_list.get(9).setTag("1");
}else{
button_list.get(9).setBackgroundResource(R.drawable.button_normal);
button_list.get(9).setTag("0");
}
break;
case button10:
if(button_list.get(10).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(10).getText().toString());
is_clicked= (String)button_list.get(10).getTag();
if ("0".equals(is_clicked)){
button_list.get(10).setBackgroundResource(R.drawable.button_pressed);
button_list.get(10).setTag("1");
}else{
button_list.get(10).setBackgroundResource(R.drawable.button_normal);
button_list.get(10).setTag("0");
}
break;
Is there a way I can use a general variable to access each case so if I make a change I won't need to make it 50 times for each button? I thought about doing something like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button[i]
if(button_list.get(i).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(i).getText().toString());
is_clicked= (String)button_list.get(i).getTag();
if ("0".equals(is_clicked)){
button_list.get(i).setBackgroundResource(R.drawable.button_pressed);
button_list.get(i).setTag("1");
}else{
button_list.get(i).setBackgroundResource(R.drawable.button_normal);
button_list.get(i).setTag("0");
}
break;
Would this work? Or is there something else I need to do so I don't need to make 50 changes to my code every time I decide I want to change the OnClick method?
I'm going to assume that ButtonOnClick is the click handler for the buttons. I'm going to further assume that for any i, the contents of button_list.get(i) is the button. If that's right, then the argument v is the same Button object stored in the list. You can then reduce your entire handler to:
public void ButtonOnClick(View v) {
Button btn = (Button) v;
if (btn.getTag().equals("0")) {
enter_txt.setText(enter_txt.getText()+btn.getText().toString());
}
is_clicked= (String) btn.getTag();
if ("0".equals(is_clicked)){
btn.setBackgroundResource(R.drawable.button_pressed);
btn.setTag("1");
}else{
btn.setBackgroundResource(R.drawable.button_normal);
btn.setTag("0");
}
}
As an aside, you might consider using Boolean objects instead of String objects as the button tags.
First you should never copy paste code, try to extract your code in another method.
If you are using a arraylist where you store all your buttons, you can probably use the ArrayList.indexOf(Object o)
So just replace your ButtonOnClick method with this one, you can handle other buttons behavior easily too in the future :
public void ButtonOnClick(View v) {
if(button_list.contains(v))
{
setButtonBackground(button_list.indexOf(v));
}
}
private setButtonBackground(int buttonNumber)
{
Button myButton = (Button) button_list.get(buttonNumber);
if(myButton.getTag().equals("0"))
{
enter_txt.setText(enter_txt.getText()+myButton.getText().toString());
}
is_clicked = (String) myButton.getTag();
if ("0".equals(is_clicked))
{
myButton.setBackgroundResource(R.drawable.button_pressed);
myButton.setTag("1");
}
else
{
myButton.setBackgroundResource(R.drawable.button_normal);
myButton.setTag("0");
}
}
When you are developping instead of copy and paste code, just use the shortcut extract to a method so you will get something like this and will save yourself time.
[...]
case button1:
setButtonBackground(1);
break;
case button2:
setButtonBackground(2);
break;
[...]
Let me know if it works for you
This is my Code for the action bar i use.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.icon:
case R.id.Kur:
Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
startActivity(Doviz);
finish();
return true;
case R.id.Hesap:
Intent Hesap = new Intent(MainActivity.this, Genel.class);
startActivity(Hesap);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
I'm trying to make it so when someone clicks the icon at top ( which is in red circle at picture) it should do the same thing as my "Döviz Kuru" Button at action bar. I have 2 problems 1 is i cant seem to make same Intent work for 2 cases in the menu 2nd is R.id.İcon doesn't reach to icon. I also tried home and the name i give to that .png neither worked.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
startActivity(Doviz);
finish();
return true;
case R.id.Kur:
Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
startActivity(Doviz);
finish();
return true;
case R.id.Hesap:
Intent Hesap = new Intent(MainActivity.this, Genel.class);
startActivity(Hesap);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
it gives an error abaut "Duplicate local variable Doviz". When i try to use the same Intent for 2 cases.
the answer for the making 2 cases with same job is done like this.
case android.R.id.home:
case R.id.Kur:
Intent Doviz = new Intent(Genel.this, MainActivity.class);
startActivity(Doviz);
finish();
return true;
To enable Home Icon of Actionbar...
Read This Document For More Info : Navigate Up to Parent Activity
You should Do following...
write these lines in OnCreate() method of your activity..
ActionBar actn = getActionBar();
actn.setHomeButtonEnabled(true); //set home button clickable...
actn.setDisplayHomeAsUpEnabled(true); //add up indicator with home button..
add this to onOptionsItemSelected()..
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
//this calls Home Icon of Actionbar...
//your code..
return true;
default:
return super.onOptionsItemSelected(item);
}
}
for calling same code for both cases You can write cases like below..
switch (item.getItemId()) {
case android.R.id.home:
case R.id.Kur:
//this code runs for both cases..
//your code..
return true;
default:
return super.onOptionsItemSelected(item);
}
if you want to restart your activity....
you should use this code..this will restart current activity..
Intent intent = getIntent();
finish();
startActivity(intent);
How do I change the android actionbar title and icon
If you want to change it in code call
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
And set the values to whatever you please.
I have recently found myself struggling in taking a user to GPS coordinates directly from my listview. I have not found any examples of this online? First click on the list takes me to a website, i was hoping that the second intent could take the user to google maps and the gps coordinates. As an example, below is the way i have it set up so far. (Also using fragment)
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch(groupPosition) {
case 0:
switch (childPosition) {
case 0:
Intent myWebLink= new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http:/www.blahblah.com"));
startActivity(myWebLink);
break;
//case 1:
Intent *Link to GPS coordinates*
break;//
case 2:
Intent d3= new Intent(v.getContext(),class3.class);
startActivity(d3);
break;
case 3:
Intent d4= new Intent(v.getContext(),class4.class);
startActivity(d4);
break;
Any help or direction would be much appreciated.