为什么代码出错了啊
这是我的主文件
package com.example.viewfilpper;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private ViewFlipper flipper;
//将资源设置在一个数组当中
private int[]resID={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化
flipper=(ViewFlipper) findViewById(R.layout.main);
//用动态导入的方式为viewflipper加入子view
for (int i = 0; i < resID.length; i++) {
flipper.addView(getImageView(resID[i]));
}
//添加动画效果
flipper.setInAnimation(this, R.anim.left_in);
flipper.setOutAnimation(this, R.anim.left_out);
//设置视图切换时间
flipper.setFlipInterval(1000);
flipper.startFlipping();
}
private ImageView getImageView(int resID){
ImageView image=new ImageView(this);
// image.setImageResource(resID);
image.setBackgroundResource(resID);//显示铺面屏幕的照片
return image;
}
}这是我的main函数
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ViewFlipper android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/flipper" ></ViewFlipper> </LinearLayout>
这是log日志
08-06 11:14:07.668: D/dalvikvm(5486): GC_FOR_ALLOC freed 100K, 5% free 3059K/3220K, paused 8ms, total 8ms
08-06 11:14:07.672: I/dalvikvm-heap(5486): Grow heap (frag case) to 3.504MB for 486012-byte allocation
08-06 11:14:07.692: D/dalvikvm(5486): GC_FOR_ALLOC freed 3K, 5% free 3531K/3696K, paused 18ms, total 18ms
08-06 11:14:07.696: D/AndroidRuntime(5486): Shutting down VM
08-06 11:14:07.696: W/dalvikvm(5486): threadid=1: thread exiting with uncaught exception (group=0x94d6fb20)
08-06 11:14:07.712: E/AndroidRuntime(5486): FATAL EXCEPTION: main
08-06 11:14:07.712: E/AndroidRuntime(5486): Process: com.example.viewfilpper, PID: 5486
08-06 11:14:07.712: E/AndroidRuntime(5486): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewfilpper/com.example.viewfilpper.MainActivity}: java.lang.NullPointerException
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.os.Handler.dispatchMessage(Handler.java:102)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.os.Looper.loop(Looper.java:136)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.app.ActivityThread.main(ActivityThread.java:5045)
08-06 11:14:07.712: E/AndroidRuntime(5486): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 11:14:07.712: E/AndroidRuntime(5486): at java.lang.reflect.Method.invoke(Method.java:515)
08-06 11:14:07.712: E/AndroidRuntime(5486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-06 11:14:07.712: E/AndroidRuntime(5486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-06 11:14:07.712: E/AndroidRuntime(5486): at dalvik.system.NativeStart.main(Native Method)
08-06 11:14:07.712: E/AndroidRuntime(5486): Caused by: java.lang.NullPointerException
08-06 11:14:07.712: E/AndroidRuntime(5486): at com.example.viewfilpper.MainActivity.onCreate(MainActivity.java:34)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.app.Activity.performCreate(Activity.java:5231)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
08-06 11:14:07.712: E/AndroidRuntime(5486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
08-06 11:14:07.712: E/AndroidRuntime(5486): ... 11 more问题出在哪儿呢?谢谢