【问题标题】:Custom adatper getView returns null pointer自定义adatper getView返回空指针
【发布时间】:2017-11-16 22:36:01
【问题描述】:

在我的应用程序中,我有一个带有自定义适配器的列表视图,该适配器在发送消息后重新加载。该应用程序在某些安装时崩溃。我在应用程序控制台中有堆栈跟踪。代码如下:

class IconicAdapter extends ArrayAdapter<Integer> {
        Activity context;

        IconicAdapter(Activity context) {
            super(context, R.layout.message_layout, listItems);

            this.context = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {


                //you can access layout inflater by accessing hosting activity
                convertView = getActivity().getLayoutInflater().inflate(R.layout.message_layout, parent, false);


            JSONObject json_data = null;
            try {
                json_data = jArray.getJSONObject(position);
            } catch (JSONException e) {
            }

            try {
                tv = (TextView) convertView.findViewById(R.id.username);
                tv.setText(json_data.getString("username"));

这里的堆栈跟踪:

java.lang.NullPointerException: 
  at com.testing.gold.CommunityFragment$IconicAdapter.getView (CommunityFragment.java:945)
  at android.widget.HeaderViewListAdapter.getView (HeaderViewListAdapter.java:232)

第 945 行:tv.setText(json_data.getString("username"));

【问题讨论】:

  • 可能你的 json_data 是 null ,打印堆栈跟踪:catch (JSONException e) { }
  • 不,堆栈跟踪中没有关于 json 的内容
  • 在 catch 中打印 json_data 和 e.printstacktrace。并把日志
  • 问题是我无法重现该问题。我只在应用程序控制台中看到它并在那里看到跟踪

标签: android custom-adapter getview


【解决方案1】:

使用以下代码: 类 IconicAdapter 扩展 ArrayAdapter { 活动上下文;

IconicAdapter(Activity context) {
    super(context, R.layout.message_layout, listItems);

    this.context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {
     convertView=LayoutInflater.inflate(R.layout.message_layout, null);
     JSONObject json_data = null;
     try {
        json_data = jArray.getJSONObject(position);

    } catch (JSONException e) {
         e.printStackTrace();
    }
 tv = (TextView) convertView.findViewById(R.id.username);

if(json_data!=null)
   tv.setText(json_data.getString("username"));
else
   tv.setText("")

【讨论】:

  • 你是对的,json_data 为空。但我不明白为什么。它有时有效,有时无效。
  • 嗯。让我们把代码发给我 ankitpatidar030@gmail.com 我会帮你查的
【解决方案2】:
class IconicAdapter extends ArrayAdapter<Integer> {
   Activity context;

   IconicAdapter(Activity context) {
    super(context, R.layout.activity_main, new Integer[]{1, 2});
    this.context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {

    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, null);
    JSONObject json_data = null;
    try {
        json_data = jArray.getJSONObject(position);
        tv = (TextView) convertView.findViewById(R.id.username);
        tv.setText(json_data.getString("username"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return convertView;
}

}

【讨论】:

  • 在fragment中运行重要吗?
  • 如果您使用的是片段的适配器,则不是。
  • 非静态方法inflate不能被静态上下文引用
  • 我已经修改了代码,知道它会起作用。也不要忘记投票给我
猜你喜欢
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
相关资源
最近更新 更多