java.lang.NoClassDefFoundError Android Studio

时间:2013-12-13 19:35:01

标签: java android android-studio

我正在尝试使用我的应用发送电子邮件,但每当我运行它时,我都会收到java.lang.NoClassDefFoundError。该应用程序是一个简单的按钮。当我按它时它总是崩溃并给我那个错误。我曾尝试过commons-mail library,javamail和commons-net,但我总是这样做。我的代码如下:

MainActivity.java:

package com.test.email;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;

import org.apache.commons.mail.EmailException;
import com.test.email.gmail;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment implements View.OnClickListener {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            Button btn = (Button)rootView.findViewById(R.id.button);
            btn.setOnClickListener(this);
            return rootView;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.button:
                    try{gmail.send();}
                    catch(Exception e){e.printStackTrace();}
                    System.out.println("heyy");
                break;
            }

        }
    }

    }

Gmail.java

package com.test.email;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.net.smtp.AuthenticatingSMTPClient;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;

import java.io.Writer;

/**
 * Created by ivykoko on 13/12/13.
 */
public class gmail {

    public static void send() throws EmailException{
        SimpleEmail mail = new SimpleEmail();
        //Configuracion necesaria para GMAIL
        mail.setHostName("smtp.gmail.com");
        mail.setTLS(true);
        mail.setSmtpPort(587);
        mail.setSSL(true);
        //En esta seccion colocar cuenta de usuario de Gmail y contraseña
        mail.setAuthentication("", "");

        //Cuenta de Email Destino
        mail.addTo("");
        //Cuenta de Email Origen, la misma con la que nos autenticamos
        mail.setFrom("");
        //Titulo del Email
        mail.setSubject("Email enviado usando Apache Commons Email");
        //Contenido del Email
        mail.setMsg("Mail enviado usando una cuenta de correo de GMAIL");
        mail.send();

    }
}

是的,我已正确导入所有库并将它们添加到build.gradle。

使用commons-net的Logcat:

2-13 14:20:18.158    1276-1276/com.test.email E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test.email, PID: 1276
    java.lang.NoClassDefFoundError: org.apache.commons.net.smtp.AuthenticatingSMTPClient
            at com.test.email.gmail.sendEmail(gmail.java:29)
            at com.test.email.MainActivity$PlaceholderFragment.onClick(MainActivity.java:74)
            at android.view.View.performClick(View.java:4424)
            at android.view.View$PerformClick.run(View.java:18383)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

Logcat使用commons-email:

12-13 14:23:42.198    1326-1326/com.test.email E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test.email, PID: 1326
    java.lang.NoClassDefFoundError: javax.activation.DataHandler
            at javax.mail.internet.MimeMessage.setContent(MimeMessage.java:1482)
            at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1066)
            at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1521)
            at org.apache.commons.mail.Email.buildMimeMessage(Email.java:1292)
            at org.apache.commons.mail.Email.send(Email.java:1436)
            at com.test.email.gmail.send(gmail.java:100)
            at com.test.email.MainActivity$PlaceholderFragment.onClick(MainActivity.java:74)
            at android.view.View.performClick(View.java:4424)
            at android.view.View$PerformClick.run(View.java:18383)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我们在https://code.google.com/p/android/issues/detail?id=63366中跟踪了一个错误,您必须在添加依赖项后清理项目(这有望在Android Studio 0.4.0中修复)。您可以尝试清理项目(构建菜单> 清理项目)并重试吗?