我正在尝试在按钮单击侦听器上滑动图像。我正在采取新的布局。每个布局包含不同的全屏图像。在单击按钮时,应显示下一个布局。这是我的代码。
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" />
答案 0 :(得分:0)
这是错误的
您不得使用设置内容视图进行图像切换,
当你将setContentView设置为另一个布局时,按钮将为null
将它用于所有人......
<?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;
}
}
});
}
}
答案 1 :(得分:0)
试试这个..
在activity_image_slider.xml
中没有Button
image1btn
和ImageButton
imageView1
这就是为什么NPE来了
使用ViewPager
答案 2 :(得分: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