onPostExecute中的intent NullPointerException

时间:2013-09-04 23:14:34

标签: java android android-intent android-asynctask

使用Async任务在doInBackground完成时移动到acivity时出现问题

它告诉我NullPointerException错误

有谁知道如何解决它?

我的完整异步任务类:

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();
}

}

我的活动课程:

     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

2 个答案:

答案 0 :(得分:0)

NPI没有发生在AsyncTask上,实际上是在你创建适配器时在ListActivity1的onCreate方法中发生的,实际上是在这里引起的&#34;在android.widget.ArrayAdapter.getCount(ArrayAdapter。的java:330)&#34;好像你正在将null值传递给适配器......

希望这有帮助。

问候!

答案 1 :(得分:0)

我认为问题在于你的arraylist无法通过。在android中,当你试图传递arraylist时,你需要使用parcelable接口。有关详细信息,请查看此主题: Pass arraylist of user defined objects to Intent android

TL;该主题的DR版本将使用:

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

而不是可序列化。祝你好运!