如何从XML中扩展RelativeLayout

时间:2022-02-21 07:57:06

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.

但是现在我得到了空指针。那么我应该如何正确地给它充气呢?下面的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>

3 个解决方案

#1


11  

You need to inflate a layout before initializing views.

在初始化视图之前,您需要对布局进行膨胀。

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

In the constructor initialize mInflater

在构造函数中初始化mInflater

LayoutInflater mInflater; //declare

Then in constructor

然后在构造函数中

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

#2


7  

try this:

尝试这个:

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

#3


3  

I assume you are writing custom adapter:

我假设您正在编写自定义适配器:

@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;
}

Declare LayoutInflater inflater and intialize inflater in your adapter like inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

声明LayoutInflater inflater并在你的适配器中初始化inflater,如inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

#1


11  

You need to inflate a layout before initializing views.

在初始化视图之前,您需要对布局进行膨胀。

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

In the constructor initialize mInflater

在构造函数中初始化mInflater

LayoutInflater mInflater; //declare

Then in constructor

然后在构造函数中

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

#2


7  

try this:

尝试这个:

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

#3


3  

I assume you are writing custom adapter:

我假设您正在编写自定义适配器:

@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;
}

Declare LayoutInflater inflater and intialize inflater in your adapter like inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

声明LayoutInflater inflater并在你的适配器中初始化inflater,如inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);