zhengran14 2013-07-22 08:53 采纳率: 50%
浏览 8765
已采纳

android程序,eclipse没报错,但是运行会意外停止

本人刚开始学习android,跟着书写了个程序,这个程序是会有一个东西随着手指拖动而移动,编译完没有问题,但是在手机上运行的时候却是意外停止,错误显示在了MainActivity的第27行,我真不知道哪里错,代码和书上写的一模一样,求大神指导

MainActivity的代码:

package com.example.test34;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frameLayout=(FrameLayout)findViewById(R.id.mylayout);
    final RabbitView rabbit=new RabbitView(MainActivity.this);
    rabbit.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v,MotionEvent event){
            rabbit.bitmapX=event.getX();
            rabbit.bitmapY=event.getY();
            rabbit.invalidate();
            return true;
        }
    });
    frameLayout.addView(rabbit);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

RabbitView.java代码

package com.example.test34;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

public class RabbitView extends View {
public float bitmapX;
public float bitmapY;
public RabbitView(Context context){
super(context);
bitmapX=750;
bitmapY=500;
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint=new Paint();
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher);
canvas.drawBitmap(bitmap,bitmapX,bitmapY,paint);
if(bitmap.isRecycled()){
bitmap.recycle();
}
}

}

activity_main代码

<FrameLayout 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"
tools:context=".MainActivity"
android:background="@drawable/background"
android:id="@+id/mylayout" >

logcat为:

07-22 17:27:52.100: D/dalvikvm(11872): GC_EXTERNAL_ALLOC freed 47K, 50% free 2740K/5379K, external 0K/0K, paused 71ms

07-22 17:27:52.120: D/AndroidRuntime(11872): Shutting down VM
07-22 17:27:52.120: W/dalvikvm(11872): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
07-22 17:27:52.120: E/AndroidRuntime(11872): FATAL EXCEPTION: main
07-22 17:27:52.120: E/AndroidRuntime(11872): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test34/com.example.test34.MainActivity}: java.lang.NullPointerException
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.os.Looper.loop(Looper.java:130)
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-22 17:27:52.120: E/AndroidRuntime(11872): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 17:27:52.120: E/AndroidRuntime(11872): at java.lang.reflect.Method.invoke(Method.java:507)
07-22 17:27:52.120: E/AndroidRuntime(11872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
07-22 17:27:52.120: E/AndroidRuntime(11872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
07-22 17:27:52.120: E/AndroidRuntime(11872): at dalvik.system.NativeStart.main(Native Method)
07-22 17:27:52.120: E/AndroidRuntime(11872): Caused by: java.lang.NullPointerException
07-22 17:27:52.120: E/AndroidRuntime(11872): at com.example.test34.MainActivity.onCreate(MainActivity.java:27)
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
07-22 17:27:52.120: E/AndroidRuntime(11872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-22 17:27:52.120: E/AndroidRuntime(11872): ... 11 more
07-22 17:27:54.130: I/dalvikvm(11872): threadid=4: reacting to signal 3
07-22 17:27:54.130: I/dalvikvm(11872): Wrote stack traces to '/data/anr/traces.txt'

  • 写回答

2条回答 默认 最新

  • JaveZh 2013-07-22 09:23
    关注

    你没有setContentView就直接查找FrameLayout那个了
    报的应该是空指针错误把

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 paddleocr运行报错
  • ¥15 怎么用 matlab 设计滞后-超前串联校正网络
  • ¥15 MFC引用C#生成的dll,将dll放置到非exe程序目录,如何操作
  • ¥15 C#创建webservice接口,三方通过多次跳转访问本方服务,获取wsdl文档,wsdl中ip地址为局域网内本机地址而非三方直接访问的地址。
  • ¥15 关于#wireshark#的问题:需要安卓app流量数据集要安卓流量做包序列长度的实验,比如某些流量是在看视频还是在发评论
  • ¥15 Smail语句如何使用判断语句跳过验证卡密界面
  • ¥15 关于#wireshark#的问题:并且能够给数据做标注,如这个流量是在看视频或者是在转账
  • ¥15 运筹优化,gurobi,python
  • ¥20 springmvc重定向和返回json
  • ¥15 电脑键盘实现触摸功能