java - 无法将数据检索到 ListView - android

标签 java android listview nullpointerexception

[编辑] 我是 android 开发的新手,所以请多多包涵。我有两个名为 Join Game 的 java 类,扩展到 AppCompatActivity和 HintList,扩展到 ArrayAdapter<> . 这是连接到数据库。那么也许这是因素之一?

对于join game的布局, 我有一个 listview 以及 hintlist 的布局我有三个textview .

代码是这样的

加入游戏

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();
                        }
                    });
                }
            });
        }
    });

}

提示列表

   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"}]

但它没有显示在我的 ListView 中 我不知道我应该做什么了。 我实际上尝试过制作这个(我添加了 View )

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: Attempt to invoke virtual method

最佳答案

改变这个:

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;
}

关于java - 无法将数据检索到 ListView - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42995489/

相关文章:

java - 从菜单单击开始新 Activity 时出错

c# - 无法删除 ListView 中显示的图像文件

android - 将新元素添加到 ListView 的顶部

Android应用程序可以在模拟器上运行,但不能在真机上运行

android - 流式实时音频

java - 从我的 ListView 中添加 ItemOnClick,使用 JSON 从 MYSQL 中获取数据

java - 如何测试 InterruptedException

java - http ://anonsvn. jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd 返回 301

c# - 将项目从 C# 迁移到 Java

java - Http 获取 Web 服务上的请求不起作用