I got a very specific task to inflate view. To be honest i dont really understand what I should do exactly. The only method, which compiled probperly was that
@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);``
But now I got null pointer. So how should I properly inflate it? XML code below.
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>
The general gist is this: If attachToRoot is set to true , then the layout file specified in the first parameter is inflated and attached to the ViewGroup specified in the second parameter. Then the method returns this combined View, with the ViewGroup as the root.
LinearLayout is less used as compared to RelativeLayout. RelativeLayout is used more in applications. We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.
try this:
RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(
R.layout.todo_item, parent, false);
You need to inflate a layout before initializing views.
convertView = mInflater.inflate(R.layout.yourlayout, parent, false);
In the constructor initialize mInflater
LayoutInflater mInflater; //declare
Then in constructor
mInflater = LayoutInflater.from(context); // context pass to the constructor of adapter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With