无法将数据检索到listview - android

时间:2017-03-24 09:15:22

标签: java android listview nullpointerexception

[编辑] 我是Android开发的新手,所以请耐心等待。我有两个名为Join Game的java类,扩展到AppCompatActivity和HintList,扩展到ArrayAdapter<>这是连接到数据库。那也许是其中一个因素?

对于join game的布局,我有listview 对于hintlist的布局,我有三个textview

代码就是这样

JoinGame

public class JoinGame extends AppCompatActivity {
ListView list;
String[] itemdescription = {};
String[] itemhints = {};
String[] itemlocasyon = {};
ProgressDialog progress;
View view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_joingame);
    Button schan = (Button)findViewById(R.id.tarascan);
    schan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(JoinGame.this,ScanActivity.class);
            startActivity(intent);
        }
    });
}

 @Override
protected void onStart() {
    super.onStart();
    progress = new ProgressDialog(this);
    progress.setMessage("Connecting...");
    progress.setCancelable(false);
    progress.show();

    RequestFactory.joingameitems(JoinGame.this, RequestFactory.user_id, new RequestCallback<JSONArray>() {
        @Override
        public void onSuccess(final JSONArray response) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    RequestFactory.response = response;
                    itemdescription = new String[response.length()];
                    itemhints = new String[response.length()];
                    itemlocasyon = new String[response.length()];

                    for (int hl = 0; hl < response.length(); hl++){
                        try{
                            itemdescription[hl] = ((String)(response.getJSONObject(hl)).get("description"));
                            itemhints[hl] = ((String)(response.getJSONObject(hl)).get("hint"));
                            itemlocasyon[hl] = ((String)(response.getJSONObject(hl)).get("location"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } ////////// below this is the adapter
                    final HintList hladapt = new HintList(JoinGame.this,itemdescription,itemlocasyon,itemhints);
                    list = (ListView)findViewById(R.id.item_hints_and_location);
                    list.setAdapter(hladapt);
                    progress.dismiss();
                }
            });
        }

        @Override
        public void onFailed(final String message) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(JoinGame.this, message, Toast.LENGTH_LONG).show();
                            progress.dismiss();
                        }
                    });
                }
            });
        }
    });

}

HintList

   public class HintList extends ArrayAdapter<String> {
    private final Activity context;
    private String[] itemDesc = {};
    private String[] itemHints = {};
    private String[] itemLocation = {};

    public HintList(Activity context,String[] itemDesc,String[] itemhints,String[] itemlocation) {
        super(context,R.layout.hints_list);
        this.context = context;
        this.itemDesc = itemDesc;
        itemHints = itemhints;
        itemLocation = itemlocation;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.hints_list, null, true);
        TextView itemDesc2 = (TextView)rowView.findViewById(R.id.itemdescription);
        TextView itemHint = (TextView)rowView.findViewById(R.id.itemhint);
        TextView itemLocation2 = (TextView)rowView.findViewById(R.id.itemlocation);

        itemDesc2.setText(itemDesc[position]);
        itemHint.setText(itemHints[position]);
        itemLocation2.setText(itemLocation[position]);

        return rowView;
    }
}

我实际上检索了数据(这里)

E/DB: [{"description":"chinese garter","location":"near Tricycle station","hint":"garter plus chinese"},{"description":"isang pinoy game","location":"near ....","hint":"may salitang baka"},{"description":"\"tinik\"","location":"below...","hint":"may salitang tinik"},{"description":"aka Tinubigan","location":"at the back...","hint":"katunog ng pintero"},{"description":"\"knock down the can\"","location":"near...","hint":"gumagamit ng lata"}]

但它没有显示在我的列表视图上 我不知道该怎么办。 我实际上尝试过这个(我添加了视图)

final HintList hladapt = new HintList(JoinGame.this,itemdescription,itemlocasyon,itemhints);
                    list = (ListView)view.findViewById(R.id.item_hints_and_location);
                    list.setAdapter(hladapt);
                    progress.dismiss();

但它只会返回错误 java.lang.NullPointerException:尝试调用虚方法

4 个答案:

答案 0 :(得分:1)

改变这个:

View rowView = inflater.inflate(R.layout.hints_list, null, true);

为:

View rowView = inflater.inflate(R.layout.hints_list, parent, false);

将您的getView()方法修改为:

    @Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.hints_list, parent, false);
    TextView itemDesc2 = (TextView)rowView.findViewById(R.id.itemdescription);
    TextView itemHint = (TextView)rowView.findViewById(R.id.itemhint);
    TextView itemLocation2 = (TextView)rowView.findViewById(R.id.itemlocation);

    itemDesc2.setText(itemDesc[position]);
    itemHint.setText(itemHints[position]);
    itemLocation2.setText(itemLocation[position]);

    return rowView;
}

答案 1 :(得分:0)

尝试添加getCount

@Override
    public int getCount() {
        return itemHints.length;
    }

尝试将您的代码更改为此

public class HintList extends ArrayAdapter<String> {
private final Activity context;
private String[] itemDesc = {};
private String[] itemHints = {};
private String[] itemLocation = {};
private LayoutInflater inflater=null;

public HintList(Activity context,String[] itemDesc,String[] itemhints,String[] itemlocation) {
    super(context,R.layout.hints_list);
    this.context = context;
    this.itemDesc = itemDesc;
    itemHints = itemhints;
    itemLocation = itemlocation;
        inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
     return itemHints.length;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
        if (view==null){
            view=inflater.inflate(R.layout.hints_list, null, true);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

    holder.itemDesc2.setText(itemDesc[position]);
    holder.itemHint.setText(itemHints[position]);
    holder.itemLocation2.setText(itemLocation[position]);

    return view;
}
static class ViewHolder{

        TextView itemDesc2,itemHint,itemLocation2;
        ViewHolder(View view) {
            itemDesc2 = (TextView)view.findViewById(R.id.itemdescription);
    itemHint = (TextView)view.findViewById(R.id.itemhint);
    itemLocation2 = (TextView)view.findViewById(R.id.itemlocation);
        }
    }
}

答案 2 :(得分:0)

我的建议是创建一个bean类(DetailsBean),用于setter和getter方法,以及ArrayList(details1)。

List<DetailsBean> details1 = new ArrayList<>(); // declare this as global

然后将bean添加到代码

下面
  public void run() {
         RequestFactory.response = response;
         itemdescription = new String[response.length()];
         itemhints = new String[response.length()];
         itemlocasyon = new String[response.length()];

         for (int hl = 0; hl < response.length(); hl++){
          try{
                itemdescription[hl] = ((String)(response.getJSONObject(hl)).get("description"));
                itemhints[hl] = ((String)(response.getJSONObject(hl)).get("hint"));
                itemlocasyon[hl] = ((String)(response.getJSONObject(hl)).get("location"));
                // here the bean 
                DetailsBean dbean = new DetailsBean(itemDescription, itemhints, itemlocasyon);
               details1.add(dbean); // add all the data to details1 ArrayList
               HintList hladapt = new HintList(getActivity(), details1);
              (ListView)findViewById(R.id.item_hints_and_location);
               list.setAdapter(hladapt);
            } catch (JSONException e) {
              e.printStackTrace();
         }
    }

您的DetailsBean应该如下所示

public class DetailsBean {

        private String itemDescription="";
        private String itemhints="";
        private String  itemlocasyon ="";


    public DetailsBean(String description, String hints, String itemlocasyon) {
       this.itemDescription=description;
        .....
 }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDesription;
    }

    public String getItemDescription() {
        return itemDescription;
    }
       .... 
}

然后你的HintList

public class HintList extends BaseAdapter{   
    Activity context;
    List<DetailsBean> details;
    private LayoutInflater mInflater;

    public CustomBaseAdapter(Activity context,List<DetailsBean> details) {
        this.context = context;
        this.details = details;
    }

     ........
}

答案 3 :(得分:0)

在适配器中添加这些覆盖方法

@Override
public int getCount() {
    return itemHints.length;
}

@Override
public Object getItem(int i) {
    return i;
}

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