ViewSwitcher NullPointerException

时间:2011-07-22 06:43:20

标签: android android-layout android-widget

我正在尝试根据 ViewFlipper 中哪个孩子目前有焦点而在 TextSwitcher 中设置我的文字,并且我不断获得空指针 。 这可能重要,也可能不重要,但我在一个活动中有三个文本切换器,每个活动都有一个不同的视图工厂。

public void onSwipe(int direction) 
    {

        switch(direction)
        {

        case SimpleGestureFilter.SWIPE_LEFT :
            UpdateTextSwitcher();
         if(flipper.getChildAt(flipper.getChildCount()-1).hasFocus())
         {   flipper.stopFlipping();
         Log.i("ARRAY_OnCreate", "True focus 1" );
         }
             else {
             flipper.setInAnimation(inFromRightAnimation());
             flipper.setOutAnimation(outToLeftAnimation());
             flipper.showNext();     
             }
         break;

        case SimpleGestureFilter.SWIPE_RIGHT :
         if(flipper.getChildAt(0).hasFocus())
         {
            flipper.stopFlipping();
         }

             else{
             flipper.setInAnimation(inFromLeftAnimation());
             flipper.setOutAnimation(outToRightAnimation());
             flipper.showPrevious();  
             }
         break;
        case SimpleGestureFilter.SWIPE_DOWN :

         default:

             break;
      }
}

private void UpdateTextSwitcher()
{
    if (flipper.getChildAt(0).hasFocus())
    {
        lSwitcher.setText("R.string.s1");
        mSwitcher.setText("R.string.s2");
        rSwitcher.setText("R.string.s3");
    }
    else 
    {
        lSwitcher.removeAllViews();
        mSwitcher.setText("R.string.s2");
        rSwitcher.setText("R.string.s1");
    }
}

logcat显示

07-22 00:47:48.812: ERROR/AndroidRuntime(11141): FATAL EXCEPTION: main
07-22 00:47:48.812: ERROR/AndroidRuntime(11141): java.lang.NullPointerException
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at android.widget.TextSwitcher.setText(TextSwitcher.java:78)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at com.tmm.android.rssreader.RssActivity.UpdateTextSwitcher(RssActivity.java:134)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at com.tmm.android.rssreader.RssActivity.onSwipe(RssActivity.java:278)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at com.tmm.android.rssreader.SimpleGestureFilter.onFling(SimpleGestureFilter.java:119)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:568)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at com.tmm.android.rssreader.SimpleGestureFilter.onTouchEvent(SimpleGestureFilter.java:47)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at com.tmm.android.rssreader.RssActivity.dispatchTouchEvent(RssActivity.java:224)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at android.os.Looper.loop(Looper.java:130)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at java.lang.reflect.Method.invoke(Method.java:507)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    07-22 00:47:48.812: ERROR/AndroidRuntime(11141):     at        dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

为什么选择mSwitcher.setText("R.string.s2");?不应只是从资源mSwitcher.setText(getResources().getString(R.string.s2));加载字符串因为如果你设置R.string.s2没有引号.setText()将不接受(int),因为需要CharSeq。

P.S:您在break;声明中不需要defaultbreak

中需要case SimpleGestureFilter.SWIPE_DOWN :