Android 4.0.3 (API 15) 及以下版本 TextView.getMaxLines() 的替代方法
Alternative method to TextView.getMaxLines() for Android 4.0.3 (API 15) and below
在 Ice Cream Sandwich Android 4.0.3 版本,
上使用 Texview 的 getMaxLines()
方法时,我遇到了以下崩溃日志
所以我得到的真正问题是,我的应用程序是 运行 较低的 sdk,它没有此 getMaxLines() 方法。
如何消除这个崩溃。我没有找到任何替代方法
java.lang.NoSuchMethodError: android.widget.TextView.getMaxLines
at com.text.mobile.new.adapters.ServiceListAdapter.onBindViewHolder(ServiceListAdapter.java:232)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
TextViewCompat
的 getMaxLines() 看起来是适合此问题的解决方案。
你也可以在TextViewCompat的SourceCode里看看,把你需要的函数复制过来
代码:
int maxLines = TextViewCompat.getMaxLines(yourtextView);
而不是:
int maxLines = yourtextView.getMaxLines();
我强烈推荐使用支持库和 TextViewCompat。
但为了完整起见,这里也是 TextViewCompatDonut 的 getMaxLines
的(当前)来源:
static int getMaxLines(TextView textView) {
if (!sMaxModeFieldFetched) {
sMaxModeField = retrieveField("mMaxMode");
sMaxModeFieldFetched = true;
}
if (sMaxModeField != null && retrieveIntFromField(sMaxModeField, textView) == LINES) {
// If the max mode is using lines, we can grab the maximum value
if (!sMaximumFieldFetched) {
sMaximumField = retrieveField("mMaximum");
sMaximumFieldFetched = true;
}
if (sMaximumField != null) {
return retrieveIntFromField(sMaximumField, textView);
}
}
return -1;
}
试试这个,可能会有帮助,
TextView t1 = (TextView) findViewById(R.id.txt1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
int no= t1.getMaxLines();
Log.e("No of Lines: ", String.valueOf(no));
}
在 Ice Cream Sandwich Android 4.0.3 版本,
上使用 Texview 的getMaxLines()
方法时,我遇到了以下崩溃日志
所以我得到的真正问题是,我的应用程序是 运行 较低的 sdk,它没有此 getMaxLines() 方法。
如何消除这个崩溃。我没有找到任何替代方法
java.lang.NoSuchMethodError: android.widget.TextView.getMaxLines
at com.text.mobile.new.adapters.ServiceListAdapter.onBindViewHolder(ServiceListAdapter.java:232)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
TextViewCompat
的 getMaxLines() 看起来是适合此问题的解决方案。
你也可以在TextViewCompat的SourceCode里看看,把你需要的函数复制过来
代码:
int maxLines = TextViewCompat.getMaxLines(yourtextView);
而不是:
int maxLines = yourtextView.getMaxLines();
我强烈推荐使用支持库和 TextViewCompat。
但为了完整起见,这里也是 TextViewCompatDonut 的 getMaxLines
的(当前)来源:
static int getMaxLines(TextView textView) {
if (!sMaxModeFieldFetched) {
sMaxModeField = retrieveField("mMaxMode");
sMaxModeFieldFetched = true;
}
if (sMaxModeField != null && retrieveIntFromField(sMaxModeField, textView) == LINES) {
// If the max mode is using lines, we can grab the maximum value
if (!sMaximumFieldFetched) {
sMaximumField = retrieveField("mMaximum");
sMaximumFieldFetched = true;
}
if (sMaximumField != null) {
return retrieveIntFromField(sMaximumField, textView);
}
}
return -1;
}
试试这个,可能会有帮助,
TextView t1 = (TextView) findViewById(R.id.txt1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
int no= t1.getMaxLines();
Log.e("No of Lines: ", String.valueOf(no));
}