Android Studio应用可在模拟器中运行,但无法在真实的设备上运行

nlejzf6q  于 2023-01-09  发布在  Android
关注(0)|答案(8)|浏览(339)

我是Android编程新手,一直遇到很多麻烦。我终于让我的应用程序在API 23和16的模拟器中工作,我的目标是API 16。它在模拟器上运行没有问题,但当我尝试在手机上使用它时(谷歌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
Backgrounds.java does essentially the same thing only it uses an Integer list and the colors.xml file to store the tags etc
这是我的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(){}
}

我为所有的代码道歉,但是我已经尝试修复这个问题一段时间了,但是没有效果。当我尝试运行时,我得到了很多错误,比如"使用引导类加载器找不到类; no stack trace available "和"无法示例化活动ComponentInf ..."我还收到一些警告,如" ClassLoader引用了未知路径:/data/app/packagenamehere-1/lib/x86_64和/data/app/packagenamehere-1/lib/arm ",即使在模拟器中运行时也是如此
下面是我的日志cat文件:

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

重要更新:我建立了一个APK的程序,并安装在我的手机上使用,它的工作没有问题。这是否意味着一个目标错误,正如下面的评论所说?这会影响我的应用程序的分发(供将来参考,我不是真的分发这个)?

yhqotfr8

yhqotfr81#

关于您的错误,似乎有一些SDK丢失。
检查您的SDK管理器是否安装了所有需要的SDK和SDK工具。

6xfqseft

6xfqseft2#

在您的build.gradle中使用multidexEnabled:

defaultConfig {
     multiDexEnabled true
}

清理、构建和运行

svmlkihl

svmlkihl3#

您还可以检查您的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'
        }
    }
}

确保你通过设置minifyEnabled为false禁用了proguard.这可以导致确保这不会导致额外的错误;)

pkln4tw6

pkln4tw64#

在大多数情况下,项目只需cleaningrebuilding就可以了。

djp7away

djp7away5#

尝试启用USB调试模式。Here's a manual

2q5ifsrm

2q5ifsrm6#

当您使用连接到Android Studio的USB数据线调试/运行应用时,对于某些设备,当您卸载应用并尝试通过Studio再次运行时,它会显示目标错误。
为此,一旦你卸载了应用程序,只需断开你的设备从USB电缆,并再次连接。

ohtdti5x

ohtdti5x7#

如果以上都不起作用,则出于其他可能原因。
我的应用程序在模拟器中运行没有问题,但不会在像素设备上运行。我在应用程序下的清单中有以下行:
机器人:仅测试=“真”
一旦删除了这条线,应用程序在设备上就能完美运行。

t9aqgxwy

t9aqgxwy8#

    • 奖金可能对某人有所帮助:**

1.首先通过更改以下内容生成可调试生成:
Debuggable trueminifyEnabled false
1.将日志添加到预期应用程序崩溃的代码库中
1.创建一个apk,将你的设备连接到你的系统,并在你的手机上启用usb调试
1.在android studio中打开logcat并从下拉菜单中选择可调试进程
1.检查日志,它可能会给你提示是什么问题。
此外,可以有一个案例,特别是对于Android版本31:
有个很棒的答案你可以试试.
此外,尝试删除react native中Android文件夹内的. gradle文件夹
同时确保您使用的是JDK 11,有时它也会因此崩溃

相关问题