程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-Android 開發的程序eclipse沒報錯,但意外停止

android-Android 開發的程序eclipse沒報錯,但意外停止

編輯:編程綜合問答
Android 開發的程序eclipse沒報錯,但意外停止

目的是編寫一個單點觸摸用戶輸入的例子,以下是程序代碼

package com.example.ontouchlistenerdemo;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.*;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnTouchListener;

public class Game extends Activity implements OnTouchListener {

DrawView drawView;

Point touch = new Point(0,0);
String inputAction= "";

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawView = new DrawView(this);
setContentView(drawView);

    drawView.setOnTouchListener(this);
}

public void onResume(){
    super.onResume();
    drawView.resume();
}
public void onPause(){
    super.onPause();
    drawView.pause();
}

public boolean onTouch(View v,MotionEvent event){
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        inputAction = "DOWN";
        break;

    case MotionEvent.ACTION_MOVE:
        inputAction = "MOVE";
        break;

    case MotionEvent.ACTION_UP:
        inputAction = "UP";
        break;

    }
    touch.x = (int)event.getX();
    touch.y = (int)event.getY();

    return true;
}

public class DrawView extends SurfaceView implements Runnable
{
    Thread gameloop = null;

    SurfaceHolder surface = null;

    volatile boolean running = false;

    AssetManager assets = null;

    public DrawView(Context context)
    {
        super(context);
        surface = getHolder();
        assets = context.getAssets();
    }
    public void resume(){
        running = true;
        gameloop = new Thread(this);
        gameloop.start();
    }
    public void pause(){
        running = false;
        while(true){
            try{
                gameloop.join();
            }catch(InterruptedException e){}
        }
    }
    public void run(){
        while(running)
        {
            if(!surface.getSurface().isValid())
                continue;

            Canvas canvas = surface.lockCanvas();
            canvas.drawColor(Color.BLACK);

            Paint paint= new Paint();
            paint.setColor(Color.WHITE);
            paint.setTextSize(24);
            canvas.drawText("Touch screen to test single touch input", 10, 20, paint);
            canvas.drawText("Action: " +inputAction, 10, 50, paint);
            canvas.drawText("Position: "+touch.x+","+touch.y,10 ,80, paint);

            if(touch.x != 0&& touch.y!=0)
                canvas.drawCircle(touch.x, touch.y, 50, paint);

            surface.unlockCanvasAndPost(canvas);

            try{
                Thread.sleep(20);
            }catch(InterruptedException e)
            {
                e.printStackTrace();
            }

        }
    }
}
}

然後是Log的報錯

04-23 09:05:03.901: E/AndroidRuntime(350): FATAL EXCEPTION: main
04-23 09:05:03.901: E/AndroidRuntime(350): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ontouchlistenerdemo/com.example.ontouchlistenerdemo.MainActivity}: java.lang.ClassNotFoundException: com.example.ontouchlistenerdemo.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.ontouchlistenerdemo-1.apk]
04-23 09:05:03.901: E/AndroidRuntime(350): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
04-23 09:05:03.901: E/AndroidRuntime(350): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-23 09:05:03.901: E/AndroidRuntime(350): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-23 09:05:03.901: E/AndroidRuntime(350): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-23 09:05:03.901: E/AndroidRuntime(350): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 09:05:03.901: E/AndroidRuntime(350): at android.os.Looper.loop(Looper.java:123)
04-23 09:05:03.901: E/AndroidRuntime(350): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 09:05:03.901: E/AndroidRuntime(350): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 09:05:03.901: E/AndroidRuntime(350): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 09:05:03.901: E/AndroidRuntime(350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 09:05:03.901: E/AndroidRuntime(350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 09:05:03.901: E/AndroidRuntime(350): at dalvik.system.NativeStart.main(Native Method)
04-23 09:05:03.901: E/AndroidRuntime(350): Caused by: java.lang.ClassNotFoundException: com.example.ontouchlistenerdemo.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.ontouchlistenerdemo-1.apk]
04-23 09:05:03.901: E/AndroidRuntime(350): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-23 09:05:03.901: E/AndroidRuntime(350): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-23 09:05:03.901: E/AndroidRuntime(350): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-23 09:05:03.901: E/AndroidRuntime(350): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-23 09:05:03.901: E/AndroidRuntime(350): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
04-23 09:05:03.901: E/AndroidRuntime(350): ... 11 more
謝謝大家看下,幫個忙看看是什麼錯誤

最佳回答:


找不到類 com.example.ontouchlistenerdemo.MainActivity
你應該在 Androidmanifest.xml 文件中,把MainActivity改為你的自定義類 Game
所有需要顯示的Activity,都要在 Androidmanifest.xml 文件中聲明。相關信息,可以百度搜索,google搜索
activity Androidmanifest.xml

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved