8 Replies - 7465 Views - Last Post: 12 June 2011 - 05:18 PM

#1 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Adding an non activity class

Posted 11 June 2011 - 04:31 PM

I am working on my first android app and I have got the GUI to work and change screens just fine. Now I am adding the working code to the app and I added a class that does all my calculations for the app. Now the app won't run because one of my activities references that class. I tried to add the class to the android manifest but that still didn't work. Any ideas of what I may have to do to get android to know that this class is suppose to be included in the app. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Adding an non activity class

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Adding an non activity class

Posted 11 June 2011 - 04:35 PM

Did you add it to a different package/project? Assuming it's a non-activity class, you should be able to just add it to a package, import it, and have no problems. Try doing a Clean/Re-build on the project. If that doesn't work, please :code:
Was This Post Helpful? 1
  • +
  • -

#3 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Adding an non activity class

Posted 11 June 2011 - 05:31 PM

Thanks for the help, tried the import and it still doesn't work.

Here is the class I am trying to add.
package com.dewald.brent;

import java.text.NumberFormat;

/* Author: Brent Dewald
 * 04-12-2011
 * 
 * This program will calculate the shim that is needed to set the proper pinion depth when using the pinion
 * depth tool from Randy's Ring and Pinion. The program will need the user to input the reading from the dial 
 * indicator and what differential they are using. The user will also have to specify what attachments they are 
 * using on the tool. 
 * 
 */

public class ShimCalculator {

	//Constants for the three different extensions used on the dial indicator
	private final double SHORT_EXT = 1.9;
	private final double MED_EXT = 2.5;
	private final double LONG_EXT = 3.375;
	
	//Constants for Master Housing Depth for use in calculations
	private final double GM10BOLT72 = 3.693;
        private final double GM10BOLT75 = 3.787;
        private final double GM10BOLT82 = 4.175;
        private final double GM10BOLT82VETTE = 4.125;
        private final double GM10BOLT85 = 4.262;
        private final double GM12BOLTSP = 4.556;
        private final double GM12BOLTLP = 4.670;
        private final double GM12BOLT93 = 4.620;
        private final double CRY10BOLT = 4.124;
        private final double CRY12BOLTSP = 4.350;
        private final double CRY12BOLTLP = 4.344;
        private final double CRY12BOLT9250 = 4.625;
        private final double FORD6625 = 3.500;
        private final double FORD75 = 4.040;
        private final double FORD8 = 4.000;
        private final double FORD88 = 4.415;
        private final double FORD9 = 4.375;
        private final double AMC8BOLT = 4.500;
        private final double DS30 = 3.625;
        private final double DS36 = 3.931;
        private final double DS44 = 4.312;
        private final double DS50 = 4.616;
        private final double DS60 = 5.000;
        private final double DS70 = 5.375;


	//Variables that need user input
	private double dialReading = 0.0;
	private double extUsed = 0.0;
	private double shim = 0.0;
	private double masterHousingDepth = 0.0;
	private double pinionHeight = 0.0;
	private double pinionDepth = 0.0;
	private double measuredPinionDepth = 0.0;
	private double blockHeight = 0.0;
	private double measuredHousingDepth = 0.0;

        //this contructor is for the OEM Calculations
        public ShimCalculator(int diff, double pinionHeight, int ext, double reading){
                this.masterHousingDepth = DiffChoice(diff);
                this.pinionHeight = pinionHeight;
                this.extUsed = ExtChoice(ext);
                this.dialReading = reading;
                this.shim = OEMCalculation();
        }//end constructor
	//this constructor is for the AfterMarket calculations
        public ShimCalculator(int ext, double reading, double scribedDepth){
                this.extUsed = ExtChoice(ext);
                this.dialReading = reading;
                this.pinionDepth = scribedDepth;
                this.shim = AfterMarketCalculation();
        }//end constructor
        //this constructor is for use for the 1-2-3 block method
        public ShimCalculator(int diff, int ext, double reading, int block){
                this.masterHousingDepth = DiffChoice(diff);
                this.extUsed = ExtChoice(ext);
                this.dialReading = reading;
                this.blockHeight = block + 1;
                this.shim = BlockMethod();
        }//end constructor
	private double OEMCalculation(){
		
		pinionDepth = masterHousingDepth - pinionHeight;
		measuredPinionDepth = extUsed - dialReading;
		shim = measuredPinionDepth - pinionDepth;
		
		return Math.abs(shim);
	}//end OEMCalculation
	private double AfterMarketCalculation(){
		
		measuredPinionDepth = extUsed - dialReading;
		shim = measuredPinionDepth - pinionDepth;
		
		return Math.abs(shim);
	}//end AfterMarketClaculation
	private double BlockMethod(){
		
		measuredHousingDepth = extUsed - dialReading + blockHeight;
		shim = masterHousingDepth - measuredHousingDepth;
		
		return Math.abs(shim);
	}//end BlockMethod

        private double ExtChoice(int ext){
                switch (ext){
                    case 0: extUsed = SHORT_EXT;
                    break;
                    case 1: extUsed = MED_EXT;
                    break;
                    case 2: extUsed = LONG_EXT;
                    break;
                    default: break;

                }//end switch
                return extUsed;
        }//end ExtChoice

        private double DiffChoice(int diff){
            switch (diff){
                case 0: masterHousingDepth = GM10BOLT72;
                break;
                case 1: masterHousingDepth = GM10BOLT75;
                break;
                case 2: masterHousingDepth = GM10BOLT82;
                break;
                case 3: masterHousingDepth = GM10BOLT82;
                break;
                case 4: masterHousingDepth = GM10BOLT82VETTE;
                break;
                case 5: masterHousingDepth =  GM10BOLT85;
                break;
                case 6: masterHousingDepth =  GM10BOLT85;
                break;
                case 7: masterHousingDepth = GM12BOLTSP;
                break;
                case 8: masterHousingDepth = GM12BOLTLP;
                break;
                case 9: masterHousingDepth = GM12BOLT93;
                break;
                case 10: masterHousingDepth = CRY10BOLT;
                break;
                case 11: masterHousingDepth = CRY12BOLTSP;
                break;
                case 12: masterHousingDepth = CRY12BOLTLP;
                break;
                case 13: masterHousingDepth = CRY12BOLT9250;
                break;
                case 14: masterHousingDepth = FORD6625;
                break;
                case 15: masterHousingDepth = FORD75;
                break;
                case 16: masterHousingDepth = FORD8;
                break;
                case 17: masterHousingDepth = FORD88;
                break;
                case 18: masterHousingDepth = FORD9;
                break;
                case 19: masterHousingDepth = AMC8BOLT;
                break;
                case 20: masterHousingDepth = DS30;
                break;
                case 21: masterHousingDepth = DS36;
                break;
                case 22: masterHousingDepth = DS44;
                break;
                case 23: masterHousingDepth = DS50;
                break;
                case 24: masterHousingDepth = DS60;
                break;
                case 25: masterHousingDepth = DS70;
                break;
                default: break;
            }//end switch
            return masterHousingDepth;
        }//end DiffChoice

        public String getShim(){
            NumberFormat nf = NumberFormat.getInstance();
            nf.setMaximumFractionDigits(2);
	    nf.setMinimumFractionDigits(2);
            return nf.format(shim);
        }
}//end class


Here is the Activity that is trying to access it.
package com.dewald.brent;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.dewald.brent.ShimCalculator;

public class OEMActivity extends Activity implements onclickListener{
	
	Spinner diffSpinner = (Spinner)findViewById(R.id.diffSpinner);
	Spinner extSpinner = (Spinner)findViewById(R.id.extSpinner);
	EditText et_pinThick = (EditText)findViewById(R.id.et_pinThick);
	EditText et_indRead = (EditText)findViewById(R.id.et_indRead);
	EditText et_shim = (EditText)findViewById(R.id.et_shim);
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oem);
        
        Button backButton = (Button)findViewById(R.id.back);
        backButton.setonclickListener(this);
        Button calculate = (Button)findViewById(R.id.calculate);
        calculate.setonclickListener(this);
	}
	
	@Override
	public void onclick(View v) {
		switch(v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.calculate:
			int diff = diffSpinner.getSelectedItemPosition();
            double ph = Double.parseDouble(et_pinThick.getText().toString());
            int ext = extSpinner.getSelectedItemPosition();
            double reading = Double.parseDouble(et_indRead.getText().toString());
            ShimCalculator sc = new ShimCalculator(diff, ph, ext, reading);
            et_shim.setText(sc.getShim());
			break;
		}
		
	}

}



Here is the manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.dewald.brent"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ShimCalculatorAndroidActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<activity android:label="OEMActivity" android:name="OEMActivity"/>
		<activity android:label="AftmActivity" android:name="AftmActivity"/>
		<activity android:label="BlockActivity" android:name="BlockActivity"/>
		
		<provider
			android:label="ShimCalculator" 
			android:name="ShimCalculator" 
			android:authorities="com.dewald.brent"
		/>
    </application>
</manifest>


In the manifest i am not sure if I am using the right tags for the ShimCalculator class. I know it is not an activity and provider seems to be a fit what it does. I am still learning this so point out anything you think would be of any help.
Was This Post Helpful? 0
  • +
  • -

#4 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Adding an non activity class

Posted 11 June 2011 - 06:05 PM

Is there anyway to know why the emulator is saying the application stopped unexpectedly? I commented out everything I added since it was last working and it still crashes when I click the button to launch the new Activity, but the other buttons for the other Activities work just fine.
Was This Post Helpful? 0
  • +
  • -

#5 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Adding an non activity class

Posted 11 June 2011 - 06:25 PM

Ok I have narrowed it down to the Variable declaration in the OEMActivity class. When I comment out the Spinner and EditText variables i have declared it works and when I uncomment then it crashes. No idea what I have done wrong here. I would appreciate any help. Apparently the import is going to work fine. I guess I should have tested earlier. Thanks for any help.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Adding an non activity class

Posted 11 June 2011 - 09:47 PM

I don't even know how you're running that. It's riddled with syntax errors. Remember that capitalization is significant in Java. A method named onclick() is totally different than one named onclick() or onclick(). Your method and class imports need to be re looked at in terms of capitalization.

Next, you have two errors here:
            ShimCalculator sc = new ShimCalculator(diff, ph, ext, reading);
            et_shim.setText(sc.getShim());



First you make a call to a constructor that doesn't exist. The class you provided has no sets of arguments that match. Secondly, there is no getShim() method to call

Lastly, for runtime errors, use LogCat
Was This Post Helpful? 0
  • +
  • -

#7 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Adding an non activity class

Posted 12 June 2011 - 06:05 AM

The capitalization errors are a result of the cut and paste here. It is correct in my code. That constructor does exist. I am basically a program I have working in Java and trying to get to work as an android app. I am using the working class from that Java program. I know the calculation code works if I give it the right values. The getShim method is at the bottom of the ShimCalculator class. Anyway, I have narrowed the errors down to the variable declaration at teh top of the OEMActivity. I will just keep looking to see what is going wrong hopefully after reading up on LogCat it can give me an idea of what is wrong. Thanks for all the help so far.
Was This Post Helpful? 0
  • +
  • -

#8 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Adding an non activity class

Posted 12 June 2011 - 07:41 AM

Ok, I solved the problem. You can't declare variables that use the findViewById until after the call of the layout in the onCreate method. I was trying to declare variables for use in the class like I would a normal java application. I figured it would be something syntax wise with android. Now it works and runs the calculations. Thanks for all the help Dogstopper. I am just glad I got this solved because I really wanted to finish this app this weekend. Here is what I changed in the OEMActivity class, if you want to compare it to the one above. I also didn't have to add anything to the manifest to make this work.

package com.dewald.brent;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.dewald.brent.ShimCalculator;

public class OEMActivity extends Activity implements onclickListener{
	
	Spinner diffSpinner;
	Spinner extSpinner;
	EditText et_pinThick;
	EditText et_indRead;
	EditText et_shim;
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oem);
        
        Button backButton = (Button)findViewById(R.id.back);
        backButton.setonclickListener(this);
        Button calculate = (Button)findViewById(R.id.calculate);
        calculate.setonclickListener(this);
        
        diffSpinner = (Spinner)findViewById(R.id.diffSpinner);
        extSpinner = (Spinner)findViewById(R.id.extSpinner);
        et_pinThick = (EditText)findViewById(R.id.et_pinThick);
        et_indRead = (EditText)findViewById(R.id.et_indRead);
        et_shim = (EditText)findViewById(R.id.et_shim);
	}
	
	@Override
	public void onclick(View v) {
		switch(v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.calculate:
			int diff = diffSpinner.getSelectedItemPosition();
			double ph = Double.parseDouble(et_pinThick.getText().toString());
            int ext = extSpinner.getSelectedItemPosition();
            double reading = Double.parseDouble(et_indRead.getText().toString());
            ShimCalculator sc = new ShimCalculator(diff, ph, ext, reading);
            et_shim.setText(sc.getShim());
			break;
		}
		
	}

}



*edit* it still made the capitalization errors wonder if it because I am copying from eclipse.

This post has been edited by Apokio: 12 June 2011 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Adding an non activity class

Posted 12 June 2011 - 05:18 PM

Sorry I couldn't help further. The syntax errors that I go and the incomplete files threw the code off. My bad. Glad to hear that you got it working. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1