我试图应用show / hide片段。这是我的代码:
public void loadSpinner() {
final Fragment specialistPickerFragment = (Fragment) getFragmentManager().findFragmentById(R.id.fragmentAppoinmentDetail);
kindOfAppoinmentSpinner = (Spinner) findViewById(R.id.kindOfAppoinment);
List dataSrc = new ArrayList();
dataSrc.add("Khám tổng quát");
dataSrc.add("Khám chuyên khoa");
ArrayAdapter adapter = new ArrayAdapter(MakingAppoinmentActivity.this, R.layout.spinner_config, dataSrc);
adapter.setDropDownViewResource(R.layout.spinner_config);
kindOfAppoinmentSpinner.setAdapter(adapter);
kindOfAppoinmentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (kindOfAppoinmentSpinner.getSelectedItem().toString().equals("Khám chuyên khoa")) {
getFragmentManager().beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(specialistPickerFragment).commit();
} else {
getFragmentManager().beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.hide(specialistPickerFragment).commit();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
我正在使用this question中的代码。但是当我前往我希望它生效的活动时,我的应用程序崩溃了。我在google上做了很多搜索,他们都有相同的代码。谁知道为什么? 这是我的错误:
E / AndroidRuntime:致命异常:主要 处理:com.example.hoang.bnhnint,PID:21150 java.lang.NullPointerException:尝试写入字段&#39; int android.app.Fragment.mNextAnim&#39;在null对象引用上 在android.app.BackStackRecord.run(BackStackRecord.java:786) 在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1582) 在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:483) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6816) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1565) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453)