跟着视频打代码,结果出现xx已经停止,求大神指导怎么修改。
这是我的mainactivity
package com.example.handler_012;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
private Handler handler1=new Handler();
private ImageView imv;
private MyRunnable myRunnable=new MyRunnable();
private int images[]={R.drawable.image_1,R.drawable.image_2,R.drawable.image_3};
private int i;
class MyRunnable implements Runnable{
public void run(){
i++;
i=i%3;
imv.setImageResource(images[i]);
handler1.postDelayed(myRunnable, 1000);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textview);
imv=(ImageView) findViewById(R.id.imageView1);
// handler1=new Handler();
handler1.postDelayed(myRunnable, 1000);
// new Thread(){
// public void run() {
// try {
// Thread.sleep(1000);
// handler1.post(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// tv.setText("update thread");
// }
// });
//
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// };
// }.start();
}
}
这是activity.xml文档:
<RelativeLayout 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="com.example.handler_012.MainActivity" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:layout_marginTop="132dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>