我正在尝试在按钮单击侦听器上滑动图像。我正在为此采取新的布局。每个布局都包含不同的全屏图像。单击按钮时,应显示下一个布局。这是我的代码。
public class ImageSlider extends Activity {
Button b, c, d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_slider);
b = (Button)findViewById(R.id.image);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.image1);
}
});
c = (Button)findViewById(R.id.image1btn);
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.image2);
}
});
d = (Button)findViewById(R.id.image2btn);
d.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_image_slider);
}
});
}
但是即使在正确初始化所有按钮之后,我也会收到 Null 指针异常。这是错误日志。请帮忙。
10-25 12:49:07.813: E/AndroidRuntime(13489): FATAL EXCEPTION: main
10-25 12:49:07.813: E/AndroidRuntime(13489): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fouractivities/com.example.fouractivities.ImageSlider}: java.lang.NullPointerException
10-25 12:49:07.813: E/AndroidRuntime(13489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-25 12:49:07.813: E/AndroidRuntime(13489):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-25 12:49:07.813: E/AndroidRuntime(13489):at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-25 12:49:07.813: E/AndroidRuntime(13489):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-25 12:49:07.813: E/AndroidRuntime(13489):at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 12:49:07.813: E/AndroidRuntime(13489):at android.os.Looper.loop(Looper.java:137)
10-25 12:49:07.813: E/AndroidRuntime(13489):at android.app.ActivityThread.main(ActivityThread.java:4424)
10-25 12:49:07.813: E/AndroidRuntime(13489):at java.lang.reflect.Method.invokeNative(Native Method)
10-25 12:49:07.813: E/AndroidRuntime(13489):at java.lang.reflect.Method.invoke(Method.java:511)
10-25 12:49:07.813: E/AndroidRuntime(13489):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-25 12:49:07.813: E/AndroidRuntime(13489):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-25 12:49:07.813: E/AndroidRuntime(13489):at dalvik.system.NativeStart.main(Native Method)
主要活动的 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=".ImageSlider" >
<Button
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="185dp"
android:text="@string/image" />
第一个图像的 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/a" />
<Button
android:id="@+id/image1btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Next" />