简体   繁体   中英

intent NullPointerException at onPostExecute

i have problem when using Async task to move to acivity when the doInBackground finish

it tells me NullPointerException error

do anyone have any idea how to slove it ?

my full Async task class :

public class Hashom extends AsyncTask<Void, Void, Pair> {

static Activity mActivity;
ProgressDialog progressDialog;

public Hashom(Activity activity, ProgressDialog progressDialog) {
    super();
    this.progressDialog = progressDialog;
    Hashom.mActivity = activity;

}

final String NAMESPACE = "http://ws.sams.com";
final String URL = "http://88.198.82.92:8080/sams1/services/listActivityWS?WSDL"; // usint
                                                                                    // localhost
final String METHOD_NAME = "getCurrentClassList";
final String SOAP_ACTION = "http://ws.sams.com/getCurrentClassList";

final String NAMESPACE2 = "http://ws.sams.com";
final String URL2 = "http://88.198.82.92:8080/sams1/services/listActivityWS?WSDL"; // usint
                                                                                    // localhost
final String METHOD_NAME2 = "getStudentId";
final String SOAP_ACTION2 = "http://ws.sams.com/getStudentId";

final String NAMESPACE3 = "http://ws.sams.com";
final String URL3 = "http://88.198.82.92:8080/sams1/services/listActivityWS?WSDL"; // usint
                                                                                    // localhost
final String METHOD_NAME3 = "getCourseName";
final String SOAP_ACTION3 = "http://ws.sams.com/getCourseName";

final String NAMESPACE4 = "http://ws.sams.com";
final String URL4 = "http://88.198.82.92:8080/sams1/services/listActivityWS?WSDL"; // usint
                                                                                    // localhost
final String METHOD_NAME4 = "getClassId";
final String SOAP_ACTION4 = "http://ws.sams.com/getClassId";

public static ArrayList<Student> StudentIdList = new ArrayList<Student>();
public static ArrayList<Student> StudentNamesList = new ArrayList<Student>();

@Override
protected Pair doInBackground(Void... voids) {
    Pair p = new Pair();
    // ///////////////////////////////////////////
    SoapObject request = new SoapObject(NAMESPACE4, METHOD_NAME4);
    PropertyInfo pi = new PropertyInfo();
    pi.setName("TID");
    pi.setValue(1);
    pi.setType(Integer.class);
    request.addProperty(pi);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL4);
    try {
        androidHttpTransport.call(SOAP_ACTION4, envelope);
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

        // ///////////////////////////////////////////////////////////////////////

        SoapObject request2 = new SoapObject(NAMESPACE3, METHOD_NAME3);
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("TID");
        pi2.setValue(1);
        pi2.setType(Integer.class);
        request2.addProperty(pi2);
        SoapSerializationEnvelope envelope2 = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope2.setOutputSoapObject(request2);
        HttpTransportSE androidHttpTransport2 = new HttpTransportSE(URL3);

        androidHttpTransport2.call(SOAP_ACTION3, envelope2);
        SoapPrimitive response2 = (SoapPrimitive) envelope2.getResponse();

        // /////////////////////////////////////////////////////////////////////////////////////////////////

        SoapObject request3 = new SoapObject(NAMESPACE, METHOD_NAME);
        HttpTransportSE androidHttpTransport3 = new HttpTransportSE(URL);
        PropertyInfo pi3 = new PropertyInfo();
        pi3.setName("TID");
        pi3.setValue(1);
        pi3.setType(Integer.class);
        request3.addProperty(pi3);

        SoapSerializationEnvelope envelope3 = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        androidHttpTransport3.call(SOAP_ACTION, envelope3);

        KvmSerializable result = (KvmSerializable) envelope3.bodyIn;

        String str = null;
        for (int i = 0; i < result.getPropertyCount(); i++) {
            str = ((String) result.getProperty(i).toString());

            Student hesham = new Student(str);
            StudentNamesList.add(hesham);

        }
        p.names = StudentNamesList;

    } catch (Exception e) {

    }

    // ////////////////////////////////////////////////////////////////////////////////////

    SoapObject request4 = new SoapObject(NAMESPACE2, METHOD_NAME2);
    PropertyInfo pi4 = new PropertyInfo();
    pi4.setName("TID");
    pi4.setValue(1);
    pi4.setType(Integer.class);
    request.addProperty(pi4);
    SoapSerializationEnvelope envelope4 = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request4);
    HttpTransportSE androidHttpTransport4 = new HttpTransportSE(URL2);

    try {

        androidHttpTransport4.call(SOAP_ACTION2, envelope4);

        KvmSerializable result = (KvmSerializable) envelope.bodyIn;

        String str = null;
        for (int i = 0; i < result.getPropertyCount(); i++) {
            str = ((String) result.getProperty(i).toString());

            Student hesham = new Student(str);

            StudentIdList.add(hesham);

        }
        p.ids = StudentIdList;
    }

    catch (Exception ex) {
    }
    return p;
}

@Override
protected void onPostExecute(Pair p) {

    Intent intent = new Intent(mActivity.getApplicationContext(),
            ListActivity1.class);
    intent.putExtra("StudentIdList", p.ids);
    intent.putExtra("StudentNamesList", p.names);

    mActivity.startActivity(intent);

    progressDialog.dismiss();
}

}

my Activity class :

     public class ListActivity1 extends ListActivity{


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

    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_list);
    // Create an array of Strings, that will be put to our ListActivity
    final InteractiveArrayAdapter adapter = new InteractiveArrayAdapter(
            this, getNames(), getIds());

    setListAdapter(adapter);
    public List<Student> getNames() {

    @SuppressWarnings("unchecked")
    ArrayList<Student> selectedMembers = (ArrayList<Student>) getIntent().getSerializableExtra ( "StudentNamesList" );

    return selectedMembers;

}


private List<Student> getIds() {


    @SuppressWarnings("unchecked")
    ArrayList<Student> selectedMembers2 = (ArrayList<Student>) getIntent().getSerializableExtra ( "StudentIdList" );

    return selectedMembers2;

} // end of on create

  } // end of class

ErrorLog :

       09-05 01:03:38.891: E/AndroidRuntime(21144): FATAL EXCEPTION: main
  09-05 01:03:38.891: E/AndroidRuntime(21144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hesham.sams/com.hesham.sams.ListActivity1}: java.lang.NullPointerException
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.ActivityThread.access$700(ActivityThread.java:140)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.os.Handler.dispatchMessage(Handler.java:99)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.os.Looper.loop(Looper.java:137)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.ActivityThread.main(ActivityThread.java:4921)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at java.lang.reflect.Method.invokeNative(Native Method)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at java.lang.reflect.Method.invoke(Method.java:511)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at dalvik.system.NativeStart.main(Native Method)
  09-05 01:03:38.891: E/AndroidRuntime(21144): Caused by: java.lang.NullPointerException
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.widget.ListView.setAdapter(ListView.java:466)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.ListActivity.setListAdapter(ListActivity.java:265)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at com.hesham.sams.ListActivity1.onCreate(ListActivity1.java:48)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.Activity.performCreate(Activity.java:5206)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
  09-05 01:03:38.891: E/AndroidRuntime(21144):  ... 11 more

The NPI is not happending on the AsyncTask, is actually happening in the onCreate method of the ListActivity1 by the time you create the Adapter, is actually being caused here "at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)" seems like you are passing a null value to the adapter...

Hope this helps.

Regards!

I think the problem is with your arraylist which could not be passed. In android when you are trying to pass arraylist you need to use parcelable interface. Check this topic out for details: Pass arraylist of user defined objects to Intent android

TL;DR version of that topic is to use:

intent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
getIntent().getParcelableArrayListExtra("key");

rather than serializable. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM