0

I have a problem with my Android app. I try to get a variable from another class and use it as a condition for an if statement this is the code that i use to get the variable:

To send the variable (is a string):

Intent intent = new Intent(context, Example1.class);
intent.putExtra("categ", result);  

To receive the variable:

final String[] pos_categ ;
pos_categ = intent1.getExtras().getStringArray("categ");

Here I declare the 3 variables that I want to change in the if statement:

    String[] title = null;
    String[] eng = null;
    final String[] dan = null; 

Those are the strings from where I take data for the title, eng and dan variables:

String [] lessons_titles = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","30","40","50","60","70","80","90"};
String [] eng_version = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
String[] dan_version = {"En","To","Tre","Fire","Fem","Seks","Syv","Otte","Ni","Ti","Elleve","Tolv","Tretten","Fjorten","Femten","Seksten","Sytten","Atten","Nitten","Tyve","Tredive","Fyrre","Halvtreds","Tres","Halvfjerds","Firs","Halvfems"};

String [] lessons_titles2 = {"Hej, jeg hedder Khalid","Hvad hedder du?","Jeg hedder Ellen"};
String [] eng_version2 = {"Hej, jeg hedder Khalid","Hvad hedder du?","Jeg hedder Ellen"};
String[] dan_version2 = {"Hej, jeg hedder Khalid","Hvad hedder du?","Jeg hedder Ellen"};

String [] lessons_titles3 = {"Hvor kommer du fra?","Jeg kommer fra Australien.","Hvor kommer du fra?","Jeg kommer dra Moroko."};
String [] eng_version3 = {"Hvor kommer du fra?","Jeg kommer fra Australien.","Hvor kommer du fra?","Jeg kommer dra Moroko."};
String[] dan_version3 = {"Hvor kommer du fra?","Jeg kommer fra Australien.","Hvor kommer du fra?","Jeg kommer dra Moroko."};

String [] lessons_titles4 = {"Hvad sprog taler du?","Jeg taler engelsk.","Hvad med dig?","Jeg taler fransk og arabisk."};
String [] eng_version4 = {"Hvad sprog taler du?","Jeg taler engelsk.","Hvad med dig?","Jeg taler fransk og arabisk."};
String[] dan_version4 = {"Hvad sprog taler du?","Jeg taler engelsk.","Hvad med dig?","Jeg taler fransk og arabisk."};

String [] lessons_titles5 = {"Hvor gammel er du?","Jeg er 22."};
String [] eng_version5 = {"Hvor gammel er du?","Jeg er 22."};
String[] dan_version5 = {"Hvor gammel er du?","Jeg er 22."};

String [] lessons_titles6 = {"Hvor bor du?","Jeg bor i Vestergade.","Hvor i Vestergade?","I nummer 38."};
String [] eng_version6 = {"Hvor bor du?","Jeg bor i Vestergade.","Hvor i Vestergade?","I nummer 38."};
String[] dan_version6 = {"Hvor bor du?","Jeg bor i Vestergade.","Hvor i Vestergade?","I nummer 38."};

And here I use an if statement to change the variables according to the value of "pos_categ" and I try to compare the first value of the pos_categ with the first value of some strings that I already have:

    if (pos_categ[0].equals(lessons_titles1[position[0])){
        title[0] = lessons_titles[position[0]];
        eng[0] = eng_version[position[0]];
        dan[0] = dan_version[position[0]].toLowerCase();

    }
    else if (pos_categ[0].equals(lessons_titles2[position[0])){
        title[0] = lessons_titles2[position[0]];
        eng[0] = eng_version2[position[0]];
        dan[0] = dan_version2[position[0]].toLowerCase();

    }
    else if (pos_categ[0].equals(lessons_titles3[position[0])){
        title[0] = lessons_titles3[position[0]];
        eng[0] = eng_version3[position[0]];
        dan[0] = dan_version3[position[0]].toLowerCase();

    }
    else if (pos_categ[0].equals(lessons_titles4[position[0])){
        title[0] = lessons_titles4[position[0]];
        eng[0] = eng_version4[position[0]];
        dan[0] = dan_version4[position[0]].toLowerCase();

    }
    else if (pos_categ[0].equals(lessons_titles5[position[0])){
        title[0] = lessons_titles5[position[0]];
        eng[0] = eng_version5[position[0]];
        dan[0] = dan_version5[position[0]].toLowerCase();

    }
    else if (pos_categ[0].equals(lessons_titles6[position[0])){
        title[0] = lessons_titles6[position[0]];
        eng[0] = eng_version6[position[0]];
        dan[0] = dan_version6[position[0]].toLowerCase();

    }

    title_view.setText(title[0]);
    eng_view.setText(eng[0]);
    dan_view.setText(dan[0]);

The problem is that when I open the app I should get the right informations according to the variable pos_categ. Instead I get this error:

Unfortunately, test has stopped working.
Blame the Split Screen.
Victor Mihaita
  • 106
  • 1
  • 2
  • 10

1 Answers1

0

A final variable can only be initialized once. You are declaring "final String[] pos_categ" which means you cannot change the value. String[] also means an array of String in java.

My understanding is that you want to pass a single string and evaluate that, if so you [] for pos_categ variable.

Alternatively if you do want to pass an array between activities in android you should do the following to do it:

Passing string array between android activities.

I don't know if this will solve your problem however because I don't see where "Unfortunately, test has stopped working. Blame the Split Screen." Is coming from.

EDIT:

To solve your if problem you need to find out the value of result and postion and then see if they are what they should be. You can use an toast, or use the eclipse debugger (if you are using eclipse) to find out.

But the if's are not the real problem here anyway its what your comparing. You are using STRING ARRAY's ("String[] title") when you should be using STRINGS (like this: "String title"). docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

EDIT 2

int position = getIntent().getExtras().getInt("position");   

String[] pos_categ ;
pos_categ = intent1.getExtras().getString("categ");    

--

String title = "";
String eng = "";
String dan = ""; 

--

if (pos_categ.equals(lessons_titles1[position)){
    title = lessons_titles[position];
    eng = eng_version[position];
    dan = dan_version[position].toLowerCase();
} 
//else if....
Community
  • 1
  • 1
jwv
  • 475
  • 4
  • 21
  • I check if I get the right string from the other class and I found out that is ok. The problem is with the if statement or with the 3 variables that I've declared.. First I made them null that i change their value in the if statement. Is that ok? – Victor Mihaita Apr 02 '14 at 18:02
  • What is position? Can I see the declaration – jwv Apr 02 '14 at 18:10
  • is another variable that i get from another class. – Victor Mihaita Apr 02 '14 at 18:12
  • final int[] position = new int[1]; position[0] = getIntent().getExtras().getInt("position"); – Victor Mihaita Apr 02 '14 at 18:12
  • I've made some changes to the code and now I don't get any error but it looks like the if statement doesn't have any effect on the variables... they remain null. – Victor Mihaita Apr 02 '14 at 18:13
  • I've declare them in the class as: String title; String eng ; String dan ; and then i just want to chenge their value in the if statement but nothing happens: final String title = lessons_titles[position[0]]; final String eng = eng_version[position[0]]; final String dan = dan_version[position[0]].toLowerCase(); – Victor Mihaita Apr 02 '14 at 18:15
  • The variables remain null because none of the if's are true. – jwv Apr 02 '14 at 18:18
  • To solve your if problem you need to find out the value of result and postion and then see if they are what they should be. You can use an toast, or use the eclipse debugger (if you are using eclipse) to find out. But the if's are not the real problem here anyway its what your comparing. You are using STRING ARRAY's ("String[] title") when you should be using STRINGS (like this: "String title"). http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – jwv Apr 02 '14 at 18:27
  • I've checked and I found out that the problem is from the variable position. But I really don't know why. – Victor Mihaita Apr 02 '14 at 19:42
  • here I send the variable position: public View getView(final int position, View convertView, ViewGroup parent) { .... Intent intent = new Intent(context, Example1.class); intent.putExtra("position", position); and i get it with this..: final int[] position = new int[1]; position[0] = getIntent().getExtras().getInt("position"); i get it as an integer because it has to be final and also I have to be able to change it's value.. is ok ? – Victor Mihaita Apr 02 '14 at 19:45
  • What is the value of position? – jwv Apr 02 '14 at 22:00
  • And why not use the following?: final int position = getIntent().getExtras().getInt("position"); – jwv Apr 02 '14 at 22:03
  • \I've tried like that but when i try to use it for example when i press a button.. I get errors. With my version I don't get.. Finally I figure it out and I resolved my problems. Thanks:) – Victor Mihaita Apr 03 '14 at 21:18