我正在处理一个项目,它给了我一个错误。当我打开列表时,错误显示,而不是旋转设备。如果我尝试选择项目或关闭列表,则会失败。代码如下:
private void showDevicesMenu() {
if (Utils.isDeviceOnline(getActivity())) {
if (getFragmentManager().findFragmentByTag("deviceFragment") == null) {
this.deviceAdapter = new DevicesMenuAdapter<String, Devices>(
getActivity(), retainFragment.getDeviceList());
/*
* this.deviceAdapter = new DevicesMenuAdapter(getActivity(),
* android.R.layout.simple_list_item_1, getDevicesAlias());
*/
this.deviceFragment = new DevicesMenuFragment();
this.deviceFragment.setListAdapter(deviceAdapter);
getFragmentManager()
.beginTransaction()
.replace(R.id.content, deviceFragment, "deviceFragment")
.commit();
} else {
getFragmentManager().beginTransaction().remove(deviceFragment)
.commit();
}
} else {
Toast.makeText(mContext, R.string.no_internet_label,
Toast.LENGTH_LONG).show();
}
}
错误如下:
03-26 09:29:22.669: E/AndroidRuntime(5692): FATAL EXCEPTION: main
03-26 09:29:22.669: E/AndroidRuntime(5692): java.lang.NullPointerException
03-26 09:29:22.669: E/AndroidRuntime(5692): at android.app.BackStackRecord.run(BackStackRecord.java:651)
03-26 09:29:22.669: E/AndroidRuntime(5692): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
03-26 09:29:22.669: E/AndroidRuntime(5692): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
03-26 09:29:22.669: E/AndroidRuntime(5692): at android.os.Handler.handleCallback(Handler.java:730)
03-26 09:29:22.669: E/AndroidRuntime(5692): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 09:29:22.669: E/AndroidRuntime(5692): at android.os.Looper.loop(Looper.java:176)
03-26 09:29:22.669: E/AndroidRuntime(5692): at android.app.ActivityThread.main(ActivityThread.java:5419)
03-26 09:29:22.669: E/AndroidRuntime(5692): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 09:29:22.669: E/AndroidRuntime(5692): at java.lang.reflect.Method.invoke(Method.java:525)
03-26 09:29:22.669: E/AndroidRuntime(5692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
03-26 09:29:22.669: E/AndroidRuntime(5692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
03-26 09:29:22.669: E/AndroidRuntime(5692): at dalvik.system.NativeStart.main(Native Method)
我该怎么做?
答案 0 :(得分:0)
configChanges
放入AndroidManifest.xml <activity
android:name="YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
>
我希望这段代码可以帮到你。
谢谢。
答案 1 :(得分:0)
您只需将此行添加到活动文件
即可<activity
android:name="YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize">
........
.......
</activity>
并将此功能添加到您的Activity类中......您可以在这里做任何事情......
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
screen_calculation();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
screen_calculation();
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
}
}