Class Def在使用jar文件的android代码中未发现错误



我在android的Eclispe ADT中使用一个简单的android代码来反转输入字符串。反向字符串函数是单独编写的,我已经将其导出为一个jar文件。然后我将jar添加到我的android项目的libs文件夹中。在构建项目时,eclipse不会显示amy错误,但在运行时,在给用户输入字符串并单击字符串按钮后,模拟器会显示消息"不幸的是,‘我的第一个项目’已经停止"

这是我的主要安卓活动类

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;
}

}

我正在使用的jar文件包含以下组件。1 META-INF文件夹,2.classpath文件,3.属性文件4一个文件夹Reeverse包含两个类文件Palindrome.class和
反转句子.class

这两个类的源代码如下:-

回文.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
 */

}

反向句子类:-

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;     
}

}

我没有解决问题的办法。由于我的java代码是使用JDK 1.7构建的,因此我尝试将Android首选项->编译器->JDK合规性修复为1.7。但我仍然会遭遇应用程序崩溃。

我使用Eclipse Juno EE作为java开发IDE,使用Eclipse java ADT作为Android IDE。

这是一个在android中检查jar文件可用性的简单程序,但我得到了noClassDefFound错误。也许我错过了什么,这是一个愚蠢的问题,但我仍然被困在这里。

请帮忙。。。。

试试这种方法。。。

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.

我认为您有两个选项:

1-将jar库的编译器设置为1.6。

2-或者,让你的jar库实际上是一个android项目,并在eclipse中将其标记为"库"。

如果你的jar库根本不使用任何android代码,我会选择1。

最新更新