简体   繁体   中英

Exception java.lang.NoClassDefFoundError android/app/Activity

In an android app, having:

public class Pars extends Activity{
    public Document docout(){

          InputStream is = getResources().openRawResource(R.raw.talechap01);
        Document docout = null;

         DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = null;
        try {
            builder = domFactory.newDocumentBuilder();
            docout = builder.parse(is);

        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return docout;
    }

and

public class Manager {

    public static String getStory(String spage, Document docs) {

          XPathExpression expr;
          XPath xpath = XPathFactory.newInstance().newXPath();

           Object result = null;
           String num = spage;
           String par = null;

           try {

           expr = xpath.compile("//decision"+num+"/p//text()");

            result = expr.evaluate(docs, XPathConstants.NODESET);
          }

         catch (XPathExpressionException e) {

             e.printStackTrace();
          }

          NodeList nodes = (NodeList) result;

         for (int i = 0; i < nodes.getLength(); i++) {

           par = nodes.item(i).getNodeValue();

           System.out.println(par);
         }
         return par;
      }
}

Now I want to test the code.

public class Test {

    public static void main(String[] args) {

        try {Pars p = new Pars();
        Document doc = p.docout();
        System.out.println(Manager.getStory("000", doc));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

I run "Test" as a java application and I get:

Exception in thread "main" java.lang.NoClassDefFoundError: android/app/Activity at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at com.stack.over.Test.main(Test.java:11) Caused by: java.lang.ClassNotFoundException: android.app.Activity at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Metho d) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 13 more

I tried to find how to fix it but cannot achieve it. I have this answer from another question but I'm kind of noob yet and can't figure it out how to fix it.

You have defined your Pars class as extending the class Activity. The error you are seeing is telling you that the class android.app.Activity is not available on the classpath. Maybe you need to check which libraries you import. – adamretter Mar 5 at 10:01

Thanks all.

The error is that you're trying to run a program that references Android classes ( android.app.Activity ) as a regular Java program that doesn't have Android runtime libraries in its classpath.

You should run Android apps as Android apps (on an emulator or a device) and regular Java programs as regular Java programs and not mix the two. Activity is Android; main() method is regular Java.

To learn Android app development, you can start with the developer.android.com getting started documentation .

The error message says it all. You are writing Android now. Trying to write a main method doesn't make any more sense in Android than it would if you were writing a webapp that runs in Tomcat. You are going to need to step back and read up on how Android works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM