我正在尝试切换我的应用程序以使用对话框片段但是当对话框可见时旋转屏幕时我遇到应用程序崩溃。我可以在下面描述的一个非常简单的应用程序中重现它。在Android studio中创建一个新项目并添加一个DialogFragment,如下所示:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
if (savedInstanceState == null) {
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setMessage("Alert")
.setTitle("My Alert")
.create();
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.setDialog(dialog);
dialogFragment.show(getSupportFragmentManager(), "dialog");
}
}, 1000);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
}
}
public static class MyDialogFragment extends DialogFragment {
private Dialog mDialog;
public MyDialogFragment() {
super();
mDialog = null;
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
}
// Set the dialog to display
public void setDialog(Dialog dialog) {
mDialog = dialog;
}
// Return a Dialog to the DialogFragment.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return mDialog;
}
}
}
现在运行应用程序,在对话框出现后(加载后1秒),旋转屏幕。请注意,我只在上面的onCreate初始化对话框。
以下是我得到的例外情况:
01-30 11:19:40.199 31986-31986/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testdialogs, PID: 31986
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testdialogs/com.example.testdialogs.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3758)
at android.app.ActivityThread.access$900(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v4.app.DialogFragment.onActivityCreated(DialogFragment.java:368)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:566)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5241)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3758)
at android.app.ActivityThread.access$900(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
我的gradle脚本如下所示:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
}
这是创建新项目并使用最新的android支持jar时的标准构建脚本。
我可以在DialogFragment的onCreate方法中执行setRetainInstance,然后它不会崩溃,但是对话框在旋转时会被解除。这显然比崩溃更好,但不是我正在寻找的。
我不确定使用DialogFragments的首选方式是什么,但我从谷歌的一些示例代码(对于谷歌服务sdk)得到了这个想法。我认为他们知道他们在做什么所以我会使用相同的概念。
答案 0 :(得分:2)
我不想回答我自己的问题,但我找出了崩溃的原因。我需要在DialogFragment的onCreateDialog中实际创建对话框,而不是从我的Activity中设置它。不完全确定为什么就是这种情况。也许在轮换时,android系统会删除绑定到Activity的旧实例的引用。这不是完全理想的,但我可以通过传递用于在
中创建对话框的数据来使用它这是更新代码,可以在不崩溃的情况下进行旋转:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
if (savedInstanceState == null) {
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "dialog");
}
}, 1000);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
}
}
public static class MyDialogFragment extends DialogFragment {
public MyDialogFragment() {
super();
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
}
// Return a Dialog to the DialogFragment.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setMessage("Alert")
.setTitle("My Alert")
.create();
}
}
}
答案 1 :(得分:2)
我通过继承DialogFragment类并重写下两个生命周期回调方法解决了这个问题:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance()) {
getDialog().setDismissMessage(null);
}
super.onDestroyView();
}
答案 2 :(得分:0)
首次创建活动时,savedInstanceState
为空,因为它没有要保存的先前状态。当屏幕旋转时,活动将被销毁,并且会再次调用onCreate(savedInstanceState)
并保存一些状态(如DialogFragment
状态,这是可见的。)
所以,首先你的savedInstaceState
为空,当屏幕旋转时,实例被保存,然后在onCreate(savedInstanceState)
上恢复,因此它不再为空,并且if(savedInstanceState == null)
内的任何内容都将被调用,然后你得到NullPointerException
。
要解决此问题,请删除此验证:
if(savedInstanceState == null){
///blablalba
}