编辑: 显然我的问题被标记为重复,事情是我花了很多时间在stackoverflow上寻找解决方案,然后发布我的问题。我的问题不是“什么是空指针异常”请帮助
noob在这里, 我正在学习如何使用适配器来填充ListView,当我尝试使用它时,我的自定义适配器出现问题App崩溃并给了我这个错误:
java.lang.NullPointerException
at android.widget.AbsListView.obtainView(AbsListView.java:2265)
然而,当我尝试使用ArrayAdatper时,它工作得很好。 我怀疑我传递了错误的背景。
请注意我使用片段而不是活动
代码应该将列表视图myCustomListview
从虚拟字符串数组dummyValues
填充到location
布局中的文本字段custom_row
Spendiing
package app.hujair.android.example.com.aou_project_nm;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Spendings extends Fragment {
View view;
public Spendings() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState) {
//inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.fragment_spendings, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView listView = (ListView) view.findViewById(R.id.myCustomListview);
String[] dummyValues = new String[]{"hello", "something"};
listView = (ListView) view.findViewById(R.id.myCustomListview);
//CustomAdapter adapter = new CustomAdapter(getActivity(),dummyValues);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getActivity(),R.layout.custom_row,R.id.location, dummyValues);
listView.setAdapter(adapter);
}
}
CustomAdapter
package app.hujair.android.example.com.aou_project_nm;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] transcation_type) {
super(context,R.layout.custom_row ,transcation_type);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_row, parent, false);
String singleLocation = getItem(position);
TextView location = (TextView)customView.findViewById(R.id.location);
location.setText(singleLocation);
return convertView;
}
}
fragment_spending
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.hujair.android.example.com.aou_project_nm.Spendings">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total spendings goes here."
android:gravity="center"
android:textSize="35dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:id="@+id/Spending_text"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myCustomListview"
android:layout_marginTop="15dp"
android:layout_below="@+id/Spending_text"
android:divider="@null"
></ListView>
</RelativeLayout>
custom_row
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ImageView
android:layout_marginTop="35dp"
android:layout_marginLeft="25dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/custom_row_icon"
android:src="@drawable/minus_circle"/>
<TextView
android:layout_marginTop="-2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="note note note note ..."
android:textSize="15dp"
android:id="@+id/note"
android:layout_alignTop="@+id/amount"
android:layout_alignStart="@+id/location" />
<TextView
android:layout_alignParentRight="true"
android:gravity="right"
android:layout_marginTop="32dp"
android:layout_marginRight="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amount"
android:textSize="18dp"
android:id="@+id/amount"/>
<TextView
android:layout_marginTop="-8dp"
android:layout_below="@+id/amount"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/custom_row_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="location"
android:textSize="12dp"
android:id="@+id/location" />
<TextView
android:layout_marginTop="-2dp"
android:layout_below="@+id/location"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/custom_row_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
android:textSize="12dp"
android:id="@+id/time" />
</RelativeLayout>
答案 0 :(得分:1)
从getView返回null,必须返回customView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_row, parent, false);
String singleLocation = getItem(position);
TextView location = (TextView)customView.findViewById(R.id.location);
location.setText(singleLocation);
return customView;
}