Settings: Fix illegal state exception on StorageSelectionPreferenceController
Log: time: 1756667643368 msg: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView stacktrace: java.lang.IllegalStateException: Default drop down view should be a TextView, at com.android.settings.deviceinfo.storage.StorageSelectionPreferenceController$StorageAdapter.getDropDownView(StorageSelectionPreferenceController.java:150) at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:1032) at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:1028) at android.widget.Spinner.measureContentWidth(Spinner.java:889) at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1265) at android.widget.Spinner$DropdownPopup.show(Spinner.java:1291) at android.widget.Spinner.performClick(Spinner.java:797) at android.view.View.performClickInternal(View.java:8060) at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0) at android.view.View$PerformClick.run(View.java:31554) at android.os.Handler.handleCallback(Handler.java:995) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loopOnce(Looper.java:248) at android.os.Looper.loop(Looper.java:338) at android.app.ActivityThread.main(ActivityThread.java:9068) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:932) Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView at com.android.settings.deviceinfo.storage.StorageSelectionPreferenceController$StorageAdapter.getDropDownView(StorageSelectionPreferenceController.java:148) ... 17 more Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
This commit is contained in:
@@ -70,7 +70,7 @@ public class StorageSelectionPreferenceController extends BasePreferenceControll
|
||||
if (storageEntries == null || storageEntries.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Collections.sort(mStorageEntries);
|
||||
Collections.sort(storageEntries);
|
||||
mStorageEntries.addAll(storageEntries);
|
||||
mStorageAdapter.addAll(storageEntries);
|
||||
|
||||
@@ -126,15 +126,9 @@ public class StorageSelectionPreferenceController extends BasePreferenceControll
|
||||
if (view == null) {
|
||||
view = getDefaultView(position, view, parent);
|
||||
}
|
||||
|
||||
TextView textView = null;
|
||||
try {
|
||||
textView = (TextView) view;
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalStateException("Default view should be a TextView, ", e);
|
||||
}
|
||||
TextView textView = extractTextView(view);
|
||||
textView.setText(getItem(position).getDescription());
|
||||
return textView;
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -142,16 +136,42 @@ public class StorageSelectionPreferenceController extends BasePreferenceControll
|
||||
if (view == null) {
|
||||
view = getDefaultDropDownView(position, view, parent);
|
||||
}
|
||||
|
||||
TextView textView = null;
|
||||
try {
|
||||
textView = (TextView) view;
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalStateException("Default drop down view should be a TextView, ", e);
|
||||
}
|
||||
TextView textView = extractTextView(view);
|
||||
textView.setText(getItem(position).getDescription());
|
||||
return textView;
|
||||
return view;
|
||||
}
|
||||
|
||||
private TextView extractTextView(View root) {
|
||||
if (root instanceof TextView) {
|
||||
return (TextView) root;
|
||||
}
|
||||
// Try common ids first
|
||||
int titleId = root.getResources().getIdentifier(
|
||||
"title", "id", "com.android.settingslib");
|
||||
TextView tv = titleId != 0 ? root.findViewById(titleId) : null;
|
||||
if (tv == null) {
|
||||
tv = root.findViewById(android.R.id.text1);
|
||||
}
|
||||
if (tv == null) {
|
||||
// Fallback: recursively find the first TextView
|
||||
tv = findFirstTextView(root);
|
||||
}
|
||||
if (tv == null) {
|
||||
throw new IllegalStateException("Spinner item view must contain a TextView");
|
||||
}
|
||||
return tv;
|
||||
}
|
||||
|
||||
private TextView findFirstTextView(View v) {
|
||||
if (v instanceof TextView) return (TextView) v;
|
||||
if (v instanceof ViewGroup) {
|
||||
ViewGroup vg = (ViewGroup) v;
|
||||
for (int i = 0; i < vg.getChildCount(); i++) {
|
||||
TextView tv = findFirstTextView(vg.getChildAt(i));
|
||||
if (tv != null) return tv;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user