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

两个方法都这样 帮我找找啥问题

02-22 15:16:42.752: W/dalvikvm(1918): threadid=1: thread exiting with uncaught exception (group=0xb608c4f0)

02-22 15:16:42.756: E/AndroidRuntime(1918): FATAL EXCEPTION: main

02-22 15:16:42.756: E/AndroidRuntime(1918): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.handler02/com.example.handler02.MainActivity}: java.lang.NullPointerException

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.app.ActivityThread.access$1500(ActivityThread.java:117)

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.os.Handler.dispatchMessage(Handler.java:99)

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.os.Looper.loop(Looper.java:130)

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.app.ActivityThread.main(ActivityThread.java:3683)

02-22 15:16:42.756: E/AndroidRuntime(1918): at java.lang.reflect.Method.invokeNative(Native Method)

02-22 15:16:42.756: E/AndroidRuntime(1918): at java.lang.reflect.Method.invoke(Method.java:507)

02-22 15:16:42.756: E/AndroidRuntime(1918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

02-22 15:16:42.756: E/AndroidRuntime(1918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

02-22 15:16:42.756: E/AndroidRuntime(1918): at dalvik.system.NativeStart.main(Native Method)

02-22 15:16:42.756: E/AndroidRuntime(1918): Caused by: java.lang.NullPointerException

02-22 15:16:42.756: E/AndroidRuntime(1918): at com.example.handler02.MainActivity.onCreate(MainActivity.java:39)

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

02-22 15:16:42.756: E/AndroidRuntime(1918): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

02-22 15:16:42.756: E/AndroidRuntime(1918): ... 11 more

-------------------------------------------------------------------------------------

package com.example.handler02;



import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView mtextview;
	private Handler mhandler;
	private ImageView mimageview;
	private int images[] = { R.drawable.image1, R.drawable.image2,
			R.drawable.image3, R.drawable.image4, };
	private int index;
	private MyRunnable myRunnable = new MyRunnable();

	class MyRunnable implements Runnable {

		@Override
		public void run() {
			index++;
			index = index % 4;
			mimageview.setImageResource(images[index]);
			mhandler.postDelayed(myRunnable, 1000);
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mtextview = (TextView) findViewById(R.id.textview);
		mimageview = (ImageView) findViewById(R.id.imageView1);
		mhandler.postDelayed(myRunnable, 1000);
		// new Thread(){
		// public void run(){
		// try {
		//
		// Thread.sleep(1000);
		// mhandler.post(new Runnable() {
		// public void run() {
		// mtextview.setText("update");
		// }
		// });
		//
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
		// }.start();

	}

}


正在回答

2 回答

是的,你没有实例化handler,报的空指针异常

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

找到里找到了

handler没有new handler

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

举报

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

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

进入课程

两个方法都这样 帮我找找啥问题

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