NullPointerException FlipView Android

时间:2011-11-26 22:51:05

标签: android xml layout view nullpointerexception

我正在编写一个简单的手电筒应用程序,我希望能够从一个视图(flashOn)移动到另一个视图(flashOff)。我看到一篇关于使用FlipView的帖子,这是最好的方法吗? (我不想用于活动..)我对此非常了解,所以请指正!我在lightBtnOn.setOnClickListener(new ImageButton.OnClickListener(){

上不断收到NullPointerException

这是Activity类:

@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

1 个答案:

答案 0 :(得分:0)

尝试“new OnClickListener”而不是“new ImageButton.OnClickListener”