java Android Studio 应用程序可在模拟器中运行,但不能在真实设备上运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41179790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio app works in emulator but not on a real device
提问by John O'Neil
I am new to android programming and have been having a lot of trouble with it. I finally got my app to work in the emulator on both API 23 and 16 my target is API 16. It runs with no problems on the emulators but when I try to use it on my phone (Google Nexus 5 API 23)
我是 android 编程的新手,并且遇到了很多麻烦。我终于让我的应用程序在 API 23 和 16 的模拟器中工作,我的目标是 API 16。它在模拟器上运行没有问题,但是当我尝试在我的手机上使用它时(Google Nexus 5 API 23)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center"
android:orientation="vertical"
android:background="#F44336"
android:id="@+id/background"
tools:context="thereisstuffhere.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/factTextBox"
android:textSize="25dp"
android:textColor="#fff"/>
</LinearLayout>
package there is stuff here too;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView factBox;
LinearLayout bg;
Facts factHolder = new Facts(this);
Backgrounds backs = new Backgrounds();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
factBox = (TextView) findViewById(R.id.factTextBox);
factBox.setText(factHolder.nextFact());
bg = (LinearLayout) findViewById(R.id.background);
bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
bg.setOnTouchListener(new OnSwipeTouchListener(this)
{
public void onSwipeTop()
{
//Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight()
{
//Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
prev();
}
public void onSwipeLeft()
{
//Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
next();
}
public void onSwipeBottom()
{
//Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
private void next() {
factBox.setText(factHolder.nextFact());
bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
}
private void prev() {
factBox.setText(factHolder.prevFact());
bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
}
public void genFact(View view)
{
factBox.setText(factHolder.nextFact());
}
Facts.java just contains a huge list and two functions that return the next or previous string in the list
Facts.java 只包含一个巨大的列表和两个返回列表中下一个或上一个字符串的函数
Backgrounds.java does essentially the same thing only it uses an Integer list and the colors.xml file to store the tags etc
Backgrounds.java 基本上做同样的事情,只是它使用一个整数列表和 color.xml 文件来存储标签等
This is my OnTouchListener class:
这是我的 OnTouchListener 类:
package stuff be here;
import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class OnSwipeTouchListener implements OnTouchListener {
final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx)
{
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
return false;
}
private final class GestureListener extends SimpleOnGestureListener{
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e)
{
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
boolean result = false;
try{
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if(Math.abs(diffX) > Math.abs(diffY)){
if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD){
if(diffX > 0){
onSwipeRight();
}else{
onSwipeLeft();
}
}
result = true;
}
else if(Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD){
if(diffY > 0){
onSwipeBottom();
}else{
onSwipeTop();
}
}
result = true;
} catch (Exception exception){
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight(){}
public void onSwipeLeft(){}
public void onSwipeTop(){}
public void onSwipeBottom(){}
}
I apologise for all of the code but I have been trying to fix this for a while to no avail. I get a lot of errors when I try to run like "Class not found using the boot class loader; no stack trace available" and "Unable to instantiate activity ComponentInf..." I also get some warnings like "ClassLoader referenced unknown path: /data/app/packagenamehere-1/lib/x86_64 and /data/app/packagenamehere-1/lib/arm" even when running in the emulator
我为所有代码道歉,但我一直试图解决这个问题,但无济于事。当我尝试运行诸如“使用引导类加载器未找到类;没有可用的堆栈跟踪”和“无法实例化活动 ComponentInf...”之类的运行时,我遇到了很多错误,我还收到了一些警告,例如“ClassLoader 引用了未知路径: /data/app/packagenamehere-1/lib/x86_64 和 /data/app/packagenamehere-1/lib/arm" 即使在模拟器中运行
Here is my log cat file:
这是我的日志猫文件:
12-16 17:26:25.167 22237-22237/com.ubiquity.sciencefacts I/art: Late-enabling -Xcheck:jni
12-16 17:26:25.197 22237-22243/com.ubiquity.sciencefacts I/art: Ignoring second debugger -- accepting and dropping
12-16 17:26:25.203 22237-22243/com.ubiquity.sciencefacts I/art: Debugger is no longer active
12-16 17:26:25.211 22237-22243/com.ubiquity.sciencefacts W/art: Suspending all threads took: 7.324ms
12-16 17:26:25.235 22237-22237/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-2/lib/arm
12-16 17:26:25.248 22237-22237/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
12-16 17:26:25.254 22237-22237/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
12-16 17:26:27.065 22237-22237/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 22237 SIG: 9
12-16 17:33:18.574 24986-24986/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/arm
12-16 17:33:18.662 24986-24986/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
12-16 17:33:18.673 24986-24986/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
12-16 17:33:20.800 24986-24986/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 24986 SIG: 9
12-16 17:34:15.668 25845-25845/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/arm
12-16 17:34:15.687 25845-25845/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
12-16 17:34:15.693 25845-25845/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
12-16 18:00:52.080 2640-2640/com.ubiquity.sciencefacts I/art: Not late-enabling -Xcheck:jni (already on)
12-16 18:00:52.142 2640-2640/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/x86_64
12-16 18:00:52.568 2640-2640/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/x86_64
12-16 18:00:52.805 2640-2640/com.ubiquity.sciencefacts W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-16 18:00:53.993 2640-2646/com.ubiquity.sciencefacts W/art: Suspending all threads took: 633.582ms
12-16 18:00:54.020 2640-2716/com.ubiquity.sciencefacts D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-16 18:00:54.063 2640-2716/com.ubiquity.sciencefacts I/OpenGLRenderer: Initialized EGL, version 1.4
12-16 18:00:54.091 2640-2716/com.ubiquity.sciencefacts E/EGL_emulation: tid 2716: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
12-16 18:00:54.091 2640-2716/com.ubiquity.sciencefacts W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f8b8ec90c40, error=EGL_BAD_MATCH
12-16 18:34:41.488 27869-27869/com.ubiquity.sciencefacts I/art: Late-enabling -Xcheck:jni
12-16 18:34:41.561 27869-27869/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-2/lib/arm
12-16 18:34:41.586 27869-27869/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
12-16 18:34:41.589 27869-27869/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ubiquity.sciencefacts, PID: 27869
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ubiquity.sciencefacts/com.ubiquity.sciencefacts.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.ubiquity.sciencefacts.MainActivity" on path: DexPathList[[zip file "/data/app/com.ubiquity.sciencefacts-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ubiquity.sciencefacts-2/lib/arm, /vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.ubiquity.sciencefacts.MainActivity" on path: DexPathList[[zip file "/data/app/com.ubiquity.sciencefacts-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ubiquity.sciencefacts-2/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)?
at android.app.ActivityThread.-wrap11(ActivityThread.java)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)?
at android.os.Handler.dispatchMessage(Handler.java:102)?
at android.os.Looper.loop(Looper.java:148)?
at android.app.ActivityThread.main(ActivityThread.java:5417)?
at java.lang.reflect.Method.invoke(Native Method)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)?
Suppressed: java.lang.ClassNotFoundException: com.ubiquity.sciencefacts.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 12 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
12-16 18:34:43.772 27869-27869/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 27869 SIG: 9
IMPORTANT UPDATE: I built the program as an apk and installed it on my phone using that and it worked without issue. Does this imply a target error as was said below in the comments? Would this affect the distribution of my app (for future reference I am not actually distributing this)?
重要更新:我将程序构建为 apk 并使用它安装在我的手机上,它运行没有问题。这是否意味着如下评论中所述的目标错误?这是否会影响我的应用程序的分发(以供将来参考,我实际上并未分发此应用程序)?
回答by beal
Regarding your error, it seems there are missing some SDKs.
关于您的错误,似乎缺少一些 SDK。
Check up your SDK manager if all the needed SDKs and SDK-tools are installed.
如果安装了所有需要的 SDK 和 SDK 工具,请检查您的 SDK 管理器。
回答by Avinash kumawat
Use multidexEnabled in your build.gradle:
在 build.gradle 中使用 multidexEnabled:
defaultConfig {
multiDexEnabled true
}
clean , build and run
清理、构建和运行
回答by imsrgadich
In most of the cases, just cleaning
and rebuilding
the project would do the trick.
在大多数情况下,只是cleaning
和rebuilding
项目会起作用。
回答by Danger
When you are debugging/ running your app using USB cable connected to Android Studio, for some devices, when you uninstall the app and try to run it again through studio it shows target error.
当您使用连接到 Android Studio 的 USB 电缆调试/运行您的应用程序时,对于某些设备,当您卸载应用程序并尝试通过 Studio 再次运行它时,它会显示目标错误。
For this once you uninstall the app, simply disconnect your device from USB cable and connect again.
为此,一旦您卸载应用程序,只需从 USB 电缆断开您的设备并重新连接。
回答by beal
You could also check your Build.Gradle file(s). In your case it should look like this:
您还可以检查您的 Build.Gradle 文件。在你的情况下,它应该是这样的:
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
be sure you disabled proguard by setting minifyEnabled to false. this can cause to be sure that this causes no additional errors ;)
确保通过将 minifyEnabled 设置为 false 来禁用 proguard。这可以确保这不会导致其他错误;)