在 Android 中同步两个水平滚动视图
我尝试遵循这篇文章 Synchronise ScrollView 滚动位置 - android 的建议,但遇到了问题。
背景 - 我想要一个具有水平和垂直滚动功能的表格布局,但我希望第一行和第一列始终存在 - 就像 Excel 的冻结窗格一样。不幸的是,Android java 编程似乎几乎不可能......让我很头疼。我已经设置了它,这样如果我可以同步这两个水平滚动视图,那么我将使表格按照我想要的方式工作。
我遵循了上面发布的链接的建议,我遇到的问题是这样的。 当我执行以下操作时,应用程序强制关闭
scrollView1.setScrollViewListener(this);
我认为问题可能是我声明 ObservableScrollViews 的方式。我没有使用 XML - 所有对象都是以编程方式创建的。我尝试使用
私有ObservableScrollView oScrollViewOne = new ObservableScrollView(this);
但这也会导致强制关闭。 (我可以创建一个常规的滚动视图,为其分配一个 ID,然后使用
scrollView1 = (ObservableScrollView) findViewById(ID); 其中 ID 是我给滚动视图的整数。
我想知道如何理解下面的注释以及如果我不使用 XML 布局如何使用它们。
我们应该在布局中指定这个新的 ObservableScrollView 类,而不是现有的 ScrollView 标签。
com.test.ObservableScrollView android:id="@+id/scrollview1" ...
使用显式代码实现同步两个水平滚动视图的任何其他建议 - 不仅仅是使用 OnTouchMotionEvent 或其他想法而不提供代码的建议,正如我之前所看到的。
这是错误代码
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): FATAL EXCEPTION: main
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.glen.apps.TeacherAidePro/com.glen.apps.TeacherAidePro.TeacherAidePro}: java.lang.NullPointerException
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.os.Looper.loop(Looper.java:144)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.main(ActivityThread.java:4937)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at java.lang.reflect.Method.invoke(Method.java:521)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at dalvik.system.NativeStart.main(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): Caused by: java.lang.NullPointerException
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.view.View.<init>(View.java:1810)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.view.View.<init>(View.java:1856)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.view.ViewGroup.<init>(ViewGroup.java:299)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.widget.FrameLayout.<init>(FrameLayout.java:83)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.widget.ScrollView.<init>(ScrollView.java:137)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.widget.ScrollView.<init>(ScrollView.java:133)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.widget.ScrollView.<init>(ScrollView.java:129)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at com.glen.apps.TeacherAidePro.ObservableScrollView.<init>(ObservableScrollView.java:12)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at com.glen.apps.TeacherAidePro.TeacherAidePro.<init>(TeacherAidePro.java:119)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at java.lang.Class.newInstanceImpl(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at java.lang.Class.newInstance(Class.java:1429)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.Instrumentation.newActivity(Instrumentation.java:1036)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): ... 11 more
这是第 12 行
1. package com.glen.apps.TeacherAidePro;
2.
3. import android.content.Context;
4. import android.util.AttributeSet;
5. import android.widget.ScrollView;
6.
7. public class ObservableScrollView extends ScrollView {
8.
9. private IScrollListener listener = null;
10.
11. public ObservableScrollView(Context context) {
12. super(context);
}
I tried following the advice from this post Synchronise ScrollView scroll positions - android but running into a problem.
Background - I want a table layout with both horizontal and vertical scrolling, but I want the first row and first column always present - like freeze pane with excel. Unfortunately it seems that is close to impossible with Android java programming...causing me such a headache. I have it set up so that if I can just synchronize these two horizontalscrollviews then I will have the table working the way I want.
I followed the advice from the posted link above and the problem I have is this.
The app force closes when I implement the
scrollView1.setScrollViewListener(this);
I think the problem may be the way I am declaring the ObservableScrollViews. I am not using XML - all objects are created programatically. I tried using
private ObservableScrollView oScrollViewOne = new ObservableScrollView (this);
but this is causing a force close as well. (Can I just create a regular scrollview, assign it an ID and then use
scrollView1 = (ObservableScrollView) findViewById(ID);
where ID is the integer number I gave the scrollview.
I'm wondering what to make of the comments below and how to use them if I am not using the XML layout.
And we should specify this new ObservableScrollView class in the layout, instead of the existing ScrollView tags.
com.test.ObservableScrollView
android:id="@+id/scrollview1"
...
Any other suggestions to implement synchronizing two horizontalscroll views, with the explicit code - not just a suggestion to use OnTouchMotionEvent or other idea without giving the code, as I've seen before.
Here's the error code
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): FATAL EXCEPTION: main
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.glen.apps.TeacherAidePro/com.glen.apps.TeacherAidePro.TeacherAidePro}: java.lang.NullPointerException
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.os.Looper.loop(Looper.java:144)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.main(ActivityThread.java:4937)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at java.lang.reflect.Method.invoke(Method.java:521)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at dalvik.system.NativeStart.main(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): Caused by: java.lang.NullPointerException
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.view.View.<init>(View.java:1810)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.view.View.<init>(View.java:1856)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.view.ViewGroup.<init>(ViewGroup.java:299)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.widget.FrameLayout.<init>(FrameLayout.java:83)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.widget.ScrollView.<init>(ScrollView.java:137)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.widget.ScrollView.<init>(ScrollView.java:133)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.widget.ScrollView.<init>(ScrollView.java:129)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at com.glen.apps.TeacherAidePro.ObservableScrollView.<init>(ObservableScrollView.java:12)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at com.glen.apps.TeacherAidePro.TeacherAidePro.<init>(TeacherAidePro.java:119)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at java.lang.Class.newInstanceImpl(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at java.lang.Class.newInstance(Class.java:1429)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.Instrumentation.newActivity(Instrumentation.java:1036)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): ... 11 more
Here's line 12
1. package com.glen.apps.TeacherAidePro;
2.
3. import android.content.Context;
4. import android.util.AttributeSet;
5. import android.widget.ScrollView;
6.
7. public class ObservableScrollView extends ScrollView {
8.
9. private IScrollListener listener = null;
10.
11. public ObservableScrollView(Context context) {
12. super(context);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误指向 ObservableScrollView 构造函数中的空指针。您能否单独发布构造函数以及第 12 行?
我在 XML 和自定义滚动视图交互方式中发现的一个可能的问题是,您的自定义滚动视图是一个内部类。有关如何在XML,或者将其移至外部类。我通常将它放在外部类中,但是如果您想将其保留为内部类,那么
我认为这不能解释您的空指针异常,所以请发布您的 ObservableScrollView 类。
编辑:
如果你坚持用 Java 做所有事情,这里有一个工作示例:
基本上,这会并排创建两个滚动视图,每个视图的权重为 1.0,并将它们放入 LinearLayout 中,布局上的总权重为 2.0,所以他们每个人都有一半的宽度。
不过,我强烈建议您习惯 XML,因为(在我看来)创建布局要容易得多。它还更容易发现错误,并且 XML 的嵌套形式使其更易于阅读。不管怎样,希望这件事能够解决。
The error points to a null pointer in a constructor in your ObservableScrollView. Could you please post the constructor, as well as line 12 on its own?
One possible issue I am seeing with the way your XML and custom scroll view interact is that your custom scroll view is an inner class. See this page for how to declare an inner class custom component in XML, or move it to an external class. I usually put it in an external class, but if you wanted to keep it as an inner class it would be something like
I don't think this explains your null pointer exception though, so please post your ObservableScrollView class.
EDIT:
If you insist on doing everything in Java, here is a working example:
Basically, this creates two scroll views side by side, with weights of 1.0 each, and puts them in a LinearLayout with total weight on the layout of 2.0, so they each get half the width.
However, I highly recommend getting used to XML as it is much, much easier (in my opinion) to create layouts. It is also easier to spot mistakes, and the nested form of XML makes it easier to read. Anyway, hope this clears things up.
这是我的解决方案在代码中的进一步解释:
我不确定这是一个完全好的答案,正如我之前提到的,您可以通过使用数组来改进这一点,而不是调用每个视图的方法来同步,int 他的情况只是 2另外,onFling 会被阻止而不是同步,如果您的滚动视图太长,这可能对用户不友好。最后,如果用户进行微小的滑动,仍然会出现一个小问题,但这种情况非常罕见。
This is my solution further explanation in the code:
Im not sure this is a completely good answers, well as I mentioned before you can improve this by using an array, insted of calling the method for each view to sync, int his case just 2. Also onFling is prevented instead of being sync, if your scroll view is too long, this could not be user friendly. And finally, there is still a litle problem if the user does a tiny fling, but its a very rare case.