ljw930824
ljw930824
2015-04-23 09:09
采纳率: 100%
浏览 1.8k

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
谢谢大家看下,帮个忙看看是什么错误

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • sirnuo
    sirnuo 2015-04-29 06:20
    已采纳

    找不到类 com.example.ontouchlistenerdemo.MainActivity
    你应该在 Androidmanifest.xml 文件中,把MainActivity改为你的自定义类 Game
    所有需要显示的Activity,都要在 Androidmanifest.xml 文件中声明。相关信息,可以百度搜索,google搜索
    activity Androidmanifest.xml

    点赞 评论
  • u014240783
    扯线木偶人 2015-04-23 09:27

    找不到你的启动activity,应该是你没有在manifest.xml文件中声明该activity,

    点赞 评论
  • bdmh
    bdmh 2015-04-23 09:34

    看看activity有没有加到mainfest、文件中

    点赞 评论
  • crazy1235
    月盡天明 2015-04-23 10:53

    有没有在manifest中声明你的activity??????

    点赞 评论
  • ljw930824
    ljw930824 2015-04-27 09:53

    你好,请问具体是什么意思?谢谢刚学不懂。

    点赞 评论

相关推荐