java - NullPointerException 和 Android ListView

标签 java android listview android-adapter

我正在尝试用 LinkedList 填充 ListView。 我认为我的实现有问题..但我无法找出问题所在。

这是我设置适配器的方法:

public void refreshListView() {
    //pManager.simpleSort();
    // pManager.getProfiles()

    LinkedList<Profile> temp = new LinkedList<Profile>();
    temp.add(new Profile("hello", "link", true)); // im kind of testing here
    adapter = new CustomAdapter(getApplicationContext(), temp);
    list.setAdapter(adapter);
}

这是我使用多个教程编写的 CustomAdapter。

public class CustomAdapter extends BaseAdapter {

    private LinkedList<Profile> profiles;
    private Context context;

    public CustomAdapter(Context context, LinkedList<Profile> input) {
        this.context = context;
        profiles = input;
    }

    @Override
    public int getCount() {
        if (profiles == null) {
            return 0;
        } else {
            return profiles.size();
        }
    }

    @Override
    public Profile getItem(int arg0) {
        return profiles.get(arg0);
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        View out = v;
        if (out == null){
            out = new ProfileItemView(context, getItem(position));
        }
        return out;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }
}

这是我编写的用于显示我的个人资料的自定义 View :

public class ProfileItemView extends RelativeLayout {

    public ProfileItemView(Context context, Profile profile) {
        super(context, null);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.list_item, null, true);
        ImageView icon = (ImageView) findViewById(R.id.default_icon_view);
        if (profile.checkDefault()) {
            icon.setImageResource(R.drawable.ic_launcher);
        } else {
            icon.setImageResource(R.drawable.android_icon);
        }
        TextView name = (TextView) findViewById(R.id.name_view);
        name.setText(profile.getName());
    }
}

这里还有一个 logcat:

Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x419c52a0)
FATAL EXCEPTION: main
java.lang.NullPointerException
   at com.exp.test.ProfileItemView.<init>(ProfileItemView.java:18)
   at com.exp.test.CustomAdapter.getView(CustomAdapter.java:37)
   at android.widget.AbsListView.obtainView(AbsListView.java:2402)
   at android.widget.ListView.makeAndAddView(ListView.java:1769)
   at android.widget.ListView.fillDown(ListView.java:672)
   at android.widget.ListView.fillFromTop(ListView.java:733)
   at android.widget.ListView.layoutChildren(ListView.java:1622)
   at android.widget.AbsListView.onLayout(AbsListView.java:2237)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1948)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1758)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1042)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4329)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
   at android.view.Choreographer.doFrame(Choreographer.java:525)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5118)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
   at dalvik.system.NativeStart.main(Native Method)

最佳答案

您需要使用膨胀的 View 对象来初始化 View 。 findViewById 在当前展开的布局中查找 View 。

同时传递 Activity 上下文

  adapter = new CustomAdapter(ActivityName.this, temp);   

使用 View 持有者模式

static class ViewHolder
{
ImageView iv;
TextView tv;
}

在构造函数中

LayoutInflater mInflater;
 public CustomAdapter(Context context, LinkedList<Profile> input) {
        mInflater = LayoutInflater.fromf(context;
        profiles = input;
    }

然后在getView

public View getView(int position, View convertView, ViewGroup parent) { 
 ViewHolder holder;      
 if (convertView == null) { 
 convertView = mInflater.inflate(R.layout.lis_item,parent, false);
 holder = new ViewHolder(); 
 holder.tv = (TextView) convertView.findViewById(R.id.name_view);  
 holder.iv = (ImageView)convertView.findViewById(R.id.default_icon_view);              
 convertView.setTag(holder); 
 } else { 
   holder = (ViewHolder) convertView.getTag(); 
 }
  Profile profile = profiles.get(position);
    if (profile.checkDefault()) {
        holder.iv.setImageResource(R.drawable.ic_launcher);
    } else {
        holder.iv.setImageResource(R.drawable.android_icon);
    }
         holder.tv.setText(profile.getName());
 return convertView; 
 }

关于java - NullPointerException 和 Android ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19663701/

相关文章:

java - ParseException:无法解析的日期:

c# - 在 xamarin android 中使用 edittext 对 ListView 进行即时搜索功能

android - 在ListView下面添加一个LinearLayout

android - 在 ListFragment 中显示 ProgressDialog

java - 如何对一个整数的3位数字求和?

java @异步方法: not running async

android - 带有用于项目的简单 Java 对象的 ListView 示例

java - E/UncaughtException : android. content.res.Resources$NotFoundException:资源 ID #0x7f0800a8

android - 我看不到的循环

java - 如何在 Spring MVC 中创建 cron 来安排任务