2015-11-26 46 views
4

我已经从Eclipse导入我的项目到Android Stdio和它运行在棒棒糖设备上,如果我在kitkat设备上运行,它会给我“没有类def找到”异常。Android工作室:java.lang.NoClassDefFoundError

在我的项目,我有两个包1 com.qapp具有核心功能类和已常用功能类2. om.qapp.common_class,比如我有UtillClass,有一种称为的方法showOkAlert它用于显示带有“ok”按钮的警报对话框。

如果我叫showOkAlert从活动方法,它在所有的棒棒糖设备和其他版本的设备执行成功给了我 java.lang.NoClassDefFoundError

代码示例:

package com.qapp.common_class; 

public class UtillClass { 
     // static method 
    public static void showOkAlert(final Context context, final String msgBody) { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
       context); 
     alertDialogBuilder.setTitle(context.getResources().getString(
       R.string.alert_msg_title)); 
     alertDialogBuilder.setMessage(msgBody).setCancelable(false) 
       .setPositiveButton("OK", new OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 
     AlertDialog alertDialog = alertDialogBuilder.create(); 
     alertDialog.show(); 
    } 

} 

而且我已经用在活动showOkAlert方法如下:

package com.qapp; 

import com.qapp.common_class.UtillClass; 

    public class TestActivity2 extends Activity { 

     private Button sendPush, gotoQnow; 
     ............. 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.gonow); 
      gotoQnow = (Button) findViewById(R.id.gotonow_btn); 
      ............. 
      gotoQnow.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View arg0) { 
        UtillClass.showOkAlert(TestActivity2.this,"hello"); // I get NoClassDefFoundError here Line no #53 
       } 
      }); 

     } 
    } 

注:

  • 及其对Eclipse的作品不无例外在所有类型的Android 版本中。
  • 在Android studio(导入的项目)中只能在棒棒糖设备中运行,其他版本设备则与 NoClassDefFoundError坠毁。
  • 我已经清理了构建和重建很多次,但没有用。
  • 它很奇怪的理解5.0和其他设备运行时发生了什么。
  • 我使用给出的Android 演播室版本1.4.1
  • 最低SDK版本是14

请帮我在Android的工作室来解决这个作品在所有的Android版本,我已经花费超过有一天,但我找不到解决方案。如果您需要更多信息,请在此留言。

我的日志在这里:

java.lang.NoClassDefFoundError: com.qapp.common_class.UtillClass$1 
at com.qapp.common_class.UtillClass.showOkAlert(UtillClass.java:382) 
at com.qapp.TestActivity2$1.onClick(TestActivity2.java:53) 
at android.view.View.performClick(View.java:3528) 
at android.view.View$PerformClick.run(View.java:14235) 
at android.os.Handler.handleCallback(Handler.java:605) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4424) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 
at dalvik.system.NativeStart.main(Native Method) 

java.lang.NoClassDefFoundError: com.qapp.common_class.UtillClass$1 
at com.qapp.common_class.UtillClass.showOkAlert(UtillClass.java:382) 
at com.qapp.TestActivity2$1.onClick(TestActivity2.java:53) 
at android.view.View.performClick(View.java:3528) 
at android.view.View$PerformClick.run(View.java:14235) 
at android.os.Handler.handleCallback(Handler.java:605) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4424) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 
at dalvik.system.NativeStart.main(Native Method) 

我的整个进口这里Utillclass

import java.io.FileNotFoundException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.LinkedHashMap; 
import java.util.Locale; 
import java.util.TimeZone; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import retrofit.RetrofitError; 
import retrofit.client.Response; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager; 
import android.content.pm.PackageManager.NameNotFoundException; 
import android.content.pm.Signature; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.BitmapShader; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Shader.TileMode; 
import android.graphics.Typeface; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.util.Base64; 
import android.util.Log; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
+0

向我们显示错误“NoClassDefFoundError”的日志,我们可以确切地告诉你,你正在使用的是API级别的棒棒糖。同时向我们展示'UtillClass'类的'进口' – Blundell

+0

请看看我更新的问题,我已经添加了logcat。 –

+0

hmm' $ 1'表示内部类。 “UtillClass”的第382行是什么? – Blundell

回答

0

从你的logcat:

java.lang.NoClassDefFoundError: com.qapp.common_class.UtillClass$1 at com.qapp.common_class.UtillClass.showOkAlert(UtillClass.java:382)

$1意味着第一1内部类$,因此找不到第382行的第一个内部类。

看来编译/类加载器步骤出错了。 这是越来越 http://developer.android.com/reference/android/view/View.OnClickListener.html

http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html

混合起来。

DialogInterface这样如果你预先固定在OnClickListener382

.setPositiveButton("OK", new DialogInterface.OnClickListener()

,并从类的顶部取下进口import android.content.DialogInterface.OnClickListener;。这更明确地告诉编译器您期望发生的事情。

4

我已经找到了Android Studio中NoClassDefFoundException的解决方案。我在清单XML中添加此

<application 
    android:name="android.support.multidex.MultiDexApplication" 
    ............ 
</application> 

添加multiDexEnabled真正 defaultConfig

defaultConfig { 
    ....... 
    multiDexEnabled true 
} 

现在是工作,没有在我的源代码改变。感谢所有。

+0

你知道为什么我们必须包含在manifest ? – nurgasemetey