android中的list和null指针

时间:2017-02-11 23:49:20

标签: android json list nullpointerexception

你好每个人我有问题,列表为null无法从对象获取值我尝试从json获取数据并在listview中显示


错误信息

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: vss.testvss, PID: 22745
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                      at vss.testvss.MainActivity$JSONTask.onPostExecute(MainActivity.java:159)
                      at vss.testvss.MainActivity$JSONTask.onPostExecute(MainActivity.java:88)
                      at android.os.AsyncTask.finish(AsyncTask.java:660)
                      at android.os.AsyncTask.-wrap1(AsyncTask.java)
                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

mainactivity:

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private ListView listView;

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

        ListView listview = (ListView) findViewById(R.id.list);



    }
    public class JSONTask extends AsyncTask<String, String, List<Company>> {
        List<Company> companyList = new ArrayList<>();
        @Override
        protected List<Company> doInBackground(String... params) {

            HttpURLConnection httpURLConnection = null;
            BufferedReader bufferedReader = null;

            try {
                URL url = new URL(params[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                // Through this we can read the data line by line
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer stringBuffer = new StringBuffer();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuffer.append(line);
                }

                String finalJSON = stringBuffer.toString();
                JSONObject parentObject = new JSONObject(finalJSON);
                JSONArray parentArray = parentObject.getJSONArray("Companies");



                // For parsing a list of items you have to use for loop
                for (int i = 0; i < parentArray.length(); i++) {
                    JSONObject object = parentArray.getJSONObject(i);
                    String n = object.getString("name");
                    String lt = object.getString("latitude");
                    String lg = object.getString("langitude");
                    int id = object.getInt("id");
                    int r = object.getInt("rate");
                    Company company = new Company(n, id, r, lt, lg);
                    // Adding the finalObject in the List
                    Log.d("name from object",n);
                    Log.d("name from list",companyList.toString());
                    companyList.add(company);
                }
                return companyList;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                    try {
                        if (bufferedReader != null) {
                            bufferedReader.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return companyList;
        }

        // Value returned by doInBackGround will be passed here...

        @Override
        protected void onPostExecute(List<Company> result) {
            super.onPostExecute(result);

            CompanyAdapter adapter = new CompanyAdapter(getApplicationContext(), R.layout.row, result);
            //listView.setAdapter(adapter);
        }
    }
}

我跟踪问题并在日志中找到该对象返回值 但列表显示如下(来自列表的D /名称:[]或者像列表中的D /名称:[vss.testvss.Company@19fd15a])

1 个答案:

答案 0 :(得分:5)

删除此行:

 ListView listview = (ListView) findViewById(R.id.list);

并替换为:

  listview = (ListView) findViewById(R.id.list);

并取消注释将Adapter设置为ListView的行。