开发者

Addition isn't working in Android

A very strange thing happened. I had an app to calculate points from answers. It worked very well, but since my localization (which has NO impact at all on my counting system of points) they aren't added properly; instead, they are added like a string.

2+4+5 = 245, before it was 11...Any ideas?

Example:

            if(antworten.geo == 0)
            punktzahl += 10;
        else if(antworten.geo == 1)
            punktzahl += 8;
        else if(antworten.geo == 2)
            punktzahl += 4;

Like this I get the answer from a spinner

    public class geo_listener implements OnItemSelectedListener{
    private boolean i = false;
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        if(i == true){
            antworten.geo_str = parent.getSelectedItem().toString();
            antworten.geo = parent.getSelectedItemPosition();
            Toast chosen = Toast.makeText(parent.getContext(), parent.getSelectedItem().toString(), Toast.LENGTH_SHORT);
            chosen.show();}
        else{
            i = true;
            antworten.geo = parent.getSelectedItemPosition();
        }
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

Like this I am defining all those vars and strngs

    public class antworten{
    public static int rennen = 0; // 1 - Rennen 2 - Langsam
    public static String rennen_str;
    public static int next_item = 0;  //1 - Küchenmesser 2 - Pistole 3 - Stift 4 - Baseball-Schläger
    public static String next_item_str;
    public static int family = 0; // 1 - Ja  2 - Nein
    public static String family_str;
    public static int place = 0; // 1 - Straße 2 - Zuhause 3 - Arbeit 4 - Restaurant 5 - Öffentlicher Platz
    public static String place_str;
    public static int geo = 0; // 1 - Berg 2 - Meer 3 - Land
    public static String geo_str;
    public static int virus = 0; // 1 - Luft 2 - Speichel 3 - Blut 4 - Weiß nicht
    public static String virus_str;
    public static int walk = 0; //1 - Küchenmesser 2 - Pistole 3 - Stift 4 - Baseball-Schläger
    public static String walk_str;
    public static int super_m = 0; // 1 - Ja  2 - Nein
    public static String super_m_str;
    public static int apo = 0; // 1 - Ja 2 - Nein
    public static String apo_str;
    public static int death = 0; // 1 - Kopfschuss 2 - Abtrennen 3 - Gegengift 4 - Normale Todesarten
    public static String death_str;
    public static long punktzahl;
    public static String punk_str;
}

and like this I am counting the points

    package com.zombies.survive;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class result_activity extends Activity {
    static Context mContext;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        String a开发者_运维技巧nt_ren = getApplicationContext().getString(R.string.ant1);
        String ant_itm = getApplicationContext().getString(R.string.ant2);
        String ant_fam = getApplicationContext().getString(R.string.ant3);
        String ant_plc = getApplicationContext().getString(R.string.ant4);
        String ant_geo = getApplicationContext().getString(R.string.ant5);
        String ant_vir = getApplicationContext().getString(R.string.ant6);
        String ant_wlk = getApplicationContext().getString(R.string.ant7);
        String ant_sup = getApplicationContext().getString(R.string.ant8);
        String ant_apo = getApplicationContext().getString(R.string.ant9);
        String ant_tot = getApplicationContext().getString(R.string.ant10);
        //Rennen Frage
            if(antworten.rennen == 0)
                antworten.punktzahl += 5;
            else
                antworten.punktzahl += 10;
        //Item Frage
            if(antworten.next_item == 0)
                antworten.punktzahl += 6;
            else if(antworten.next_item == 1)
                antworten.punktzahl += 10;
            else if(antworten.next_item == 2)
                antworten.punktzahl += 2;
            else if(antworten.next_item == 3)
                antworten.punktzahl += 5;
        //Familie Frage
            if(antworten.family == 0)
                antworten.punktzahl += 3;
            else
                antworten.punktzahl += 10;
        //Place Frage
            if(antworten.place == 0)
                antworten.punktzahl += 6;
            else if(antworten.place == 1)
                antworten.punktzahl += 2;
            else if(antworten.place == 2)
                antworten.punktzahl += 8;
            else if(antworten.place == 3)
                antworten.punktzahl += 10;
            else if(antworten.place == 4)
                antworten.punktzahl += 5;
            else if(antworten.place == 5)
                antworten.punktzahl += 6;
            else if(antworten.place == 6)
                antworten.punktzahl += 5;
        //Geo Frage
            if(antworten.geo == 0)
                antworten.punktzahl += 10;
            else if(antworten.geo == 1)
                antworten.punktzahl += 8;
            else if(antworten.geo == 2)
                antworten.punktzahl += 4;
        //Virus Frage
            if(antworten.virus == 0)
                antworten.punktzahl += 2;
            else if(antworten.virus == 1)
                antworten.punktzahl += 4;
            else if(antworten.virus == 2)
                antworten.punktzahl += 10;
            else if(antworten.virus == 3)
                antworten.punktzahl += 1;
        //Walk Frage
            if(antworten.walk == 0)
                antworten.punktzahl += 6;
            else if(antworten.walk == 1)
                antworten.punktzahl += 8;
            else if(antworten.walk == 2)
                antworten.punktzahl += 10;
            else if(antworten.walk == 3)
                antworten.punktzahl += 3;
            else if(antworten.walk == 4)
                antworten.punktzahl += 1;
        //Supermarkt Frage
            if(antworten.super_m == 0)
                antworten.punktzahl += 10;
            else if(antworten.super_m == 1)
                antworten.punktzahl += 5;
       //Apotheken Frage
            if(antworten.apo == 0)
                antworten.punktzahl += 10;
            else if(antworten.apo == 1)
                antworten.punktzahl += 5;
       //Töten Frage
            if(antworten.death == 0)
                antworten.punktzahl += 7;
            else  if(antworten.death == 1)
                antworten.punktzahl += 4;
            else  if(antworten.death == 2)
                antworten.punktzahl += 5;
            else  if(antworten.death == 3)
                antworten.punktzahl += 10; 
        antworten.punk_str = String.valueOf(antworten.punktzahl);
        TextView ergebniss = (TextView)findViewById(R.id.res_txt);
        ergebniss.setMovementMethod(new ScrollingMovementMethod());
        ergebniss.setText(R.string.wenn_eine_zombie_apokalypse_eintreten_w_rde_steht_die_chance_f_r_dich_zu_berleben_bei_ + antworten.punk_str + "%\n\n\n"+ant_ren+ant_itm+ant_plc+ant_fam+ant_geo+ant_vir+ant_wlk+ant_sup+ant_apo+ant_tot);

        Button back_but = (Button)findViewById(R.id.back_but);
        back_but.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(result_activity.this,home.class);
                startActivity(i);

            }
        });
    }
}


If you add the strings "2", "4", and "5", together, you'll get "245", whereas if you add ints, you'll get 11. Your variable "punktzahl" must be holding a string.


Are you sure that the punktzahl-variable you are adding together is the one from class antworten? You're writing antworten.geo, but not antworten.punktzahl.

If you try to look up the variable in the IDE where do you arrive?


You might try using .intValue():

int tempInt1 = 5;
int tempInt2 = 4;
int tempInt3 = tempInt2.intValue() + tempInt1.intValue();

or .add()

int tempInt1 = 5;
int tempInt2 = 4;
int tempInt3 = tempInt2.add(tempInt1);

I don't have a compiler at hand, so I don't know if these will work, but they're something to try.

Also, keep in mind that if somewhere you ever accidentally add a string to an int, the resulting value will be a string! That might be a reason why you might have strings where you did not expect them.


I think that @thoredge is on the right track. Try replacing this line:

antworten.punk_str = String.valueOf(antworten.punktzahl);

With this:

antworten.punk_str = antworten.punktzahl.getClass().getName();

If punktzahl is really a long, you should get a syntax error. Otherwise, you will see what class Java and Android think it really is.

There are two ways I can think of that it could end up being some other type. The first, as @thoredge suggests, is that you aren't actually getting class antworten. Is it possible that you have two different classes with the same name in different packages in your project?

Maybe com.zombies.survive.antworten and com.zombies.antworten?

Is it possible that you're looking at an old copy of antworten? Maybe it didn't get recompiled, and an earlier version actually used a String. Try doing a make-clean/build-all (Project > Clean in Eclipse). Try restarting your IDE. Try deleting the source, or even the project, and checking it out of source control from scratch.

The last thing I can think of is that it could be a Classpath issue. Is there any possibility that you have a jar file in the project with a class named "antworten" that you could be getting instead of the one you are expecting? I have seen some very weird things happen in the past, for instance, when two apps were installed on WebSphere and they used different versions of the same library. There were two different versions of the same class on the classpath. The result was totally mystifying. I had a very unpleasant afternoon helping a co-worker figure it out. So make sure that isn't happening to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜