当前位置: 首页 > 知识库问答 >
问题:

我如何在Android Studio中消除这些错误?

郑正文
2023-03-14

我是一个新手android应用程序程序员,刚刚开始编写游戏。我最近发现了一个视频,其中一个红色的方块应该出现,并允许被拖动。我确定了这一点,并准确地复制了代码。以下是所有代码:

主要活动:

package com.onemorelaugh.mads;

import android.app.Activity;

import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //makes full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //gets rid of title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new GamePanel(this));
    }
}

游戏面板:

package com.onemorelaugh.mads;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by OneMoreLaugh on 8/7/2016.
 */
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
    private MainThread thread;
    private RectPlayer player;
    private Point playerPoint;

    public GamePanel(Context context){
        super(context);
        getHolder().addCallback(this);
        thread = new MainThread(getHolder(),this);
        player = new RectPlayer(new Rect(100, 100, 100, 100), Color.rgb(255, 0, 0));
        playerPoint = new Point(150,150);
        setFocusable(true);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder){
        thread = new MainThread(getHolder(),this);

        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        while (true) {
            try {
                thread.setRunning(false);
                thread.join();
            } catch (Exception e) {e.printStackTrace();}
            retry = false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        //return super.onTouchEvent(event);
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                playerPoint.set((int)event.getX(), (int)event.getY());
        }
        return true;
    }

    public void update() {
        player.update(playerPoint);
    }

    @Override
    public void draw(Canvas canvas){
        super.draw(canvas);
        canvas.drawColor(Color.WHITE);
        player.draw(canvas);
    }
}

游戏对象:

package com.onemorelaugh.mads;

import android.graphics.Canvas;

/**
 * Created by OneMoreLaugh on 8/7/2016.
 */
public interface GameObject {
    public void draw(Canvas canvas);
    public void update();
}

RectPlayer:

package com.onemorelaugh.mads;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
/**
 * Created by OneMoreLaugh on 8/7/2016.
 */
public class RectPlayer implements GameObject {

    private Rect rectangle;
    private int color;

    public RectPlayer(Rect rectangle, int color){
        this.rectangle = rectangle;
        this.color = color;
    }
    @Override
    public void draw(Canvas canvas){
        Paint paint = new Paint();
        paint.setColor(color);
        canvas.drawRect(rectangle, paint);


    }

    @Override
    public void update(){

    }

    public void update(Point point){
        rectangle.set(point.x - rectangle.width()/2, point.y - rectangle.height()/2, point.x + rectangle.width()/2, point.y + rectangle.height()/2);
    }
}

主线程:

package com.onemorelaugh.mads;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

/**
 * Created by OneMoreLaugh on 8/7/2016.
 */

public class MainThread extends Thread{
    public static final int MAX_FPS = 30;
    private double averageFPS;
    private SurfaceHolder surfaceHolder;
    private GamePanel gamePanel;
    private boolean running;
    public static Canvas canvas;

    public void setRunning(boolean running){
        this.running = running;
    }

    public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel){
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;

    }

    @Override
    public void run(){
        long startTime;
        long timeMillis = 1000/MAX_FPS;
        long waitTime;
        int frameCount = 0;
        long totalTime = 0;
        long targetTime = 1000/MAX_FPS;

        while(running){
            startTime = System.nanoTime();
            canvas = null;
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    this.gamePanel.update();
                    this.gamePanel.draw(canvas);
                }
            }catch(Exception e){e.printStackTrace();}
            finally {
                if(canvas != null){
                    try {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }catch(Exception e) {e.printStackTrace();}
                }
            }
            timeMillis = (System.nanoTime() - startTime)/1000000;
            waitTime =  targetTime - timeMillis;
            try {
                if (waitTime > 0)
                    this.sleep(waitTime);
            }catch(Exception e){e.printStackTrace();}
            totalTime += System.nanoTime() - startTime;
            frameCount++;
            if (frameCount == MAX_FPS){
                averageFPS = 1000/((totalTime/frameCount)/1000000);
                frameCount = 0;
                totalTime = 0;
                System.out.println(averageFPS);
            }
        }

    }
}

现在我仔细检查了这段代码,它对视频中的人有效。当我尝试运行它时,(我使用的是android studio),模拟器会弹出并显示手机的主屏幕。这就像应用程序崩溃一样。下面是我得到的错误代码:

Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:prepareDebugUnitTestDependencies, :app:mockableAndroidJar, :app:generateDebugAndroidTestSources, :app:assembleDebug]
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2340Library
:app:prepareComAndroidSupportAppcompatV72340Library
:app:prepareComAndroidSupportSupportV42340Library
:app:prepareComAndroidSupportSupportVectorDrawable2340Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:mergeDebugShaders
:app:compileDebugShaders
:app:generateDebugAssets
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:preDebugUnitTestBuild UP-TO-DATE
:app:prepareDebugUnitTestDependencies
:app:mockableAndroidJar UP-TO-DATE
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl
:app:processDebugAndroidTestManifest
:app:compileDebugAndroidTestRenderscript
:app:generateDebugAndroidTestBuildConfig
:app:mergeDebugAndroidTestShaders
:app:compileDebugAndroidTestShaders
:app:generateDebugAndroidTestAssets
:app:mergeDebugAndroidTestAssets
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources
:app:generateDebugAndroidTestSources
:app:incrementalDebugJavaCompilationSafeguard
:app:compileDebugJavaWithJavac
:app:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:buildInfoDebugLoader
:app:transformClassesWithExtractJarsForDebug
:app:transformClassesWithInstantRunVerifierForDebug
:app:transformClassesWithJavaResourcesVerifierForDebug UP-TO-DATE
:app:mergeDebugJniLibFolders
:app:transformNative_libsWithMergeJniLibsForDebug
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug
:app:transformResourcesAndNative_libsWithJavaResourcesVerifierForDebug UP-TO-DATE
:app:transformClassesWithInstantRunForDebug
:app:transformClasses_enhancedWithInstant+reloadDexForDebug UP-TO-DATE
:app:incrementalDebugTasks
:app:prePackageMarkerForDebug
:app:fastDeployDebugExtractor
:app:generateDebugInstantRunAppInfo
:app:coldswapKickerDebug
:app:transformClassesWithInstantRunSlicerForDebug
:app:transformClassesWithDexForDebug
To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html
Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0
Error:  at java.lang.ClassLoader.defineClass1(Native Method)
Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0
Error:  at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
Error:  at java.lang.ClassLoader.defineClass1(Native Method)
Error:  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
Error:  at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
Error:  at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
Error:  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
Error:  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
Error:  at java.security.AccessController.doPrivileged(Native Method)
Error:  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
Error:  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
Error:  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
Error:  at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Error:  at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Error:Exception in thread "main" 
Error:  at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0
Error:  at java.lang.ClassLoader.defineClass1(Native Method)
Error:  at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
Error:  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
Error:  at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
Error:  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
Error:  at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
Error:  at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
Error:  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
Error:  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0
Error:  at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
Error:  at java.lang.ClassLoader.defineClass1(Native Method)
Error:  at java.security.AccessController.doPrivileged(Native Method)
Error:  at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
:app:transformClassesWithDexForDebug FAILED
Error:  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
Error:  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
Error:  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
Error:  at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
Error:  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
Error:  at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
Error:  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
Error:  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
Error:  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
Error:  at java.security.AccessController.doPrivileged(Native Method)
Error:  at java.security.AccessController.doPrivileged(Native Method)
Error:  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
Error:  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
Error:  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
Error:  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
Error:  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
Error:  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
Error:  at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
FAILURE: Build failed with an exception.
Error:  at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Error:Exception in thread "main" 
* What went wrong:
Error:  at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Error:Exception in thread "main" 
Error:  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
Error:  at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Error:  at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Error:Exception in thread "main" 
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Information:BUILD FAILED
Information:Total time: 4.702 secs
Information:59 errors
Information:0 warnings
Information:See complete output in console

我不知道我做错了什么。谢谢大家的回答。

共有1个答案

伏业
2023-03-14

键位于第一个错误行中:

Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0

在谷歌上搜索这个字符串,第一个答案是这个帖子:我的应用程序中不支持的major.minor版本52.0

编程技巧#1:当您收到错误时,您要做的第一件事就是查找错误并向Google询问。

 类似资料:
  • 已经在proguard-rules.pro上尝试了 -keepattributes cunclosingmethod和-dontwarn innerclasses ,但没有成功:( 你们有什么提示吗? 分级文件(主要部分): java-version&&javac-version java version“1.7.0_71” java(TM)SE运行时环境(构建1.7.0_71-B14) java

  • 这是在使用Firebase消息传递推送通知时显示的错误... 这是Firebase消息的js代码 这个日志是打印出来的。。。 这是颤振代码 获得ios权限,但我正在使用android手机进行调试 onCreateActivityFeedItem活动源项已创建{commentData:'ab to man ja',mediaUrl:'https://firebasestorage.googleapi

  • 如何从没有主键的表中删除满足几个条件的行?(实际上主键由每一列组成) 经过多次尝试后,我有了一段代码,它首先创建一个临时表,在其中插入我想删除的行,最后我尝试从原始表中删除那些与临时表相比符合条件的行。 但是它给了我一个关于不能链接几个部分标识符#temp.idCarga的错误 也许这很容易,但是我已经尝试了这么多时间,我无法正常集中注意力。

  • 错误(25,1):PLS-00103:遇到符号“EXEC” 放下桌上的学生;提交;

  • 编译过程中的错误有: 说明资源路径位置类型new AsyncHttpResponseHandler(){}类型的onSuccess(String)方法必须重写或实现超类型方法

  • 否则,当我删除 inplace=true 时,它工作得很好,这是代码:in: x=df1.drop('结果', axis=1, inplace=True) y=df1['结果'] out: KeyError Traceback(最近一次调用last)~\ anaconda 3 \ lib \ site-packages \ pandas \ core \ indexes \ base . py i