Android ViewFlipper导致空指针异常
我有一个使用view flipper的程序,当调用activity时,我得到一个Android ViewFlipper导致空指针异常,android,nullpointerexception,Android,Nullpointerexception,我有一个使用view flipper的程序,当调用activity时,我得到一个NullPointerException,如下所示。只要活动正在运行,我希望循环浏览一组图像 06-13 18:52:05.358: E/AndroidRuntime(368): Caused by: java.lang.NullPointerException 活动: import android.app.Activity; import android.os.Bundle; import android.os.
NullPointerException
,如下所示。只要活动正在运行,我希望循环浏览一组图像
06-13 18:52:05.358: E/AndroidRuntime(368): Caused by: java.lang.NullPointerException
活动:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class MainMenuActivity extends Activity{
Handler handler;
Runnable runnable;
ViewFlipper imageSwitcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menupage);
imageSwitcher= (ViewFlipper) new ViewFlipper(this).findViewById(R.id.sportspersons);
this.viewchanger();
}
public void viewchanger()
{
imageSwitcher.setInAnimation(this, R.anim.fadein);
imageSwitcher.setOutAnimation(this, R.anim.fadeout);
ImageView i = new ImageView(this);
i.setBackgroundDrawable(getResources().getDrawable(R.drawable.jowens));
ImageView i2 = new ImageView(this);
i2.setBackgroundDrawable(getResources ().getDrawable(R.drawable.maradonna));
ImageView i3 = new ImageView(this);
i2.setBackgroundDrawable(getResources().getDrawable(R.drawable.mjordan));
ImageView i4 = new ImageView(this);
i2.setBackgroundDrawable(getResources().getDrawable(R.drawable.pele));
imageSwitcher.addView(i);
imageSwitcher.addView(i2);
imageSwitcher.addView(i3);
imageSwitcher.addView(i4);
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable, 3000);
imageSwitcher.showNext();
}
};
handler.postDelayed(runnable, 500);
}
}
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="508dp"
android:layout_x="-4dp"
android:layout_y="-5dp"
android:scaleType="fitXY"
android:src="@drawable/mainmenu01" />
<ViewFlipper
android:id="@+id/sportspersons"
android:layout_width="86dp"
android:layout_height="91dp"
android:layout_x="38dp"
android:layout_y="373dp" />
</AbsoluteLayout>
XML:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class MainMenuActivity extends Activity{
Handler handler;
Runnable runnable;
ViewFlipper imageSwitcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menupage);
imageSwitcher= (ViewFlipper) new ViewFlipper(this).findViewById(R.id.sportspersons);
this.viewchanger();
}
public void viewchanger()
{
imageSwitcher.setInAnimation(this, R.anim.fadein);
imageSwitcher.setOutAnimation(this, R.anim.fadeout);
ImageView i = new ImageView(this);
i.setBackgroundDrawable(getResources().getDrawable(R.drawable.jowens));
ImageView i2 = new ImageView(this);
i2.setBackgroundDrawable(getResources ().getDrawable(R.drawable.maradonna));
ImageView i3 = new ImageView(this);
i2.setBackgroundDrawable(getResources().getDrawable(R.drawable.mjordan));
ImageView i4 = new ImageView(this);
i2.setBackgroundDrawable(getResources().getDrawable(R.drawable.pele));
imageSwitcher.addView(i);
imageSwitcher.addView(i2);
imageSwitcher.addView(i3);
imageSwitcher.addView(i4);
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable, 3000);
imageSwitcher.showNext();
}
};
handler.postDelayed(runnable, 500);
}
}
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="508dp"
android:layout_x="-4dp"
android:layout_y="-5dp"
android:scaleType="fitXY"
android:src="@drawable/mainmenu01" />
<ViewFlipper
android:id="@+id/sportspersons"
android:layout_width="86dp"
android:layout_height="91dp"
android:layout_x="38dp"
android:layout_y="373dp" />
</AbsoluteLayout>
更改
imageSwitcher= (ViewFlipper) new ViewFlipper(this).findViewById(R.id.sportspersons);
到
添加到Waqas点
您在下面的代码中遇到了严重的问题
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable, 3000);
imageSwitcher.showNext();
}
};
handler.postDelayed(runnable, 500);
您可以这样尝试:
runnable=新的runnable(){
仅供参考,AbsoluteLayout已被弃用。是的,我删除了该部分。您知道如何使用viewflipper自动循环浏览吗