【问题标题】:Trouble parsing a json in android studio that works in one activity and not the other无法在 android studio 中解析在一个活动而不是另一个活动中工作的 json
【发布时间】:2018-05-02 07:45:03
【问题描述】:

我有这个 Json,我在我的应用程序的第一个活动中解析它,但是当我尝试在另一个活动上使用相同的 json 重新创建相同的东西时,它就不起作用了。

我一直在阅读有关此内容和空指针的信息,以查看是否可以查看在哪里可以解决此问题,但我没有想法。由于我在其他活动中有更多 Json 需要阅读,而我无法让其中一个工作,所以我无法完成剩下的工作。

当我调用 getData 时,我输入了一个函数、用户名和密码,这又将成为 url 的一部分,它为我提供 json 内容以及我尝试读取和解析的位置。

这是 MainActivity:

public class MainActivity extends AppCompatActivity
{
Button logindaapp; // Botão de Login
EditText username; // Secção username
EditText password; // Secção password

private String user;
private String pass;

User userEntry = null;

ArrayList<CarDataset> carsDataset = new ArrayList<>();

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

    // Botão Login
    logindaapp = (Button) findViewById(R.id.btn_login); // Associa o id do botão

    logindaapp.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        { // Quando é clicado o botão
            disableButton(v);
            String funcao = "wslogin";
            username = (EditText)findViewById(R.id.et_username);
            user = username.getText().toString();
            password = (EditText)findViewById(R.id.et_password);
            pass = password.getText().toString();
            getData(funcao, user, pass);

        }
    });
}

String mLogTag;
// "wslogin"
public void getData(String funcao, String username, String password)
{


    String url = "http://www.xxxxxxxxx.xxx/index.php/site/" + funcao + "?u="+username+"&p="+ password;
    Log.e(mLogTag, "getting car data\n" + url);
    DownloadJsonFile downloadCarData = new DownloadJsonFile();
    downloadCarData.execute(url);
}

// Método para prevenir Duplo click
public static void disableButton(final View v)
{
    try
    {
        v.setEnabled(false);
        v.setAlpha((float) 0.5);
        v.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    v.setEnabled(true);
                    v.setAlpha((float) 1.0);
                } catch (Exception e)
                {
                    Log.d("disableButton","Exception while un hiding the view: " + e.getMessage());
                }
            }
        }, 1000);
    } catch (Exception e)
    {
        Log.d("disableButton","Exception while hiding the view : " + e.getMessage());
    }
}

private void updateCarDataset(String json) throws JSONException
{
    JSONArray array = new JSONArray(json);

    for (int i = 0; i < array.length(); i++)
    {
        JSONObject object = array.getJSONObject(i);
        Integer carID = object.getInt("viaturaID");
        String lPlate = object.getString("matricula");
        Integer companyID = object.getInt("empresaID");
        Integer modelID = object.getInt("modeloID");
        Integer driverID = object.getInt("condutorID");
        Integer departmentID = object.getInt("departamentoID");
        Integer fuelID = object.getInt("combustivelID");
        Integer image = object.getInt("imagem");
        Integer idTypeVehicle = object.getInt("idtipoviatura");
        Long locationID = object.getLong("localizacaoID");
        Double latit = object.getDouble("lat");
        Double longt = object.getDouble("lon");
        String dateTime = object.getString("datahora");
        Double speed = object.getDouble("speed");
        Integer brandID = object.getInt("marcaID");
        String model = object.getString("modelo");
        String brand = object.getString("marca");
        String company = object.getString("empresa");
        Long nif = object.getLong("NIF");
        String address = object.getString("morada");
        String place = object.getString("local");
        String cp = object.getString("cp");
        String phone = object.getString("telefone");
        String email = object.getString("mail");
        String responsable = object.getString("responsavel");
        String expirationLicense = object.getString("validadeLicenca");
        Integer litersWarningDepot = object.getInt("LitrosAvisoDeposito");
        Integer kmsReminder = object.getInt("KmsLembrete");
        Integer daysReminder = object.getInt("diasLembrete");
        Integer outsiderNr = object.getInt("nrexterno");
        String name = object.getString("nome");
        String fuel = object.getString("combustivel");
        Double lastPrice = object.getDouble("ultimoPreco");


        carsDataset.add(new CarDataset(carID,lPlate, companyID, modelID, driverID, departmentID, fuelID, image, locationID, idTypeVehicle, latit, longt, dateTime, speed, brandID, model, brand, company, nif, address, place, cp, phone, email, responsable, expirationLicense, litersWarningDepot, kmsReminder, daysReminder, outsiderNr, name, fuel, lastPrice));
    }
    Log.d("my Tag", carsDataset.get(1).licencePlate);
    if (ValidateCredentials(carsDataset) == true)
    {
        userEntry = new User(user, pass);
        Intent login = new Intent();
        login.putExtra("user", userEntry);

        login.setClass(MainActivity.this, Menu.class);
        startActivity(login); // Começa a nova Actividade
        MainActivity.this.finish(); // Termina esta atividade
    }
    else
    {
        Toast.makeText(MainActivity.this, "Credencias Erradas. Tente de Novo", Toast.LENGTH_LONG).show();
    }
}

public boolean ValidateCredentials(ArrayList<CarDataset> testArray)
{
    if (testArray.isEmpty())
    {
        return false;
    }
    else
    {
        return true;
    }

}

public class DownloadJsonFile extends AsyncTask<String, Void, String> {
    @Override

    protected String doInBackground(String... params)
    {
        try
        {
            URL url = new URL(params[0]);

            // Open a stream from the URL
            InputStream stream = new URL(params[0]).openStream();

            String line;
            StringBuilder result = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

            while ((line = reader.readLine()) != null)
            {
                result.append(line);
                Log.e(mLogTag, "reading ...");
            }

            // Close the stream
            reader.close();
            stream.close();

            return result.toString();// + params[1];
        }
        catch (IOException e)
        {
            Log.e(mLogTag, "download data could not be read");
        }
        return null;
    }

    @Override
    protected void onPostExecute(String jsonObject)
    {
        if (jsonObject != null)
        {
            try
            {
                Log.e(mLogTag, "data received ->" + jsonObject);

                if (!jsonObject.contains("noDataAvailable"))
                {
                    try
                    {
                        updateCarDataset(jsonObject);
                    } catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    Log.e(mLogTag, "heello");
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

}

这是我的第二个活动:

public class SelectCarToTrack extends AppCompatActivity
{
private String mLogTag;
private User userEntry = null;
RecyclerView recyclerView;
ArrayList<String> matriculas = new ArrayList<>();;
ArrayList<CarDataset> carros = new ArrayList<>();;
CarDataset selecionado = null;

private String user;
private String pass;


// Necessário para o RecyclerView
@Override
protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    ((CarExpandableAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}

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

    String carFuncao = "wslogin";

    Intent i = getIntent();
    userEntry = (User) i.getExtras().getParcelable("user");
    user = userEntry.getUsername();
    pass = userEntry.getPassword();

    String Url = "http://www.xxxxxxxx.xxx/index.php/site/"+ carFuncao + "?u=" + user + "&p=" + pass;
    Log.d("myTag", user);
    Log.d("myTag", pass);


    getData(carFuncao, user, pass);


    obtainLicensePlates(carros);

    Log.d("myTag", carros.get(0).licencePlate); // ERROR COMING THROUGH HERE!
    recyclerView = (RecyclerView)findViewById(R.id.rv_listcars);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    CarExpandableAdapter adapter = new CarExpandableAdapter(this,initData(matriculas));
    adapter.setParentClickableViewAnimationDefaultDuration();
    adapter.setParentAndIconExpandOnClick(true);

    recyclerView.setAdapter(adapter);
    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(SelectCarToTrack.this, recyclerView, new RecyclerItemClickListener.OnItemClickListener()
    {
        @Override
        public void onItemClick(View view, int position)
        {
            selecionado = carros.get(position);
            Intent geolocalizarSpecific = new Intent();
            geolocalizarSpecific.putExtra("select", selecionado);
            geolocalizarSpecific.setClass(SelectCarToTrack.this, Geolocalizcao.class);
            startActivity(geolocalizarSpecific);
        }

        @Override
        public void onItemLongCLick(View view, int position)
        {
        }
    }));
}

private void obtainLicensePlates(ArrayList<CarDataset> cars)
{
    if(cars != null)
    {
        for(int i=0; i < carros.size(); i++)
        {
            String temp = "";
            temp = carros.get(i).licencePlate;
            matriculas.add(i,temp);
        }
    }
    else
    {
        Toast.makeText(SelectCarToTrack.this, "Não foram recebidos dados", Toast.LENGTH_LONG).show();
    }
}

// O que acontece quando clico o botão para voltar para atrás
@Override
public void onBackPressed() {
    Intent goBack = new Intent();
    goBack.putExtra("user", userEntry );
    goBack.setClass(SelectCarToTrack.this, Menu.class);
    startActivity(goBack);
    SelectCarToTrack.this.finish();
}

private List<ParentObject> initData(ArrayList<String> licensePlates)
{
    CarCreator carCreator = CarCreator.get(this, licensePlates);
    List<CarParent> cars = carCreator.getAll();
    List<ParentObject> parentObject = new ArrayList<>();
    for(CarParent car:cars)
    {
        List<Object> childList = new ArrayList<>();
        childList.add(new CarChild("Track your vehicle","See Details"));
        car.setChildObjectList(childList);
        parentObject.add(car);
    }
    return parentObject;
}

public void getData(String funcao, String username, String password)
{
    // funcao = wslogin

    String Url = "http://www.xxxxx.xxx/index.php/site/"+ funcao + "?u=" + username + "&p=" + password;
    Log.e(mLogTag, "getting car data\n" + Url);
    DownloadJsonFile downloadCarData = new DownloadJsonFile();
    downloadCarData.execute(Url);
}

private void updateCarDataset(String json) throws JSONException
{
    JSONArray array = new JSONArray(json);

    for (int i = 0; i < array.length(); i++)
    {
        JSONObject object = array.getJSONObject(i);
        Integer carID = object.getInt("viaturaID");
        String lPlate = object.getString("matricula");
        Integer companyID = object.getInt("empresaID");
        Integer modelID = object.getInt("modeloID");
        Integer driverID = object.getInt("condutorID");
        Integer departmentID = object.getInt("departamentoID");
        Integer fuelID = object.getInt("combustivelID");
        Integer image = object.getInt("imagem");
        Integer idTypeVehicle = object.getInt("idtipoviatura");
        Long locationID = object.getLong("localizacaoID");
        Double latit = object.getDouble("lat");
        Double longt = object.getDouble("lon");
        String dateTime = object.getString("datahora");
        Double speed = object.getDouble("speed");
        Integer brandID = object.getInt("marcaID");
        String model = object.getString("modelo");
        String brand = object.getString("marca");
        String company = object.getString("empresa");
        Long nif = object.getLong("NIF");
        String address = object.getString("morada");
        String place = object.getString("local");
        String cp = object.getString("cp");
        String phone = object.getString("telefone");
        String email = object.getString("mail");
        String responsable = object.getString("responsavel");
        String expirationLicense = object.getString("validadeLicenca");
        Integer litersWarningDepot = object.getInt("LitrosAvisoDeposito");
        Integer kmsReminder = object.getInt("KmsLembrete");
        Integer daysReminder = object.getInt("diasLembrete");
        Integer outsiderNr = object.getInt("nrexterno");
        String name = object.getString("nome");
        String fuel = object.getString("combustivel");
        Double lastPrice = object.getDouble("ultimoPreco");


        carros.add(new CarDataset(carID, lPlate, companyID, modelID, driverID, departmentID, fuelID, image, locationID, idTypeVehicle, latit, longt, dateTime, speed, brandID, model, brand, company, nif, address, place, cp, phone, email, responsable, expirationLicense, litersWarningDepot, kmsReminder, daysReminder, outsiderNr, name, fuel, lastPrice));
    }
}


public class DownloadJsonFile extends AsyncTask<String, Void, String> {
    @Override

    protected String doInBackground(String... params)
    {
        try
        {
            URL url = new URL(params[0]);

            // Open a stream from the URL
            InputStream stream = new URL(params[0]).openStream();

            String line;
            StringBuilder result = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

            while ((line = reader.readLine()) != null)
            {
                result.append(line);
                Log.e(mLogTag, "reading ...");
            }

            // Close the stream
            reader.close();
            stream.close();

            return result.toString();// + params[1];
        }
        catch (IOException e)
        {
            Log.e(mLogTag, "download data could not be read");
        }
        return null;
    }

    @Override
    protected void onPostExecute(String jsonObject)
    {
        if (jsonObject != null)
        {
            try
            {
                Log.e(mLogTag, "data received ->" + jsonObject);

                if (!jsonObject.contains("noDataAvailable"))
                {
                    try
                    {
                        updateCarDataset(jsonObject);
                    } catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    Log.e(mLogTag, "heello");
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

}

Logcat:

                                                       [ 11-18 17:29:58.579 10015:10015 E/         ]
                                                       getting car data
                                                       http://www.gescar3w.com/index.php/site/wslogin?u=xxxxxxx@xxxxx.xxx&p=xxxxxxx
11-18 17:29:58.580 10015-10015/g3w.gescarcopytest D/AndroidRuntime: Shutting down VM
11-18 17:29:58.582 10015-10015/g3w.gescarcopytest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: g3w.gescarcopytest, PID: 10015
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{g3w.gescarcopytest/g3w.gescarcopytest.SelectCarToTrack}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                    at android.os.Looper.loop(Looper.java:164)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                 Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                    at java.util.ArrayList.get(ArrayList.java:437)
                                                                    at g3w.gescarcopytest.SelectCarToTrack.onCreate(SelectCarToTrack.java:79)
                                                                    at android.app.Activity.performCreate(Activity.java:6975)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                    at android.os.Looper.loop(Looper.java:164) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
11-18 17:29:58.592 10015-10020/g3w.gescarcopytest I/zygote: Do partial code cache collection, code=118KB, data=102KB
11-18 17:29:58.592 10015-10020/g3w.gescarcopytest I/zygote: After code cache collection, code=118KB, data=102KB
11-18 17:29:58.592 10015-10020/g3w.gescarcopytest I/zygote: Increasing code cache capacity to 512KB
11-18 17:29:58.592 10015-10020/g3w.gescarcopytest I/zygote: JIT allocated 56KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)

我正在获取用户的信息,那里没问题...并且代码实际上是相同的,除了它具有回收器视图的部分和所有部分。但我不明白我哪里出错了......

【问题讨论】:

  • getData() 使用的是AsyncTask,所以当你的Log.d() 语句被执行时,carros 仍然是空的。

标签: android json object arraylist null-pointer


【解决方案1】:

感谢 Ben P. 所说的,我设法弄明白了。 AsyncTask 读取数据需要一点时间,大约需要 2/3 秒。因此,立即询问信息是行不通的,会导致崩溃。

在第二个活动中调用 getData 之后,我必须把所有东西都拿走,并在我更新我的 ArrayList 之后放置它。现在看起来像这样:

private void updateCarDataset(String json) throws JSONException {
    JSONArray array = new JSONArray(json);

    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        Integer carID = object.getInt("viaturaID");
        String lPlate = object.getString("matricula");
        Integer companyID = object.getInt("empresaID");
        Integer modelID = object.getInt("modeloID");
        Integer driverID = object.getInt("condutorID");
        Integer departmentID = object.getInt("departamentoID");
        Integer fuelID = object.getInt("combustivelID");
        Integer image = object.getInt("imagem");
        Integer idTypeVehicle = object.getInt("idtipoviatura");
        Long locationID = object.getLong("localizacaoID");
        Double latit = object.getDouble("lat");
        Double longt = object.getDouble("lon");
        String dateTime = object.getString("datahora");
        Double speed = object.getDouble("speed");
        Integer brandID = object.getInt("marcaID");
        String model = object.getString("modelo");
        String brand = object.getString("marca");
        String company = object.getString("empresa");
        Long nif = object.getLong("NIF");
        String address = object.getString("morada");
        String place = object.getString("local");
        String cp = object.getString("cp");
        String phone = object.getString("telefone");
        String email = object.getString("mail");
        String responsable = object.getString("responsavel");
        String expirationLicense = object.getString("validadeLicenca");
        Integer litersWarningDepot = object.getInt("LitrosAvisoDeposito");
        Integer kmsReminder = object.getInt("KmsLembrete");
        Integer daysReminder = object.getInt("diasLembrete");
        Integer outsiderNr = object.getInt("nrexterno");
        String name = object.getString("nome");
        String fuel = object.getString("combustivel");
        Double lastPrice = object.getDouble("ultimoPreco");


        carros.add(new CarDataset(carID, lPlate, companyID, modelID, driverID, departmentID, fuelID, image, locationID, idTypeVehicle, latit, longt, dateTime, speed, brandID, model, brand, company, nif, address, place, cp, phone, email, responsable, expirationLicense, litersWarningDepot, kmsReminder, daysReminder, outsiderNr, name, fuel, lastPrice));
    }

    obtainLicensePlates(carros);

    recyclerView = (RecyclerView) findViewById(R.id.rv_listcars);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    CarExpandableAdapter adapter = new CarExpandableAdapter(this, initData(matriculas));
    adapter.setParentClickableViewAnimationDefaultDuration();
    adapter.setParentAndIconExpandOnClick(true);

    recyclerView.setAdapter(adapter);
    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(SelectCarToTrack.this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {

        @Override
        public void onItemClick(View view, int position) {
            selecionado = carros.get(position);
            Intent geolocalizarSpecific = new Intent();
            geolocalizarSpecific.putExtra("select", selecionado);
            geolocalizarSpecific.setClass(SelectCarToTrack.this, Geolocalizcao.class);
            startActivity(geolocalizarSpecific);
        }

        @Override
        public void onItemLongCLick(View view, int position) {
        }
    }));
} 

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多