我正在尝试实现Marquee HorizontalScrollView但是我在第55行收到一条错误消息,指出FATAL EXCEPTION: main Unable to start activity java.lang.NullPointerException
:
s.fullScroll(HorizontalScrollView.FOCUS_LEFT);
我不确定为什么会发生这种情况。我用XML创建/声明了HorizontalScrollView:
<HorizontalScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:scrollbars="none" >
并尝试使用以下方式开始滚动过程:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HorizontalScrollView s =(HorizontalScrollView) findViewById(R.id.sc);
s.fullScroll(HorizontalScrollView.FOCUS_LEFT);
我使用这个例子得到了这个:
HorizontalScrollView: auto-scroll to end when new Views are added?
但是我不确定为什么我会得到这个致命的例外/ NPE。
建议/输入表示赞赏!
04-07 15:11:23.931: D/AndroidRuntime(11307): Shutting down VM
04-07 15:11:23.931: W/dalvikvm(11307): threadid=1: thread exiting with uncaught exception (group=0x41968ba8)
04-07 15:11:23.931: E/AndroidRuntime(11307): FATAL EXCEPTION: main
04-07 15:11:23.931: E/AndroidRuntime(11307): Process: com.test.app, PID: 11307
04-07 15:11:23.931: E/AndroidRuntime(11307): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.app/com.test.app.Cast}: java.lang.NullPointerException
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.os.Looper.loop(Looper.java:136)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 15:11:23.931: E/AndroidRuntime(11307): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 15:11:23.931: E/AndroidRuntime(11307): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 15:11:23.931: E/AndroidRuntime(11307): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 15:11:23.931: E/AndroidRuntime(11307): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 15:11:23.931: E/AndroidRuntime(11307): at dalvik.system.NativeStart.main(Native Method)
04-07 15:11:23.931: E/AndroidRuntime(11307): Caused by: java.lang.NullPointerException
04-07 15:11:23.931: E/AndroidRuntime(11307): at com.test.app.Cast.onCreate(Cast.java:55)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.app.Activity.performCreate(Activity.java:5231)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 15:11:23.931: E/AndroidRuntime(11307): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 15:11:23.931: E/AndroidRuntime(11307): ... 11 more
答案 0 :(得分:0)
您在设置内容视图之前尝试查找视图。在找到视图之前添加此行。
setContentView(R.layout.your_xml_name);