0
我正在寫一個簡單的手電筒應用程序,我希望能夠從一個視圖(flashOn)移動到另一個(flashOff)。我看到一篇關於使用FlipView的文章,這是最好的方法嗎? (我不想用於活動..)我很瞭解這一點,所以請糾正我!我不斷收到關於lightBtnOn.setOnClickListener(new ImageButton.OnClickListener(){
NullPointerException FlipView Android
這裏一個NullPointerException是活動類:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.no_torch);
ImageButton lightBtnOn = (ImageButton) findViewById(R.id.light_on);
ImageButton lightBtnOff = (ImageButton) findViewById(R.id.light_off);
final ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.viewflipper);
final Animation animFlipInNext = AnimationUtils.loadAnimation(this, R.anim.flipinnext);
final Animation animFlipOutNext = AnimationUtils.loadAnimation(this, R.anim.flipoutnext);
final Animation animFlipInPrevious = AnimationUtils.loadAnimation(this, R.anim.flipinprevious);
final Animation animFlipOutPrevious = AnimationUtils.loadAnimation(this, R.anim.flipoutprevious);
lightBtnOn.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
viewFlipper.setInAnimation(animFlipInNext);
viewFlipper.setOutAnimation(animFlipOutNext);
viewFlipper.showNext();
}});
lightBtnOff.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
viewFlipper.setInAnimation(animFlipInPrevious);
viewFlipper.setOutAnimation(animFlipOutPrevious);
viewFlipper.showPrevious();
}});
這裏是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<ViewFlipper
android:id="@+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoStart= "true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/light_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/light_on"
android:layout_centerInParent="true"/>
<!-- To be used With Strobe Light Feature
<SeekBar android:id="@+id/seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="0" /> -->
<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/light_on"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="@string/lightOff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/light_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/light_off"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/light_off"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="@string/lightOn" />
</RelativeLayout>
</ViewFlipper>
這裏是NullPointerException異常logcat的結果:
11-26 18:02:31.501: E/AndroidRuntime(22663): FATAL EXCEPTION: main
11-26 18:02:31.501: E/AndroidRuntime(22663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marshall.meadows182/com.marshall.meadows182.LightTorch}: java.lang.NullPointerException
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.os.Looper.loop(Looper.java:143)
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.app.ActivityThread.main(ActivityThread.java:4263)
11-26 18:02:31.501: E/AndroidRuntime(22663): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 18:02:31.501: E/AndroidRuntime(22663): at java.lang.reflect.Method.invoke(Method.java:507)
11-26 18:02:31.501: E/AndroidRuntime(22663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-26 18:02:31.501: E/AndroidRuntime(22663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-26 18:02:31.501: E/AndroidRuntime(22663): at dalvik.system.NativeStart.main(Native Method)
11-26 18:02:31.501: E/AndroidRuntime(22663): Caused by: java.lang.NullPointerException
11-26 18:02:31.501: E/AndroidRuntime(22663): at com.marshall.meadows182.LightTorch.onCreate(LightTorch.java:43)
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-26 18:02:31.501: E/AndroidRuntime(22663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
11-26 18:02:31.501: E/AndroidRuntime(22663): ... 11 more
我剛試過。同樣的迴應。我現在發佈堆棧跟蹤。 – comead
您是否嘗試清潔並重新制作? – dor506
是的,我已經清理過了。並且在id中有flash_on和flash_off的引用。唯一的是imagebutton是空的..但爲什麼.. – comead