0

I am using a simple android code in Eclispe ADT for android to reverse a input string.The reverse string function is written separately and I have exported it as a jar file.Then I added the jar to libs folder of my android project.While building the project the eclipse is not showing amy error,but at the runtime after giving the user input string and clicking the string button the emulator shows the message "Unfortunately 'My First Project' has stopped"

This is my main android activity class

package com.example.myfirstproject;

import Reverse.ReverseSentence;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Menu;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends Activity {
private EditText text1,text2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text1=(EditText)findViewById(R.id.editText1);
    text2=(EditText)findViewById(R.id.editText2);

}

public void onClick(View view) {
    switch (view.getId()) {
    case R.id.button1:
      RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
      RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
      if (text1.getText().length() == 0) {
        Toast.makeText(this, "Please enter a valid number",
            Toast.LENGTH_LONG).show();
        return;
      }

      float inputValue = Float.parseFloat(text1.getText().toString());
      if (celsiusButton.isChecked()) {
        text1.setText(String
            .valueOf(convertFahrenheitToCelsius(inputValue)));
        celsiusButton.setChecked(false);
        fahrenheitButton.setChecked(true);
      } else {
        text1.setText(String
            .valueOf(convertCelsiusToFahrenheit(inputValue)));
        fahrenheitButton.setChecked(false);
        celsiusButton.setChecked(true);
      }
      break;
    case R.id.button2:
        String inputString=text2.getText().toString();
        if(text2.getText().length()==0){
        Toast.makeText(this, "You have enetered blank ", Toast.LENGTH_LONG).show();
        return;
        }

        try{
        //Reverse.ReverseSentence rObj=new Reverse.ReverseSentence();
        ReverseSentence rObj=new ReverseSentence();

        String outputString=new String();
        outputString=rObj.reverseString(inputString);
        text2.setText(outputString);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        break;
    }
  }
// Converts to celsius
  private float convertFahrenheitToCelsius(float fahrenheit) {
    return ((fahrenheit - 32) * 5 / 9);
  }

  // Converts to fahrenheit
  private float convertCelsiusToFahrenheit(float celsius) {
    return ((celsius * 9) / 5) + 32;
  }

  //Reverses a string

  /*public String reverseString(String input){
        String original,reverse="";
        //Scanner scanner=new Scanner(System.in);

        original=input;
        System.out.println("The input string is "+ original);

        int length=original.length();

        for(int i=length-1;i>=0;i--){
            reverse=reverse+original.charAt(i);

            }

        return reverse;     

    }*/

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

The jar file I am using has the following components. 1 META-INF folder, 2 .classpath file , 3 .properties file 4 A folder Reeverse containting two class files Palindrome.class and
ReverseSentence.class

The source code of the two classes are as follows:-

Palindrome.class

package Reverse;

import java.util.*;

public class Palindrome {

public static void main(String args[]){
    System.out.println("Type a string to give input");
    Scanner scanner=new Scanner(System.in);
    String inputString=new String();
    String outputString=new String();
    inputString=scanner.nextLine();
    scanner.close();
    System.out.println("The input String is "+inputString);

    ReverseSentence r=new ReverseSentence();

    outputString=r.reverseString(inputString);

    System.out.println("The output String is "+outputString);
    if(inputString.equalsIgnoreCase(outputString)){
        System.out.println("The string is a palindrome");
    }
    else{
        System.out.println("The string is not a palindrome");
    }
        System.out.println("The program is working fine");

}

/**
 * @param args
 */


}

ReverseSentence class:-

package Reverse;

public class ReverseSentence {

String original,reverse="";
public ReverseSentence(){

}
public ReverseSentence(String input){
    this.original=input;

}
public String reverseString(String input){

    //Scanner scanner=new Scanner(System.in);

    original=input;
    System.out.println("The input string is "+ original);

    int length=original.length();

    for(int i=length-1;i>=0;i--){
        reverse=reverse+original.charAt(i);

        }

    return reverse;     

}



}

I am not getting the solution to fix the problem.I have tried fix the Android preferences -> compiler-> JDK compliance to 1.7 as my java code is built using JDK 1.7. But I am still getting the app crash.

I am using Eclipse Juno EE as java development IDE and Eclipse java ADT as Android IDE.

This is a simple program to check the usability of jar file in android,but I am getting noClassDefFound error.Probably I am missing something and its a sily problem,still I am stuck at this.

Please help....

Samrat
  • 161
  • 2
  • 16

2 Answers2

0

try this way...

1.Right click on your project and go to Properties.
2.go to java build path..//which is on the 5th position on left side
3.go to Order and Export tab.
4.check(Tick Mark) on your selected jar file. and click ok.
5.Now, clean your project and Run.
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • I have tried this,but its showing the same error.Is there any error in the code part where I am calling the method in the jar file from my android code? – Samrat Feb 07 '13 at 09:36
0

I think you have two options:

1- Set your compiler to 1.6 for the jar library.

2- Or, make your jar library actually an android project, and mark it as "library" in eclipse.

I would go for 1 if your jar library does not use any android code at all.

Cristiano Coelho
  • 1,675
  • 4
  • 27
  • 50