在我的应用程序中,我有一个寻呼机作为主视图,我在onStart()中填充它但是当我旋转屏幕时它崩溃(因为它找不到视图)。
@Override
public void onStart() {
super.onStart();
createPager();
}
void createPager(){
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
每次旋转设备时,我应该在哪里填充寻呼机,以便重新填充?
EDIT2:
这是我片段的代码
public class WakeyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup wakeyView = (ViewGroup) inflater.inflate(
R.layout.wakey, container, false);
return wakeyView;
}
@Override
public void onSaveInstanceState(final Bundle outState) {
// super.onSaveInstanceState(outState);
}
}
public class PremiumFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup premiumView = (ViewGroup) inflater.inflate(
R.layout.premium_features, container, false);
return premiumView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setViews();
buttonSetup();
autoWakeCheck();
cpuWakeCheck();
if (WakeService.isAwake) {
glow.setVisibility(View.VISIBLE);
}
else {
glow.setVisibility(View.INVISIBLE);
}
}
@Override
public void onSaveInstanceState(final Bundle outState) {
// super.onSaveInstanceState(outState);
}
}
setViews()是我在我的xml中使用findViewByID()查找所有视图的视图,buttonSetup()是我用onClickListeners设置所有按钮的地方,以及我用setTypeface,setBackground()等准备视图的地方。 现在,当我尝试修改视图时,调用buttonSetup()时发生崩溃(这只有在我旋转屏幕时才会发生)
这是我正在获取的新日志
01-10 15:06:25.714 16847-16847/com.doublep.wakey E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.doublep.wakey, PID: 16847
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.doublep.wakey/com.doublep.wakey.Bulb}: 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 com.doublep.wakey.Bulb.buttonSetup(Bulb.java:604)
at com.doublep.wakey.Bulb$PremiumFragment.onViewCreated(Bulb.java:1417)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
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 com.doublep.wakey.Bulb.onStart(Bulb.java:1015)
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)
第1417行是buttonSetup(),第604行是buttonSetup()方法的第一行,我改变了视图的背景
答案 0 :(得分:0)
检查Activity lifecycle,您会看到onResume()
是首选方法。
此外,每个Fragment都需要一个没有参数的默认公共构造函数。
答案 1 :(得分:0)
您的片段似乎是嵌套的内部类
在static
和public
之间添加class
关键字。
答案 2 :(得分:0)
我找到了一个解决方案,我正在使用本地broadcastReceiver,我正在从onViewCreated()发送消息,然后我从广播接收器运行我的方法,我不知道它是否是理想的解决方案,但它的工作原理现在