3

我尝试做这样的事情:

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)
4

3 回答 3

1

你的代码是有效的,你绝对可以创建一个ArrayList<View>,你也可以像这样膨胀你的布局。所以你的问题在别的地方。

您应该检查哪个对象为 null:vinflater. 在第一种情况下,您的布局或充气方法可能有问题。如果是第二种情况,则您的实例化inflater错误或丢失。

于 2013-03-19T20:30:33.170 回答
0

你可以试试这段代码

View child = getLayoutInflater().inflate(R.layout.child);
rootView.add(child);
于 2013-03-19T19:47:08.073 回答
0

您可以在数组列表中添加视图

ArrayList<View> viewsList =new ArrayList<>();
// adding views to array list
viewsList.add(view1);
viewList.add(view2);

您可以从 ArrayList 获取视图,如下所示

viewsList.get(0);  //to get view at first position
viewsList.get(1);  //to get view at second position

继续编码:)

于 2019-05-06T11:39:28.193 回答