2 u011283812 u011283812 于 2013.07.22 16:53 提问

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个回答

Jave_
Jave_   2013.07.22 17:23
已采纳

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

Jave_
Jave_ 恩恩,选答案吧
4 年多之前 回复
u011283812
u011283812 感谢,我弄好了
4 年多之前 回复
u011283812
u011283812 感谢,我成功了!!果然少了setContentView
4 年多之前 回复
Jave_
Jave_ 你需要先setContentView,再去findViewById。如果你是想new一个layout做view,就FrameLayout frameLayout=new FrameLayout(this);然后setContentView(frameLayout)
4 年多之前 回复
Jave_
Jave_ setContentView(R.layout.main)对应你这个 FrameLayout frameLayout=(FrameLayout)findViewById(R.id.mylayout);中的mylayout所在的xml文件
4 年多之前 回复
u011283812
u011283812 加了setContentView(frameLayout)就汇报这句话错误
4 年多之前 回复
shaojie519
shaojie519   2013.07.22 17:07

把logcat错误日志贴出来

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!