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....