【问题标题】:Data is not being displayed on my listview from the database数据库中的列表视图上未显示数据
【发布时间】:2018-01-03 03:43:09
【问题描述】:

我正在尝试在我的应用程序的 phpmyadmin 中显示数据库中的用户名列表,但我不断收到以下信息

Unable to start activity ComponentInfo{ie.example.artur.adminapp/ie.example.artur.adminapp.ShowUsers}: java.lang.NullPointerException: storage == null

我知道这意味着数据没有被发送,因此活动在选择时无法启动,列表是空的,但我的问题是为什么列表没有填满数据?

这是我的 ShowUser 活动

package ie.example.artur.adminapp;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toolbar;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;

import android.view.View;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import static android.R.attr.name;

/**
 * Created by family on 24/07/2017.
 */

public class ShowUsers extends AppCompatActivity {


    ListView lv;
    //String[] names = {"Amy","John","Joseph","Carl"};
    InputStream is = null;
    String line= null;
    String result = null;
    String temp="";
    String[] arr;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showusers);

        lv = (ListView) findViewById(R.id.lv);

        //Inlfate the list view with the items

        lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1,arr));

        android.widget.Toolbar toolbar = (android.widget.Toolbar) findViewById(R.id.toolbar);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        //set up the code to fetch data from the database


        try {
            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost("http://10.3.2.51/www/");

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            //SETUP THE INPUTSTREAM TO RECEIVE THE DATA (INITIAL)
        }catch (Exception e){
            System.out.println("Exception 1 caught");
        }

        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            // Create a String builder object to hold the data
            StringBuilder sb = new StringBuilder();
            while((line = reader.readLine())!=null)
                sb.append(line+"\n");


            //Use the toString() method to get the data in the result

            result = sb.toString();
            is.close();
            //check the data by printing the results in the logcat

            System.out.println("-----Here is my data -----");
            System.out.println(result);

        }catch(Exception e){
            System.out.print("Exception 2 caught");
        }

        try{

            JSONArray jsonArray = new JSONArray(result);//Create a json array
            int count = jsonArray.length();


            for(int i=0; i<count; i++){
                //create a json object to extract the data
                JSONObject json_data = jsonArray.getJSONObject(i);
                temp +=json_data.getString("name")+":";
                //where name is attribute of the getdata table
                //using ':' as the delimiter

            }

            //Afer receiving everything store the contents in a string array from temo separated using the delimiter
            arr = temp.split(":");
            //set the list adapter with the array arr

            lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1, arr));

        }catch(Exception e){
            System.out.println("I am really bored of writing all these exception blocks");
        }



    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        {
            switch (item.getItemId())
            {
                case R.id.action_settings : startActivity (new Intent(this, ShowUsers.class));
                    break;
            }
            return super.onOptionsItemSelected(item);
        }}




}

这是 phpmyadmin 中数据库中的用户表:

这也是我保存 tut.php 文件的地方:

tut.php:

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("socialmedia_website",$con);

$r=mysql_query("select name from users where 1";

while($row=mysql_fetch_array($r))

    {

        $out[]=$row;
    }

    print(json_encode($out));
    mysql_close($con)

*我也在使用安卓设备来做这个项目

这就是我要返回的内容:

【问题讨论】:

  • 不要使用 mysql_* 。只用过mysqli_*
  • mysql 在 PHP 7 中已弃用并完全删除。请改用 mysqliPDO
  • @B.Desai 是的,我知道我只是没有意识到,因为我正在关注视频教程

标签: php android phpmyadmin


【解决方案1】:
$r=mysql_query("select name from users where 1";

您的查询有问题,您错过了右括号。

您的查询应如下所示。

$r=mysql_query("select name from users where 1");

【讨论】:

  • WHERE 1 是“true”或“everything”的同义词。
【解决方案2】:

setAdapter 行重复了两次。所以删除第一个,因为那个时间 arr 值为 null,这就是你得到 NullpointerException 的原因

 lv = (ListView) findViewById(R.id.lv);
 lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1,arr)); //remove this

【讨论】:

    【解决方案3】:

    这里有一个错误。你错过了) 右括号

    $r=mysql_query("select name from users where 1");
    

    注意:使用 mysqli_* 函数。

    【讨论】:

    • 是的,我已经解决了这个问题,但是只是出现了一个空白屏幕,它是空的:/
    • 查询是否返回值?
    • 我认为这可能会有所帮助:stackoverflow.com/questions/16777829/…
    • 问题是我现在也没有返回任何错误,所以我不知道现在是什么问题
    • 我想我会选择这个作为正确答案,因为您解决了我的第一个问题,即缺少),那么我需要再问一个问题
    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多