问题描述
我将项目从Eclipse导入到Android Stdio,它的运行在Lollipop设备上,如果我运行在kitkat设备上,它给我没有类def def异常。在我的项目中,我有两个包 1。 com.qapp 具有核心功能类和 2。 om.qapp.common_class 其中有常用的功能类,例如我有 UtillClass ,有一种方法叫做 showOkAlert 它用于显示一个警告对话框使用确定按钮。
如果我从Activity中调用了 showOkAlert 方法,则其在所有Lollipop设备和其他版本设备中成功执行,我的
java.lang.NoClassDefFoundError 。
代码示例:
package com.qapp.common_class;
public class UtillClass {
//静态方法
public static void showOkAlert(final Context context,final String msgBody){
AlertDialog.Builder alertDialogBuilder = new AlertDialog。 Builder(
上下文);
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); //我得到NoClassDefFoundError行号#53
}
});
}
}
注意:
- 它在Eclipse上的工作,在所有类型的Android
版本中都不例外。 - 在Android studio项目导入)只适用于Lollipop设备,其他版本的设备崩溃了
NoClassDefFoundError 。 - 我已经清理了构建并重建了很多次,不要使用。
- 很奇怪,了解在运行5.0和其他设备之间发生的情况。
- 我正在使用 Android Studio版本1.4.1
- 最小SDK版本为14
请帮我在Android studio中解决这个问题,在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)
在android.view.View.performClick(View.java:3528)
在android.view.View $ PerformClick.run(View.java:14235)
在android.os.Handler.handleCallback(Handler.java:605)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop( Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4424)
在java.lang.reflect.Method.invokeNative(本机方法)
在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)
在dalvik.system.NativeStart.main(Native Method)$ c
com.qapp.com .TestActivity2 $ 1.onClick(TestActivity2.java:53)
在android.view.View.performClick(View.java:3528)
在android.view.View $ PerformClick.run(View.java: 14235)
在android.os.Handler.handleCallback(Handler.java:605)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper .loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4424)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:817)
在com.android .internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
我的整个进口都在这里用于Utillclass
impo rt 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;
我在Android studio中找到了NoClassDefFoundException的解决方案。我已经在我的清单XML中添加了这个
< application
android:name =android.support.multidex。 MultiDexApplication
............
< / application>
在 defaultConfig 中添加 multiDexEnabled true p>
defaultConfig {
.......
multiDexEnabled true
}
它现在正在工作,在我的源代码中没有任何改变。感谢所有人。
I have imported my project from Eclipse to Android Stdio and its runs on Lollipop devices if I run on kitkat devices it gives me "No class def found" exception.
In my project I have two packages 1. com.qapp which has core functionality class and 2. om.qapp.common_class which has commonly used functionality class, for example I have UtillClass,there is a method called showOkAlert it is used for show an alert dialog with "ok" button.
If I called the showOkAlert methods from an Activity, its successfully executed in all Lollipop devices and other version devices gives mejava.lang.NoClassDefFoundError.
Code sample:
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();
}
}
And I have used showOkAlert method in an Activity as follows:
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
}
});
}
}
Note:
- Its works on Eclipse without no exception in all type of Androidversion.
- In Android studio (project imported) only works in Lollipop devices, other version device its crashed withNoClassDefFoundError.
- I have clean the build and rebuild so many times but no use.
- Its very strange to understand what happen between 5.0 and other devices while running.
- I am using Android studio version 1.4.1
- minimum SDK version given is 14
Please help me to resolve this in Android studio to works all in Android versions, I have spend more than One day for it but I could not found solution. If you need more information please leave a comment here.
My Log here:
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)
my whole imports are here for 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;
I have found out a solution for NoClassDefFoundException in Android studio. I have added this in my Manifest XML
<application
android:name="android.support.multidex.MultiDexApplication"
............
</application>
Add multiDexEnabled true in defaultConfig
defaultConfig {
.......
multiDexEnabled true
}
it is working now and nothing changed in my source code. Thanks for all.
这篇关于Android studio:java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!