0

我无法找出导致错误的行。我知道它是 NullPointerException,但我找不到确切的原因。

11-09 20:04:57.677: ERROR/AndroidRuntime(22119): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at android.app.Activity.findViewById(Activity.java:1853)
    at com.example.Posten2.HenteFelt.samleStatuser(HenteFelt.java:128)
    at com.example.Posten2.Posten.finnInfo(Posten.java:109)
    at com.example.Posten2.Posten.string(Posten.java:72)
    at com.example.Posten2.Posten$GetData.onPostExecute(Posten.java:190)
    at com.example.Posten2.Posten$GetData.onPostExecute(Posten.java:160)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

在文档中,我发现应该有一行以“原因:”开头,这是错误真正发生的地方。

HenteFelt.java 的代码

public class HenteFelt extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);





}
String returnString;


public String avsenderLand(JSONObject object) {
    //Sjekker avsenderland fra json - retureneren en string

    try {
        JSONArray consignmentSet = object.getJSONArray("consignmentSet");
        JSONObject object1 = consignmentSet.getJSONObject(0);
        JSONArray packageSet = object1.getJSONArray("packageSet");
        JSONObject object2 = packageSet.getJSONObject(0);
        JSONArray eventSet = object2.getJSONArray("eventSet");
        Integer countEvents = eventSet.length();
        countEvents = countEvents - 1;
        JSONObject object3 = eventSet.getJSONObject(countEvents);
        String country = object3.getString("country");

        returnString = "Pakken er sendt fra : " + country + "\n";

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return returnString;
}

public String lesSisteStatus(JSONObject object) {
    //leser ut siste status fra json - retureneren en string
    String ID = "";

    try {
        JSONArray consignmentSet = object.getJSONArray("consignmentSet");
        JSONObject object1 = consignmentSet.getJSONObject(0);
        JSONArray packageSet = object1.getJSONArray("packageSet");
        JSONObject object2 = packageSet.getJSONObject(0); //object inni packageSet
        JSONArray eventSet = object2.getJSONArray("eventSet");
        JSONObject object3 = eventSet.getJSONObject(0);
        ID = "Siste status er : " + object3.getString("description") + "\n";

        if (ID.contentEquals("Siste status er : Sendingen er utlevert\n")) {
            ID = "Du har hentet pakken !";

        }
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(), "Vennligst bruk et gyldig sporingsnummer", Toast.LENGTH_SHORT).show();
    }
    return ID;
}

public String sentDato(JSONObject object) {

    try {
        JSONArray consignmentSet = object.getJSONArray("consignmentSet");
        JSONObject object1 = consignmentSet.getJSONObject(0);
        JSONArray packageSet = object1.getJSONArray("packageSet");
        JSONObject object2 = packageSet.getJSONObject(0);
        JSONArray eventSet = object2.getJSONArray("eventSet");
        Integer countEvents = eventSet.length();
        countEvents = countEvents - 1;
        JSONObject object3 = eventSet.getJSONObject(countEvents);
        object3.getString("displayDate");
        returnString = "Datoen varen ble sendt er : " + object3.getString("displayDate") + "\n";

    } catch (Exception e) {
        e.printStackTrace();
    }
    return returnString;
}

public String antallEventer(JSONObject object) {
    try {
        //Sjekker antall hendelser i json - retureneren en string
        JSONArray consignmentSet = object.getJSONArray("consignmentSet");
        JSONObject object1 = consignmentSet.getJSONObject(0);
        JSONArray packageSet = object1.getJSONArray("packageSet");
        JSONObject object2 = packageSet.getJSONObject(0);
        JSONArray eventSet = object2.getJSONArray("eventSet");
        Integer countEvents = eventSet.length();
        returnString = "Antall hendelser er : " + countEvents.toString() + "\n";


    } catch (JSONException e) {
        e.printStackTrace();
    }
    return returnString;

}

public String samleStatuser(JSONObject object) {

    //TODO lage en string array som inneholder de ulike statusene pakken har hatt samt dato og tid på de.

    Integer countEvents = 0;
    String buffer = "";
    String dato = "";
    String tid = "";

       ArrayList<String> arrayList = new ArrayList<String>();
       ListView lv = (ListView) findViewById(R.id.listView);
       ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);




    try {
        JSONArray consignmentSet = object.getJSONArray("consignmentSet");
        JSONObject object1 = consignmentSet.getJSONObject(0);
        JSONArray packageSet = object1.getJSONArray("packageSet");
        JSONObject object2 = packageSet.getJSONObject(0);
        JSONArray eventSet = object2.getJSONArray("eventSet");
        countEvents = eventSet.length();

    } catch (Exception e) {
        e.printStackTrace();
    }


    Integer i;
    Integer objekt;

    if (countEvents == 0) {


        i = 0;
        try {

            JSONArray consignmentSet = object.getJSONArray("consignmentSet");
            JSONObject object1 = consignmentSet.getJSONObject(0);
            JSONArray packageSet = object1.getJSONArray("packageSet");
            JSONObject object2 = packageSet.getJSONObject(0);
            JSONArray eventSet = object2.getJSONArray("eventSet");
            JSONObject object3 = eventSet.getJSONObject(i);
            buffer = object3.getString("description");
            dato = object3.getString("displayTime");
            tid = object3.getString("displayDate");
            returnString += dato + "kl : " + tid + " Status : " + buffer;


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    for (i = countEvents; i > 0; i--) {
        try {


            objekt = i - 1;
            JSONArray consignmentSet = object.getJSONArray("consignmentSet");
            JSONObject object1 = consignmentSet.getJSONObject(0);
            JSONArray packageSet = object1.getJSONArray("packageSet");
            JSONObject object2 = packageSet.getJSONObject(0);
            JSONArray eventSet = object2.getJSONArray("eventSet");
            JSONObject object3 = eventSet.getJSONObject(objekt);
            buffer = object3.getString("description");
            dato = object3.getString("displayTime");
            tid = object3.getString("displayDate");
            returnString += dato + "kl : " + tid + " Status : " + buffer + "\n";
            lagArrayList(returnString, arrayList, arrayAdapter, lv);


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    {


    }


    return returnString;

}

public void lagArrayList(String status, ArrayList<String> arrayList, ArrayAdapter<String> arrayAdapter, ListView lv) {


    try {
        arrayList.add(status);
        String s = status += "YYY";
        // ListView lv = (ListView) findViewById(R.id.listView);
        lv.setAdapter(arrayAdapter);
    } catch (Exception e) {
        e.printStackTrace();
    }


}

public String hentetDatoTid(JSONObject object) {
    try {
        JSONArray consignmentSet = object.getJSONArray("consignmentSet");
        JSONObject object1 = consignmentSet.getJSONObject(0);
        JSONArray packageSet = object1.getJSONArray("packageSet");
        JSONObject object2 = packageSet.getJSONObject(0);
        JSONArray eventSet = object2.getJSONArray("eventSet");
        JSONObject object3 = eventSet.getJSONObject(0);
        String dato = object3.getString("displayDate");
        String tid = object3.getString("displayTime");
        String sted = object3.getString("city");

        returnString = "Klokka : " + tid + " " + "\n" + "Den " + dato + " " + "\n" + "På " + sted;


    } catch (JSONException e) {
        e.printStackTrace();
    }
    return returnString;
}


}

“Posten.java”的代码

public class Posten extends Activity {
/**
 * Called when the activity is first created.
 */

EditText et_sporingsnummer = null;
String ID = "";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Button button = (Button) findViewById(R.id.button);

    et_sporingsnummer = (EditText) findViewById(R.id.editText);
    et_sporingsnummer.setText("TESTPACKAGE-LOADED-FOR-DELIVERY");


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String[] input = {String.valueOf(et_sporingsnummer.getText())};
            new GetData().execute(input);


        }
    });


}

public void string(String sporingsnummer, String json) {

    FileOutputStream oFile;
    String sFileName = sporingsnummer + ".json";
    try {

        oFile = openFileOutput(sFileName, Context.MODE_PRIVATE);
        oFile.write(json.getBytes());
        oFile.close();
    } catch (Exception ex) {
        //handle exception here
    }
    finnInfo(sporingsnummer);

}

public void finnInfo(String sporingsnummer) {
    InputStream oFile;
    String combine;
    try {



        TextView tv = (TextView) findViewById(R.id.txt_sendtDato);

        oFile = openFileInput(sporingsnummer + ".json");
        InputStreamReader oReader = new InputStreamReader(oFile);
        BufferedReader oBuffer = new BufferedReader(oReader);
        String sText = "";
        String sEachLine = "";
        while ((sEachLine = oBuffer.readLine()) != null) {
            sText += sEachLine;
        }
        oReader.close();
        oBuffer.close();
        JSONObject object = new JSONObject(sText);
        //Sjekk om pakken er registrert eller ikke

        Posten posten = new Posten();
        // Posten posten = new Posten();
        HenteFelt henteFelt = new HenteFelt();
        Boolean riktigSporing = posten.sjekkSporingsNummer(object);
        if (riktigSporing == false) {
            tv.setText("Sporingsnummeret er feil eller så er ikke pakken registrert hos posten enda.");
        } else if (riktigSporing == true) {
            String sentDato = henteFelt.sentDato(object); // do function and return string
            String lesSisteStatus = henteFelt.lesSisteStatus(object);
            String antallEventer = henteFelt.antallEventer(object);
            String avsenderLand = henteFelt.avsenderLand(object);
            String samleStatuser = henteFelt.samleStatuser(object);
            String hentetDatoTid = henteFelt.hentetDatoTid(object);

            if (lesSisteStatus.contentEquals("Du har hentet pakken !")) {

                combine = "Du hentet pakken den : " + hentetDatoTid;
                tv.setTextSize(20);

            } else {

                combine = sentDato + "\n" + lesSisteStatus + "\n" + antallEventer + "\n" + avsenderLand + "\n" + "\n" + samleStatuser;
                tv.setTextSize(12);
            }
            tv.setText(combine);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public boolean sjekkSporingsNummer(JSONObject object) {
    Boolean riktigSporing = null;

    try {
        JSONArray consignmentSet = object.getJSONArray("consignmentSet");
        JSONObject object1 = consignmentSet.getJSONObject(0);
        JSONObject object2 = object1.getJSONObject("error");


        String s = object2.getString("message");

        s = "WORKS";

        riktigSporing = false;


    } catch (Exception e) {
        e.printStackTrace();
        riktigSporing = true;
        String ss = "FAIL";

    }
    return riktigSporing;


}

class GetData extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... data) {
        String URL = "http://sporing.bring.no/sporing.json?q=" + data[0];
        String jsonReturnText = "";
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 10000; // 10 second timeout for connecting to site
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        int timeoutSocket = 30000; // 30 second timeout for obtaining results
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpGet httpget = new HttpGet(URL);
        try {
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity r_entity = response.getEntity();
            jsonReturnText = EntityUtils.toString(r_entity);
        } catch (Exception e) {
            jsonReturnText = e.getMessage();
        }
        String s = "";
        try {
            final JSONObject oData = new JSONObject(jsonReturnText);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonReturnText;
    }

    protected void onPostExecute(String result) {

        string(et_sporingsnummer.getText().toString(), result);


    }

}


}
4

1 回答 1

1

引用您的项目的异常之后的第一行告诉您在哪里查看

at com.example.Posten2.HenteFelt.samleStatuser(HenteFelt.java:128)

所以你NPE在第 128 行有一个HenteFelt

所以那条线上的东西是null。和之前的那一行

at android.app.Activity.findViewById(Activity.java:1853)

使它看起来像是View您正在尝试初始化的。如果您需要帮助,那么您可以在该行周围发布代码,但这就是开始的地方。

于 2013-11-09T19:10:00.267 回答