Я пишу простое приложение для фонарика, и я хочу иметь возможность переходить от одного представления (flashOn) к другому (flashOff). Я видел сообщение об использовании FlipView, это лучший способ? (Я не хочу использовать для деятельности ..) Я очень хорошо знал это, поэтому, пожалуйста, поправьте меня! Я получаю исключение NullPointerException на lightBtnOn.setOnClickListener(new ImageButton.OnClickListener(){
Вот класс деятельности:
@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