Android:简单的适配器空指针

时间:2018-09-21 08:07:25

标签: android simpleadapter jsonparser

我有一个应用程序,我想使用PHP Json从服务器检索数据并获取方法,为此应用程序使用简单的适配器以及Json下载器和json解析器,

我的简单适配器无法在listView中显示数据,

当我运行项目时,它会崩溃并显示一些错误。

这是主要活动:

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

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    set_bedehkariha_list();


}

public void set_bedehkariha_list(){
    JSONDownloader jsdl = new JSONDownloader();
    String temp = jsdl.downloadURL( "http://famila1.ir/khabgah/get_khabgah_cash.php");

    List<HashMap<String,Object>> cash = new ArrayList<HashMap<String, Object>>();
    cashParser cashparse = new cashParser();
    cash= cashparse.parse(temp);

    String[] from = {"hazine","tozihat","hazine_shareTo","who_paid","date"};
    int[] to = {R.id.mablaq_hazine,R.id.babate_hazine,R.id.moshtarak_ba,R.id.pardakhtaz,R.id.tarikh_hazine};
    SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this,cash, R.layout.cash_list_row,from,to);
    ListView lv = (ListView) findViewById(R.id.list_bedehkariha);
    lv.setAdapter(myadapter);
}

这是jsonDownloader:

  public class JSONDownloader
{
    public String downloadURL( String strUrl )
    {
        String data = "";

    try
    {
        URL url = new URL( strUrl );

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setReadTimeout(10000);

        connection.setConnectTimeout(15000);

        connection.setRequestMethod("GET");

        connection.setDoInput(true);

        connection.connect();

        InputStream myStream = connection.getInputStream();

        BufferedReader br = new BufferedReader( new InputStreamReader( myStream ) );

        StringBuilder sb = new StringBuilder();

        String line;

        while ( ( line = br.readLine() ) != null )
        {
            sb.append( line );
        }

        data = sb.toString();

        br.close();

        connection.disconnect();

        myStream.close();
    }
    catch ( Exception e )
    {
        /*
          *Log.i( "MatiMessage" , "error in JSONDownloader in downloadURL() -> " + e.toString() );
          */
    }

    return data;
}

这是我的JsonParser:

    public List<HashMap<String , Object>> parse( String json )
{
    List<HashMap<String , Object>> all_cash = new ArrayList<>();

    try
    {
        JSONObject jObj = new JSONObject( json );

        JSONArray jArr = jObj.getJSONArray( "cash" );

        for( int i = 0; i < jArr.length(); i ++ )
        {
            JSONObject temp = jArr.getJSONObject( i );

            HashMap<String , Object> cash = new HashMap<String , Object>();

            cash.put( "id" , temp.getString( "id" ) );
            cash.put( "member_id" , temp.getString( "member_id" ) );
            cash.put( "hazine" , temp.getString( "hazine" ) );
            cash.put( "date" , temp.getString( "date" ) );
            cash.put( "hazine_shareTo" , "hazine_shareTo" );
            cash.put( "tozihat" , temp.getString( "tozihat" ) );
            cash.put( "who_paid" , temp.getString( "who_paid" ) );
            all_cash.add( cash );
        }

    }
    catch ( Exception e )
    {
        /*
         * Log.i( "MatiMessage" , "error in cashParser in parse() -> " + e.toString() );
         */
    }

    return ( all_cash );
}

这是我运行应用程序时主要活动中的错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aqamamad.myapplication/com.example.aqamamad.myapplication.MainActivity}:
     

java.lang.NullPointerException:尝试调用虚拟方法'void   android.widget.ListView.setAdapter(android.widget.ListAdapter)'   空对象引用

2 个答案:

答案 0 :(得分:0)

您的代码包含

onCreate()
    setContentView(R.layout.activity_main);
    set_bedehkariha_list()

...

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

    // nullpointerexception here
    lv.setAdapter(myadapter);

表示R.layout.activity_main不包含R.id.list_bedehkariha或 在onCreate()之前调用set_bedehkariha_list()

答案 1 :(得分:0)

您需要在单独的线程上调用network Api。请尝试以下

NULL

最后更改以下方法以调用asyncTask。

class DownloadTask extends AsyncTask<Void, Void, String> {
        JSONDownloader downloader;

        public DownloadTask() {
            downloader = new JSONDownloader();
        }

        @Override
        protected String doInBackground(Void... voids) {
            return downloader.downloadURL("http://famila1.ir/khabgah/get_khabgah_cash.php");
        }

        @Override
        protected void onPostExecute(String data) {
            super.onPostExecute(data);

            cashParser cashparse = new cashParser();
            List<HashMap<String,Object>> cash = cashparse.parse(data);

            String[] from = {"hazine","tozihat","hazine_shareTo","who_paid","date"};
            int[] to = {R.id.mablaq_hazine,R.id.babate_hazine,R.id.moshtarak_ba,R.id.pardakhtaz,R.id.tarikh_hazine};
            SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this,cash, R.layout.cash_list_row,from,to);
            ListView lv = (ListView) findViewById(R.id.list_bedehkariha);
            lv.setAdapter(myadapter);
        }
    }