为什么在对 TextSwitcher 使用 setCurrentText 或 setText 时出现 NullPointerException?

发布于 2024-11-09 03:43:58 字数 7288 浏览 0 评论 0原文

TextSwitcherListView 行的一部分(在 xml 中定义)。 我在 setViewValue 中定义了 TextSwitcher 的文本:

switch(viewId) {
case R.id.tw_text:
    TextSwitcher twText = (TextSwitcher) view;
    twText.setCurrentText("test text");
    return true;

如果 twTextTextView,则此代码可以正常工作。但它返回 NullPointerException 并带有 TextSwitcher

05-22 10:24:08.781: ERROR/AndroidRuntime(3348): FATAL EXCEPTION: main
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): java.lang.NullPointerException
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.TextSwitcher.setCurrentText(TextSwitcher.java:89)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at my.package.MainActivity$1.setViewValue(MainActivity.java:87)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:126)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.ListView.onMeasure(ListView.java:1109)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.os.Looper.loop(Looper.java:123)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at java.lang.reflect.Method.invoke(Method.java:521)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at dalvik.system.NativeStart.main(Native Method)

更新 1看起来我需要将 setFactory() 添加到我的代码中。 但如果我执行以下操作:

  TextSwitcher twText = (TextSwitcher) view;
  twText.setFactory(new ViewSwitcher.ViewFactory() {
      @Override
      public View makeView() {
      TextView t = new TextView(MainActivity.this);
      return t;
      }
      });

我收到另一个错误:

05-22 10:54:42.852: ERROR/AndroidRuntime(3907): java.lang.IllegalStateException: Can't add more than 2 views to a ViewSwitcher
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.widget.ViewSwitcher.addView(ViewSwitcher.java:64)
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.widget.TextSwitcher.addView(TextSwitcher.java:67)
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.view.ViewGroup.addView(ViewGroup.java:1845)

更新 2:我在 twText.setFactory() 之前添加了 twText.removeAllViews()代码>.它有效,但看起来不是再次删除和添加视图的最佳方法。如何检查视图是否已经存在?

另外,我想在 xml 中保留 TextSwitcher 的格式/样式。 setFactory 的使用不允许我这样做。我能做些什么?

TextSwitcher is part of ListView row (defined in xml).
I define the text of TextSwitcher in the setViewValue:

switch(viewId) {
case R.id.tw_text:
    TextSwitcher twText = (TextSwitcher) view;
    twText.setCurrentText("test text");
    return true;

This code works perfectly if twText is TextView. But it returns NullPointerException with TextSwitcher:

05-22 10:24:08.781: ERROR/AndroidRuntime(3348): FATAL EXCEPTION: main
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): java.lang.NullPointerException
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.TextSwitcher.setCurrentText(TextSwitcher.java:89)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at my.package.MainActivity$1.setViewValue(MainActivity.java:87)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:126)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.ListView.onMeasure(ListView.java:1109)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.os.Looper.loop(Looper.java:123)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at java.lang.reflect.Method.invoke(Method.java:521)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at dalvik.system.NativeStart.main(Native Method)

Update 1: looks like I need to add setFactory() to my code.
But if I do the following:

  TextSwitcher twText = (TextSwitcher) view;
  twText.setFactory(new ViewSwitcher.ViewFactory() {
      @Override
      public View makeView() {
      TextView t = new TextView(MainActivity.this);
      return t;
      }
      });

I am getting another error:

05-22 10:54:42.852: ERROR/AndroidRuntime(3907): java.lang.IllegalStateException: Can't add more than 2 views to a ViewSwitcher
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.widget.ViewSwitcher.addView(ViewSwitcher.java:64)
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.widget.TextSwitcher.addView(TextSwitcher.java:67)
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.view.ViewGroup.addView(ViewGroup.java:1845)

Update 2: I've added twText.removeAllViews() before twText.setFactory(). It works, but looks like not the best way to delete and add Views again. How can I check if Views are already there?

Also, I would like to keep formatting/style of my TextSwitcher in the xml. Usage of setFactory doesn't allow me to do so. What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面犯桃花 2024-11-16 03:43:58

看起来在调用 setCurrentText 时没有将任何视图添加到文本切换器中。此方法基本上只是获取当前文本视图并为其设置文本,因此如果出现 NullPointerException ,则可能仅由于文本切换器中缺少视图而发生。

关于工厂的错误:您必须小心设置工厂或自己添加两个视图。您可以选择以下任一选项:

  • 调用 setFactory 这将创建两个视图。
  • 通过调用 addView 手动添加两个视图。

在这两种情况下,只能有两种观点,不多也不少。如果你设置了一个工厂并且已经添加了一些视图,你将会得到一个错误。您可以通过检查 getChildCount() 方法返回的内容来检查是否需要设置工厂:

TextSwitcher switcher = (TextSwitcher) view;
if (switcher.getChildCount() != 2) {
    switcher.removeAllViews();
    switcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            return new TextView(MainActivity.this);
        }
    });
}

It looks like no views have been added to the text switcher at the point when setCurrentText is called. This method basically just gets current text view and sets the text for it, so if a NullPointerException appears, it may only happen due to the absence of the views in the text switcher.

Regarding the error with the factory: you have to be careful to either set a factory or to add two views yourself. You have either of these options:

  • Call setFactory which will create two Views.
  • Add two views manually by calling addView.

In both cases there can be only two views, no more no less. If you set a factory and some views have already been added, you will get an error. You can check if you need to set a factory by checking what getChildCount() method returns:

TextSwitcher switcher = (TextSwitcher) view;
if (switcher.getChildCount() != 2) {
    switcher.removeAllViews();
    switcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            return new TextView(MainActivity.this);
        }
    });
}
懷念過去 2024-11-16 03:43:58

可能有点太晚了,但我在寻找我的答案时发现了你的问题。当尝试调用 setFactory 时,我在空 TextSwitcher 中遇到类似的错误,无法添加超过 2 个视图...。当我将活动更改为扩展 Activity 而不是 ActionBarActivity 时,我的问题得到了解决。希望这对未来的一些人有所帮助。

TextSwitcher 抛出“超过 2 个视图”错误setFactory() 被调用

Probably a little too late on this but I found your question while I was searching for an answer to mine. I was getting a similar error, can't add more than 2 views..., in an empty TextSwitcher when trying to call setFactory. My issue was resolved when I changed my activity to extending Activity instead of ActionBarActivity. Hopefully this will help some people in the future.

TextSwitcher throwing "more than 2 views" error when setFactory() is called

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文