0

我正在尝试在按钮单击侦听器上滑动图像。我正在为此采取新的布局。每个布局都包含不同的全屏图像。单击按钮时,应显示下一个布局。这是我的代码。

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" />

4

3 回答 3

0

这是错误的

您不得使用设置内容视图进行图像切换,

当您 setContentView 另一个布局时,按钮将为空

所有人都使用它...

<?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">

<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" />
</RelativeLayout>

您可以对 MainActivity 使用类似的东西

public class MainActivity extends Activity {


int[] pic_num = {R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3};
int i = 1;

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

    final ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            iv.setImageResource(pic_num[i+1]);
            i ++;
            if(i == 3){
                i = 1;
            }
        }
    });
}

}
于 2013-10-25T07:32:57.883 回答
0

尝试这个..

activity_image_slider.xml 没有这个,Button image1btnImageButton imageView1就是 NPE 来的原因

利用ViewPager

https://github.com/chiuki/android-swipe-image-viewer

于 2013-10-25T07:34:18.407 回答
0

我认为您首先不匹配布局。设置setContentView(R.layout.main_activity)而不是setContentView(R.layout.activity_image_slider)

super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity) //respective layout here
b = (Button)findViewById(R.id.image);

由于布局不匹配,因此缺少相应布局的 id 并通过 NPE

于 2013-10-25T07:45:40.073 回答