为了账号安全,请及时绑定邮箱和手机立即绑定

为什么汇报空指针异常

05-26 16:23:07.836: E/AndroidRuntime(1702): FATAL EXCEPTION: main

05-26 16:23:07.836: E/AndroidRuntime(1702): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lunbo/com.example.lunbo.MainActivity}: java.lang.NullPointerException

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.app.ActivityThread.access$1500(ActivityThread.java:117)

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.os.Handler.dispatchMessage(Handler.java:99)

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.os.Looper.loop(Looper.java:130)

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.app.ActivityThread.main(ActivityThread.java:3683)

05-26 16:23:07.836: E/AndroidRuntime(1702): at java.lang.reflect.Method.invokeNative(Native Method)

05-26 16:23:07.836: E/AndroidRuntime(1702): at java.lang.reflect.Method.invoke(Method.java:507)

05-26 16:23:07.836: E/AndroidRuntime(1702): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

05-26 16:23:07.836: E/AndroidRuntime(1702): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

05-26 16:23:07.836: E/AndroidRuntime(1702): at dalvik.system.NativeStart.main(Native Method)

05-26 16:23:07.836: E/AndroidRuntime(1702): Caused by: java.lang.NullPointerException

05-26 16:23:07.836: E/AndroidRuntime(1702): at com.example.lunbo.MainActivity.onCreate(MainActivity.java:36)

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-26 16:23:07.836: E/AndroidRuntime(1702): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

05-26 16:23:07.836: E/AndroidRuntime(1702): ... 11 more

05-26 16:23:35.537: D/AndroidRuntime(1740): Shutting down VM

05-26 16:23:35.537: W/dalvikvm(1740): threadid=1: thread exiting with uncaught exception (group=0xb5f7c4f0)

05-26 16:23:35.537: E/AndroidRuntime(1740): FATAL EXCEPTION: main

05-26 16:23:35.537: E/AndroidRuntime(1740): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lunbo/com.example.lunbo.MainActivity}: java.lang.NullPointerException

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.app.ActivityThread.access$1500(ActivityThread.java:117)

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.os.Handler.dispatchMessage(Handler.java:99)

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.os.Looper.loop(Looper.java:130)

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.app.ActivityThread.main(ActivityThread.java:3683)

05-26 16:23:35.537: E/AndroidRuntime(1740): at java.lang.reflect.Method.invokeNative(Native Method)

05-26 16:23:35.537: E/AndroidRuntime(1740): at java.lang.reflect.Method.invoke(Method.java:507)

05-26 16:23:35.537: E/AndroidRuntime(1740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

05-26 16:23:35.537: E/AndroidRuntime(1740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

05-26 16:23:35.537: E/AndroidRuntime(1740): at dalvik.system.NativeStart.main(Native Method)

05-26 16:23:35.537: E/AndroidRuntime(1740): Caused by: java.lang.NullPointerException

05-26 16:23:35.537: E/AndroidRuntime(1740): at com.example.lunbo.MainActivity.onCreate(MainActivity.java:34)

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-26 16:23:35.537: E/AndroidRuntime(1740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

05-26 16:23:35.537: E/AndroidRuntime(1740): ... 11 more

05-26 16:24:05.617: D/AndroidRuntime(1777): Shutting down VM

05-26 16:24:05.617: W/dalvikvm(1777): threadid=1: thread exiting with uncaught exception (group=0xb5f7c4f0)

05-26 16:24:05.617: E/AndroidRuntime(1777): FATAL EXCEPTION: main

05-26 16:24:05.617: E/AndroidRuntime(1777): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lunbo/com.example.lunbo.MainActivity}: java.lang.NullPointerException

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.app.ActivityThread.access$1500(ActivityThread.java:117)

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.os.Handler.dispatchMessage(Handler.java:99)

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.os.Looper.loop(Looper.java:130)

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.app.ActivityThread.main(ActivityThread.java:3683)

05-26 16:24:05.617: E/AndroidRuntime(1777): at java.lang.reflect.Method.invokeNative(Native Method)

05-26 16:24:05.617: E/AndroidRuntime(1777): at java.lang.reflect.Method.invoke(Method.java:507)

05-26 16:24:05.617: E/AndroidRuntime(1777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

05-26 16:24:05.617: E/AndroidRuntime(1777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

05-26 16:24:05.617: E/AndroidRuntime(1777): at dalvik.system.NativeStart.main(Native Method)

05-26 16:24:05.617: E/AndroidRuntime(1777): Caused by: java.lang.NullPointerException

05-26 16:24:05.617: E/AndroidRuntime(1777): at com.example.lunbo.MainActivity.onCreate(MainActivity.java:34)

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-26 16:24:05.617: E/AndroidRuntime(1777): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

05-26 16:24:05.617: E/AndroidRuntime(1777): ... 11 more


 


import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.widget.ImageView;


public class MainActivity extends Activity {

private ImageView img;

private Handler handler;

private int[] images = { R.drawable.aa, R.drawable.dd, R.drawable.ff};

    private int index;

    private MyRunnble runnable= new MyRunnble();

    class MyRunnble implements Runnable{

   

    @Override

    public void run() {

    index++;

    index=index%3;

    img.setImageResource(images[index]);

    handler.postDelayed(runnable, 2000);

   

    }

   

    }

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


img = (ImageView) findViewById(R.id.img);

handler.postDelayed(runnable, 2000);

}


}

正在回答

1 回答

把handler实例化一下:Handler handler=new Handler()

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android面试常客Handler详解
  • 参与学习       54881    人
  • 解答问题       183    个

学Android开发,必学Handler,也是Android面试常客

进入课程

为什么汇报空指针异常

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信