我正在尝试使用自定义数组适配器在片段中填充ListView.但是我在自定义数组适配器的getView()方法中的方法findViewById()上得到NullPointerException.
这是CustomAdapter.java:
package graph.prathya.com.materialdesign;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Prathya on 5/19/2015.
*/
public class CustomAdapter extends ArrayAdapter<Post>{
Context context;
int layoutid,singlerowimgid= R.id.imgIcon;
Post data[]=null;
public CustomAdapter(Context context, int layoutid, Post data[]){
super(context, layoutid);
this.layoutid = layoutid;
this.data = data;
this.context =context;
}
private class PostHolder{
ImageView imgicon;
TextView title;
}
@Override
public int getCount(){
return data.length;
}
@Override
public View getView(int position, View convertView,ViewGroup parent){
PostHolder holder;
LayoutInflater li = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
li.inflate(layoutid,parent,false);
holder = new PostHolder();
holder.imgicon = (ImageView)convertView.findViewById(singlerowimgid);
holder.title =(TextView)convertView.findViewById(R.id.txtTitle);
}
else {
holder = (PostHolder)convertView.getTag();
}
Post post = data[position];
holder.title.setText(post.title);
holder.imgicon.setImageResource(post.imgicon);
return convertView;
}
}
这是我的XML文件,它定义了ListFragment中单行的外观
singlerow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView android:id="@+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
这是我的Fragment类:
Fragment1.java:
package graph.prathya.com.materialdesign;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
/**
* Created by Prathya on 5/19/2015.
*/
public class Fragment1 extends ListFragment{
Post post[] = new Post[]{
new Post(R.drawable.passion,"Passion"),
new Post(R.drawable.event,"Event"),
new Post(R.drawable.project,"Project"),
new Post(R.drawable.groupstudy,"Group Study")};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1,container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new CustomAdapter(getActivity(), R.layout.singlerow, post));
}
}
这是fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffaa00">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_gravity="center_horizontal" />
这是Post.java
package graph.prathya.com.materialdesign;
/**
* Created by Prathya on 5/19/2015.
*/
public class Post {
public int imgicon;
public String title;
public Post(){
super();
}
public Post(int imgicon, String title){
this.imgicon = imgicon;
this.title = title;
}
}
这是MainActivity.java
package graph.prathya.com.materialdesign;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addframent();
}
public void addframent(){
FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fm1 = new Fragment1();
fragmentTransaction.add(R.id.sa,fm1);
fragmentTransaction.commit();
}
}
这是错误信息
05-19 19:26:13.362 18306-18306/graph.prathya.com.materialdesign
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: graph.prathya.com.materialdesign, PID: 18306
java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View android.view.View.findViewById(int)’ on a null
object reference
at graph.prathya.com.materialdesign.CustomAdapter.getView(CustomAdapter.java:42)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17554)
..
解决方法:
在CustomAdapter.java中,您必须为convertView分配膨胀的布局:
convertView = li.inflate(layoutid,parent,false);
更新:
您必须将持有者设置为新增加的视图:
covnertView.setTag(holder);