我有一個TextView
,我想得到一些特定的行文本,但是當我使用txt.getLayout.getLineStart(1)
時,它返回null並崩潰應用程序,原因是什麼?getLineStart()總是爲我的TextView返回null?
TextView txt = (TextView)mRootView.findViewById(R.id.txt);
int start = txt.getLayout().getLineStart(1);
int end = txt.getLayout().getLineEnd(1);
String first = txt.getText().subSequence(start,end).toString();
崩潰日誌
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sattva.animalsencyclopedia, PID: 11244
java.lang.NullPointerException
at com.sattva.animalsencyclopedia.fragments.FragmentInformation.onViewCreated(FragmentInformation.java:110)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
顯示代碼,請 –
@IntelliJAmiya我已經編輯後 – DavidOli
簡單地檢查什麼'txt.getLayout()'返回... – pskink