使用jar文件在Android代码中找不到类Def

时间:2013-02-07 09:28:22

标签: android jar noclassdeffounderror

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

这是我的主要Android活动类

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 .properties文件             4文件夹Reeverse包含两个类文件Palindrome.class和
               ReverseSentence.class

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

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类: -

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;     

}



}

我没有得到解决问题的解决方案。我尝试修复Android偏好设置 - >反编译> JDK符合1.7,因为我的java代码是使用JDK 1.7构建的。 但我仍然让应用程序崩溃。

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

这是一个简单的程序,用于检查android文件中jar文件的可用性,但是我收到noClassDefFound错误。可能我错过了一些东西,这是一个狡猾的问题,但我仍然坚持这个。

请帮忙......

2 个答案:

答案 0 :(得分:0)

试试这个......

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 :(得分:0)

我认为你有两个选择:

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

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

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