我的/d-6ren">
gpt4 book ai didi

膨胀 View 时Android ListView NullPointerException

转载 作者:行者123 更新时间:2023-11-29 02:02:08 24 4
gpt4 key购买 nike

我有一个带有自定义行的 ListView:detail_row1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:padding="6dip"
**android:background="@drawable/item_background_selector"** ---> line 7
android:id="@+id/detail_linearLayout"
>

<TextView
android:id="@+id/propName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center_vertical|left"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingLeft="4sp"

/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/propValue"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingRight="4sp"
android:gravity="center_vertical|right"
/>

</LinearLayout>

我的/drawable/item_background_selector.xml(如果我将它放在 /color/item_background_selector.xml 下并在 detail_row1.xml 中正确引用它,则相同)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:color="#ACD52B">
</item>
<item android:state_selected="true" android:color="#0094CE">
</item>
</selector>

当我尝试在我的 ListAdapter 中扩充此 View 时:

public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.detail_row1, null); **--> NullPointerException**
}
.
.

它抛出 NullPointerException。

如果我只是从 detail_row1.xml 中删除第 7 行(android/background),情况就不是这样了

关于为什么会发生这种情况的任何线索/帮助(对 android 来说相当新)

提前致谢!

(编辑)Logcat:

**08-24 00:27:53.637: E/AndroidRuntime(15295): FATAL EXCEPTION: main**
08-24 00:27:53.637: E/AndroidRuntime(15295): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
08-24 00:27:53.637: E/AndroidRuntime(15295): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
08-24 00:27:53.637: E/AndroidRuntime(15295): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-24 00:27:53.637: E/AndroidRuntime(15295): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
08-24 00:27:53.637: E/AndroidRuntime(15295): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
08-24 00:27:53.637: E/AndroidRuntime(15295): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
08-24 00:27:53.637: E/AndroidRuntime(15295): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-24 00:27:53.637: E/AndroidRuntime(15295): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-24 00:27:53.637: E/AndroidRuntime(15295): at **com.dhomes.steel.DetailActivity$1.getView(DetailActivity.java:101)**

(第二次编辑)

我的列表适配器:

lvProperties.setAdapter(new ListAdapter() {
//Section section=new Section();

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;

if(v==null) {
//LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater vi =(LayoutInflater)DetailActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.detail_row1, null);
}

TextView name=(TextView)v.findViewById(R.id.propName);
TextView value=(TextView)v.findViewById(R.id.propValue);

if(name!=null) {
name.setText(section.propNames.get(position));
}
if(value!=null) {
value.setText(section.propValues.get(position));
}
return v;
}
.
.
.
}

最佳答案

LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.detail_row1, null);

从,上面的行。你似乎只是在你的 customadapter 类中夸大它。所以,你从 Constructor 中得到了 context 对象,所以最好像下面这样使用它 -

...
LayoutInflater vi = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.detail_row1, null);
...

并且,不要从您的 xml 文件中删除背景。只需尝试清理您的项目并在完成此更改后运行即可。

关于膨胀 View 时Android ListView NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12085018/

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