gpt4 book ai didi

android - 如何从 XML 扩展 RelativeLayout

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:09 25 4
gpt4 key购买 nike

我有一个非常具体的任务来膨胀 View 。老实说,我真的不明白我到底应该做什么。唯一正确编译的方法是

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

最佳答案

您需要在初始化 View 之前扩充布局。

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

在构造函数中初始化mInflater

LayoutInflater mInflater; //declare

然后在构造函数中

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

关于android - 如何从 XML 扩展 RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com