Как раздуть RelativeLayout из XML

У меня есть очень конкретная задача раздуть представление. Честно говоря, я действительно не понимаю, что я должен делать именно так. Единственный метод, который был составлен

@Override public View getView(int position, View convertView, ViewGroup parent) { //TODO - Get the current ToDoItem final ToDoItem toDoItem = mItems.get(position); //TODO - Inflate the View for this ToDoItem // from todo_item.xml. RelativeLayout itemLayout = (RelativeLayout)convertView.findViewById(R.id.RelativeLayout1);`` 

Но теперь я получил нулевой указатель. Итак, как я должен правильно раздувать его? XML-код ниже.

 02-21 09:21:11.260: E/AndroidRuntime(1506): FATAL EXCEPTION: main 02-21 09:21:11.260: E/AndroidRuntime(1506): java.lang.NullPointerException 02-21 09:21:11.260: E/AndroidRuntime(1506): at course.labs.todomanager.ToDoListAdapter.getView(ToDoListAdapter.java:93) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.widget.AbsListView.obtainView(AbsListView.java:2177) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.widget.ListView.makeAndAddView(ListView.java:1840) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.widget.ListView.fillSpecific(ListView.java:1321) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.widget.ListView.layoutChildren(ListView.java:1633) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.widget.AbsListView.onLayout(AbsListView.java:2012) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.View.layout(View.java:14289) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.ViewGroup.layout(ViewGroup.java:4562) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.View.layout(View.java:14289) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.ViewGroup.layout(ViewGroup.java:4562) 02-21 09:21:11.260: E/AndroidRuntime(1506): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.View.layout(View.java:14289) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.ViewGroup.layout(ViewGroup.java:4562) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.View.layout(View.java:14289) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.ViewGroup.layout(ViewGroup.java:4562) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.Choreographer.doFrame(Choreographer.java:532) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.os.Handler.handleCallback(Handler.java:730) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.os.Handler.dispatchMessage(Handler.java:92) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.os.Looper.loop(Looper.java:137) 02-21 09:21:11.260: E/AndroidRuntime(1506): at android.app.ActivityThread.main(ActivityThread.java:5103) 02-21 09:21:11.260: E/AndroidRuntime(1506): at java.lang.reflect.Method.invokeNative(Native Method) 02-21 09:21:11.260: E/AndroidRuntime(1506): at java.lang.reflect.Method.invoke(Method.java:525) 02-21 09:21:11.260: E/AndroidRuntime(1506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 02-21 09:21:11.260: E/AndroidRuntime(1506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 02-21 09:21:11.260: E/AndroidRuntime(1506): at dalvik.system.NativeStart.main(Native Method) <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/titleView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:textAppearance="?android:attr/textAppearanceLarge" > </TextView> <TextView android:id="@+id/StatusLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/statusCheckBox" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="17dp" android:text="@string/done_string" > </TextView> <CheckBox android:id="@+id/statusCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="52dp" android:layout_toRightOf="@+id/StatusLabel" > </CheckBox> <TextView android:id="@+id/PriorityLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/statusCheckBox" android:layout_alignTop="@+id/StatusLabel" android:layout_toLeftOf="@+id/priorityView" android:text="@string/priority_string" > </TextView> <TextView android:id="@+id/priorityView" android:layout_width="50dip" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/statusCheckBox" android:layout_alignParentRight="true" android:layout_alignTop="@+id/StatusLabel" > </TextView> <TextView android:id="@+id/DateLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/statusCheckBox" android:text="@string/date_string" > </TextView> <TextView android:id="@+id/dateView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/DateLabel" android:layout_toRightOf="@+id/DateLabel" > </TextView> </RelativeLayout> 

Перед инициализацией просмотров вам нужно раздуть макет.

 convertView = mInflater.inflate(R.layout.yourlayout, parent, false); 

В конструкторе инициализируйте mInflater

 LayoutInflater mInflater; //declare 

Затем в конструкторе

 mInflater = LayoutInflater.from(context); // context pass to the constructor of adapter 

попробуй это:

 RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate( R.layout.todo_item, parent, false); 

Я предполагаю, что вы пишете пользовательский адаптер:

 @Override public View getView(int position, View convertView, ViewGroup parent) { View itemLayout; if (convertView == null) { itemLayout = inflater.inflate(R.layout.fragment_active_orders_item, parent, false); } else { itemLayout = convertView; } //do what you want with itemLayout; return itemLayout; } 

LayoutInflater inflater и включите надувной inflater в вашем адаптере, например inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);