views:

125

answers:

3

Hello,

here

I uploaded my Android project made in Eclipse. The idea is that i have a service, which computes the sum of two random numbers. But when i press the OK Button, i don't see the result in that edit box... why? What i'm doing wrong? Please help

Thanks!

EDIT: The code:

//service class

 package service;

import java.util.Random;
import com.android.AplicatieSuma;
import android.widget.EditText;
import android.widget.TextView;

public class ServiciuSuma 
{   
    public ServiciuSuma() { }

    public int CalculateSum()
    {
        Random generator=new Random();
        int n=generator.nextInt();
        int m=generator.nextInt();
        return n+m;
    }
}

The Application class:

package com.android;
import android.app.*;
import service.*;

public class ApplicationSum extends Application {
    public ServiciuSuma service = new ServiciuSuma();
}

and the main Activity class:

package com.android;

import com.android.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class Activitate extends Activity  {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  

        View btn_ok = findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(new OnClickListener() 
            {
                public void onClick(View v) 
                {
                    CalculeazaSuma();                   
                }
            });
    }


    private void CalculeazaSuma()
    {
        AplicatieSuma appState = ((AplicatieSuma)this.getApplication());
        EditText txt_amount = (EditText)findViewById(R.id.txt_amount);
        txt_amount.setText(appState.service.CalculateSum());
        //BindData();
    }
}

So that edit text does not show the sum of the random generated numbers, by the service. What's wrong?

Thanks

EDIT:

the manifest xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android"
      android:versionCode="1"
      android:versionName="1.0.1">
      <supports-screens
          android:largeScreens="true"
          android:normalScreens="true"
          android:smallScreens="true"
          android:anyDensity="true" />

    <application android:name="ApplicationSum" android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:label="@string/app_name" android:name=".Activitate">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>
    <uses-sdk android:minSdkVersion="7"  android:targetSdkVersion="7" />
</manifest>
A: 

Have you specified in manifest that you want ApplicationSum to be used as application class?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <application 
        android:name="com.android.ApplicationSum">
        ...
    </application>
</manifest>
Fedor
Yes (i edited the first post)..
qwerty
A: 

Does this work?

public String CalculateSum()
{
    Random generator=new Random();
    int n=generator.nextInt();
    int m=generator.nextInt();

    String sum = Integer.toString(n+m) ;

    Log.v("CalculateSum", "N+M = " + sum);

    return sum;
}
HXCaine
String sum = n+m? cannot convert from int to String..
qwerty
But first, how can ensure that those numbers are really generated, and how can i see them?
qwerty
Check out the edit. If you don't know what the Log line means, search google for "adb logcat"
HXCaine
I still got that error.. "The application .... has stopped unexpectedly. Please try again". And in the Eclipse console i did not see anything regarding to that log..
qwerty
Android devices/emulators don't output to console, you have to use "adb logcat'. Google it and see. It's very useful for tracking down exceptions and logging activity. Essentially, you type "adb logcat" into a command prompt and it's a live feed of log messages
HXCaine
Thanks for responses. Now please tell me which event should i use for the numeric keyboard from android emulator? So when i press any button (from letters, numbers), i should do something... but which event?Thanks
qwerty
This is an entirely different question. Please post it as a new question on the main page. Also, report back as to what happened with the current issue
HXCaine
A: 

This fix will work:

txt_amount.setText(String.valueOf(appState.service.CalculateSum()));

Without that you pass integer and android thinks it's an id of a resource to display. You should really use DDMS to identify such problems.

Fedor