简体   繁体   English

inPost上的Intent NullPointerException

[英]intent NullPointerException at onPostExecute

i have problem when using Async task to move to acivity when the doInBackground finish 我在doInBackground完成时使用异步任务移动到主动性时遇到问题

it tells me NullPointerException error 它告诉我NullPointerException错误

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... NPI不在AsyncTask上发生,实际上是在创建适配器时在ListActivity1的onCreate方法中发生的,实际上是由于“在android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)”引起的就像您将null值传递给适配器一样...

Hope this helps. 希望这可以帮助。

Regards! 问候!

I think the problem is with your arraylist which could not be passed. 我认为问题出在您无法通过的arraylist上。 In android when you are trying to pass arraylist you need to use parcelable interface. 在android中,当您尝试传递arraylist时,需要使用parcelable接口。 Check this topic out for details: Pass arraylist of user defined objects to Intent android 请查看本主题以获取详细信息: 将用户定义对象的数组列表传递给Intent android

TL;DR version of that topic is to use: 该主题的TL; DR版本将使用:

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

rather than serializable. 而不是可序列化的。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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