List显示为非null但ArrayAdapter抛出NullPointerException

时间:2015-06-11 08:38:11

标签: java android android-listview nullpointerexception android-arrayadapter

任何人都可以帮助我吗?我只是不明白为什么它会抛出空指针异常。  我使用kso​​ap2,WCF web方法和android。我检查了"列表"在调试器模式下它不是null。

这是我的代码:

public class MainActivity  extends Activity {

    String loginMemberId = "a001";
    Context context;
    List list;
    ListView listView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                getBeneficiariesNameSet BNL = new getBeneficiariesNameSet();
                try {
                    list = BNL.execute().get();

                    if(list!=null && list.size()>0) {
                        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list); //null pointer exception,line 43
                        listView = (ListView) findViewById(R.id.listBeneficiaries);
                        listView.setAdapter(arrayAdapter);
                    }else{
                        Toast.makeText(context,"No data in array",Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    public class getBeneficiariesNameSet extends AsyncTask<String, Void, List<String>> {

        private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

        @Override
        protected List<String> doInBackground(String... params) {
            TestArray CS = new TestArray();
            List<String> response = CS.getBeneficiariesNameSet(loginMemberId);

            return response;

        }

        @Override
        protected void onPostExecute(List<String> s) {
            super.onPostExecute(s);
            dialog.dismiss();

        }
    }


}

这是 Log cat

06-11 14:22:20.484  22245-22245/? W/System.err﹕ java.lang.NullPointerException
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at com.example.administrator.androidwcf.MainActivity$1.onClick(MainActivity.java:43)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at android.view.View.performClick(View.java:4443)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at android.view.View$PerformClick.run(View.java:18442)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5021)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
06-11 14:22:20.484  22245-22245/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
06-11 14:22:20.514      599-624/? W/audio_hw_primary﹕ out_write() limiting sleep time 112539 to 46439
06-11 14:22:20.584      599-624/? W/audio_hw_primary﹕ out_write() limiting sleep time 65759 to 46439

1 个答案:

答案 0 :(得分:1)

您的contextnull。使用MainActivity.this作为活动的匿名内部类中的上下文。