2011-11-26 2 views
0

J'écris une application simple lampe de poche et je veux être en mesure de passer d'une vue (flashOn) à l'autre (flashOff). J'ai vu un article sur l'utilisation d'un FlipView, est-ce le meilleur moyen? (Je ne veux pas utiliser pour des activités ..) Je suis très au courant de cela, alors s'il vous plaît corrigez-moi! Je continue de recevoir un NullPointerException sur lightBtnOn.setOnClickListener(new ImageButton.OnClickListener(){NullPointerException FlipView Android

Voici la classe d'activité:

@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(); 
}}); 

Voici mon 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> 

Voici le NullPointerException LogCat Résultat:

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 

Répondre

0

essayez "nouveau OnClickListener" au lieu de "nouveau ImageButton.OnClickListener"

+0

Je viens d'essayer cela. Même réponse Je poste le Stack Trace maintenant. – comead

+0

avez-vous essayé de nettoyer et de construire à nouveau? – dor506

+0

oui, j'ai déjà nettoyé. et il y a une référence pour flash_on et flash_off dans id. La seule chose est l'imagebutton est null .. mais pourquoi .. – comead