return layout.getLineForOffset(selectionStart);
此行导致我的应用崩溃。
我用它来获取多行编辑文本中的当前光标行。像这样。
public int getCurrentCursorLine(EditText editText) {
int selectionStart = Selection.getSelectionStart(editText.getText());
Layout layout = editText.getLayout();
if (!(selectionStart == -1) && editText.hasFocus()) {
return layout.getLineForOffset(selectionStart);
}
return -1;
}
这是错误日志
10-18 21:07:28.711 24433-24433/com.example.tabstrial2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tabstrial2, PID: 24433
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getLineForOffset(int)' on a null object reference
at com.example.tabstrial2.TabFragment1.getCurrentCursorLine(TabFragment1.java:451)
at com.example.tabstrial2.TabFragment1$2.onTextChanged(TabFragment1.java:169)
at android.widget.TextView.sendOnTextChanged(TextView.java:7689)
at android.widget.TextView.setText(TextView.java:4070)
at android.widget.TextView.setText(TextView.java:3925)
at android.widget.EditText.setText(EditText.java:85)
at android.widget.TextView.setText(TextView.java:3900)
at android.widget.TextView.onRestoreInstanceState(TextView.java:3800)
at android.view.View.dispatchRestoreInstanceState(View.java:13754)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2894)
at android.view.View.restoreHierarchyState(View.java:13732)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1135)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:670)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:632)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:613)
at com.example.tabstrial2.MainActivity.onNavigationItemSelected(MainActivity.java:569)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:153)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:328)
at android.view.View.performClick(View.java:4783)
at android.view.View$PerformClick.run(View.java:19887)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5290)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
此外,我几次收到此错误,我认为可能与编辑文本视图相关的问题有关。
10-18 21:05:58.691 10607-24276/? E/AppsFlyer_3.0-804958145: failed to connect to t.appsflyer.com/127.0.0.1 (port 80) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
java.net.ConnectException: failed to connect to t.appsflyer.com/127.0.0.1 (port 80) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:238)
at libcore.io.IoBridge.connectErrno(IoBridge.java:171)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
at java.net.Socket.connect(Socket.java:882)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
at com.android.okhttp.Connection.connect(Connection.java:152)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
at com.appsflyer.AppsFlyerLib.a(SourceFile:1378)
at com.appsflyer.AppsFlyerLib.b(SourceFile:1348)
at com.appsflyer.AppsFlyerLib.a(SourceFile:32)
at com.appsflyer.AppsFlyerLib$SendToServerRunnable.run(SourceFile:1534)
at com.appsflyer.AppsFlyerLib.c(SourceFile:973)
at com.appsflyer.AppsFlyerLib.a(SourceFile:32)
at com.appsflyer.AppsFlyerLib$DataCollector.run(SourceFile:1479)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:223)
at libcore.io.IoBridge.connectErrno(IoBridge.java:171)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
at java.net.Socket.connect(Socket.java:882)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
at com.android.okhttp.Connection.connect(Connection.java:152)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
at com.appsflyer.AppsFlyerLib.a(SourceFile:1378)
at com.appsflyer.AppsFlyerLib.b(SourceFile:1348)
at com.appsflyer.AppsFlyerLib.a(SourceFile:32)
at com.appsflyer.AppsFlyerLib$SendToServerRunnable.run(SourceFile:1534)
at com.appsflyer.AppsFlyerLib.c(SourceFile:973)
at com.appsflyer.AppsFlyerLib.a(SourceFile:32)
at com.appsflyer.AppsFlyerLib$DataCollector.run(SourceFile:1479)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
同样重要的信息:我的应用程序有tabLayout,带有5个标签,每个标签都包含单独的片段。当我第一次打开应用程序并且第一次加载包含此代码的片段时,没有崩溃或错误,当我浏览选项卡时以及第二次返回该选项卡时应用程序崩溃。我在使用FragmentStatePagerAdapter
,如果这里有任何相关性的话。
答案 0 :(得分:0)
我也遇到了同样的问题,就我而言,TextView
或您的情况EditText
的布局似乎不是由android系统完全创建的,因此getLayout()
返回null
我通过添加Handler
并在onResume()
@Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getCurrentCursorLine(myEditText);
}
}, 300); // 300 msec delay
}