我尝试做这样的事情:
public static ArrayList<View> rootView = new ArrayList<View>();
但是当我尝试向它添加视图时,我只是得到一个空指针异常,这让我想知道我是否做错了什么,或者是否不可能做到这一点?
谢谢。
代码 :
ArrayList<View> rootView = new ArrayList<View>();
View v=inflater.inflate(R.layout.activity_main, null);
rootView.add(v);
rootView.add(v); 只是产生一个空指针异常
Logcat 补充说:
03-19 20:49:35.543: E/AndroidRuntime(8755): FATAL EXCEPTION: main
03-19 20:49:35.543: E/AndroidRuntime(8755): java.lang.NullPointerException
03-19 20:49:35.543: E/AndroidRuntime(8755): at code94.message.TestFragment.onCreateView(TestFragment.java:113)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.View.measure(View.java:15517)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.View.measure(View.java:15517)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.View.measure(View.java:15517)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.View.measure(View.java:15517)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-19 20:49:35.543: E/AndroidRuntime(8755): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2313)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.View.measure(View.java:15517)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.os.Handler.handleCallback(Handler.java:725)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.os.Looper.loop(Looper.java:137)
03-19 20:49:35.543: E/AndroidRuntime(8755): at android.app.ActivityThread.main(ActivityThread.java:5228)
03-19 20:49:35.543: E/AndroidRuntime(8755): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 20:49:35.543: E/AndroidRuntime(8755): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 20:49:35.543: E/AndroidRuntime(8755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-19 20:49:35.543: E/AndroidRuntime(8755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-19 20:49:35.543: E/AndroidRuntime(8755): at dalvik.system.NativeStart.main(Native Method)