只有当我尝试强制布局显示带有递增和递减按钮的选择器时才会出现问题。因此,当我在我的片段中使用此代码并按“+”或“ - ”时,应用程序崩溃:
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.cust_dialog);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
如果我对上面的代码行发表评论,那么一切正常 这是cust_dialog:
<style name="cust_dialog" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>
这是堆栈跟踪:
09-06 01:15:35.947: E/AndroidRuntime(19858): FATAL EXCEPTION: main
09-06 01:15:35.947: E/AndroidRuntime(19858): java.lang.NullPointerException
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.widget.TimePicker.updateInputState(TimePicker.java:580)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.widget.TimePicker.access$000(TimePicker.java:56)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.widget.TimePicker$2.onValueChange(TimePicker.java:150)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.widget.NumberPicker.notifyChange(NumberPicker.java:1776)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.widget.NumberPicker.setValueInternal(NumberPicker.java:1562)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.widget.NumberPicker.changeValueByOne(NumberPicker.java:1592)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.widget.NumberPicker.access$200(NumberPicker.java:90)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.widget.NumberPicker$1.onClick(NumberPicker.java:633)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.view.View.performClick(View.java:4202)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.view.View$PerformClick.run(View.java:17340)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.os.Handler.handleCallback(Handler.java:725)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.os.Looper.loop(Looper.java:137)
09-06 01:15:35.947: E/AndroidRuntime(19858): at android.app.ActivityThread.main(ActivityThread.java:5039)
09-06 01:15:35.947: E/AndroidRuntime(19858): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 01:15:35.947: E/AndroidRuntime(19858): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 01:15:35.947: E/AndroidRuntime(19858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-06 01:15:35.947: E/AndroidRuntime(19858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-06 01:15:35.947: E/AndroidRuntime(19858): at dalvik.system.NativeStart.main(Native Method)
我该如何解决这个问题?
答案 0 :(得分:4)
在时间戳中添加标记。它对我有用:
<TimePicker android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<requestFocus />
</TimePicker>
答案 1 :(得分:2)
问题似乎尚未解决并且已知错误。 在我的例子中,当我点击EditText时发生了nullPointer异常。我的解决方法是遍历子项,直到找到EditText并将其“focusable”属性设置为false。
递归,
// level is just a counter for reading better Logs
private void hideFocusable(ViewGroup vg, int level)
{
for(int i=0; i < vg.getChildCount(); i++)
{
try
{
ViewGroup vt = (ViewGroup) vg.getChildAt(i);
Log.i("NODE",level+") "+ vg.getChildAt(i).getClass().getName());
hideFocusable(vt, level+1);
}catch (ClassCastException e) {
View vf = (View) vg.getChildAt(i);
Log.i("LEAF",level+") "+ vf.getClass().getName());
// here you can pick EditText and setFocusable to false
if(vf.getClass().equals(EditText.class))
vf.setFocusable(false);
}
}
}
答案 2 :(得分:0)
在TimePicker中使用NPE在API级别19(4.4)上遇到同样的问题:695。问题是我在将24HourView设置为true之前设置了onTimeChangedListener。
错:
timePicker = (TimePicker) viewContent.findViewById(R.id.timePicker);
timePicker.setOnTimeChangedListener(listener);
timePicker.setIs24HourView(true);
工作:
timePicker = (TimePicker) viewContent.findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(listener);
答案 3 :(得分:0)
我必须在我的AndroidManifest.xml中将我的应用程序的主题更改为android:Theme.Black
<application
android:name="com.tunapui.news"
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
将此样式添加到styles.xml
<style name="AppBaseTheme" parent="android:Theme.Black" />
我的片段的风格不同但现在它有效。
<强>更新强>
android:descendantFocusability="blocksDescendants"
我认为问题与虚拟键盘在屏幕上的焦点有关,现在我没有改变样式我将属性android:descendantFocusability="blocksDescendants"
添加到我的TimePicker,并且像魅力一样工作:)。
<TimePicker
android:id="@+id/toTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants" />
答案 4 :(得分:0)
我用
解决了这个问题 timePicker.setSaveEnabled(false);