无法让自定义适配器在片段中工作
Cant get custom adapter to work in fragment
编辑:
所以显然我的问题被标记为重复,问题是我在 Whosebug 上花了很多时间寻找解决方案,然后才发布我的 question.my 问题不是 "what is null pointer exception" 请帮助
这里是菜鸟,
我正在学习如何使用适配器来填充 ListView,当我尝试使用它时我的自定义适配器出现问题应用程序崩溃并给我这个错误:
java.lang.NullPointerException
at android.widget.AbsListView.obtainView(AbsListView.java:2265)
然而,当我尝试使用 ArrayAdatper 时,它工作得很好。
我怀疑我传递了错误的上下文。
请注意,我使用的是片段而不是 activity
应该将虚拟字符串数组 dummyValues
中的列表视图 myCustomListview
填充到 custom_row
布局
中的文本字段 location
中的代码
消费
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);
}
}
自定义适配器
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>
您正在从 getView 中 returning null,您必须 return 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;
}
编辑: 所以显然我的问题被标记为重复,问题是我在 Whosebug 上花了很多时间寻找解决方案,然后才发布我的 question.my 问题不是 "what is null pointer exception" 请帮助
这里是菜鸟, 我正在学习如何使用适配器来填充 ListView,当我尝试使用它时我的自定义适配器出现问题应用程序崩溃并给我这个错误:
java.lang.NullPointerException
at android.widget.AbsListView.obtainView(AbsListView.java:2265)
然而,当我尝试使用 ArrayAdatper 时,它工作得很好。 我怀疑我传递了错误的上下文。
请注意,我使用的是片段而不是 activity
应该将虚拟字符串数组 dummyValues
中的列表视图 myCustomListview
填充到 custom_row
布局
location
中的代码
消费
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);
}
}
自定义适配器
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>
您正在从 getView 中 returning null,您必须 return 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;
}