我目前正在尝试实施Material Design活动转换。由于一个我真的不知道的原因,它崩溃了。有人能给我一个线索吗? (我不知道它是否重要,但在我的MainActivity中,我有一个带有Fragments的ViewPager,我尝试从中开始下一个Activity)
非常感谢你的帮助。
我的代码:
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
(...)
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("TAAG", "MainActivity::onCreate1");
super.onCreate(savedInstanceState);
Log.i("TAAG", "MainActivity::onCreate2");
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Log.i("TAAG", "MainActivity::onCreate3");
setContentView(R.layout.activity_main);
Log.i("TAAG", "MainActivity::onCreate4");
(...)
}
以下是日志:
11-13 18:12:36.442 1098-1098/I/TAAG﹕ MainActivity::onCreate1
11-13 18:12:36.544 1098-1098/I/TAAG﹕ MainActivity::onCreate2
11-13 18:12:36.559 1098-1098/E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: mypackage, PID: 1098
java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:301)
at mypackage.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:5)
尝试在getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
之前调用super.onCreate()
。