空指针异常字符串数组

时间:2015-04-19 18:34:54

标签: android arrays android-asynctask

我正在尝试在我的应用程序中为一个数组分配一些id值,但 Activity 一直在崩溃,我认为这是因为该数组。
在我将它添加到代码之前,数据显示但是一旦我添加它,事情就开始出错了。

以下是活动代码:

private static final String EVENT_POST_URL = "http://10.0.2.2/ZCASPlatform/courseinfo.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_EVENT = "events";
private static final String TAG_POST = "posts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";

String option;
String idnum;
String discuss;
String[] discussId;

private ProgressDialog pDialog;
private JSONArray Details = null;
private ArrayList < HashMap < String, String >> List;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnLogout = (Button) findViewById(R.id.logout);
    TxtFname = (TextView) findViewById(R.id.fnametxt);
    Bundle required = getIntent().getExtras();
    option = required.getString("keyPress");
    idnum = required.getString("keyNum");
    btnLogout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            Intent login = new Intent(getApplicationContext(), LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            finish();
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    new LoadCourses().execute();
}

public void updateJSONdata() {
    List = new ArrayList < HashMap < String, String >> ();
    JSONParser jParser = new JSONParser();
    List < NameValuePair > params = new ArrayList < NameValuePair > ();
    params.add(new BasicNameValuePair("idnumber", idnum));
    params.add(new BasicNameValuePair("pressed", option));
    JSONObject json = jParser.makeHttpRequest(EVENT_POST_URL, "POST", params);
    try {
        if (option.equals("event")) {
            Details = json.getJSONArray(TAG_EVENT);
        } else if (option.equals("forum")) {
            Details = json.getJSONArray(TAG_POST);
        }
        for (int i = 0; i < Details.length(); i++) {
            int pos = 0;
            JSONObject c = Details.getJSONObject(i);
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            discussId[pos] = id;
            HashMap < String, String > map = new HashMap < String, String > ();
            map.put(TAG_ID, id);
            map.put(TAG_NAME, name);
            List.add(map);
            pos++;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public void updateList() {
    ListAdapter adapter = new SimpleAdapter(this, List, R.layout.activty_view_course,
    new String[] {
        TAG_ID, TAG_NAME
    }, new int[] {
        R.id.idnumber, R.id.fullname
    });
    setListAdapter(adapter);
    ListView lv = getListView();
}

public class LoadCourses extends AsyncTask < Void, Void, Boolean > {@Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(EventForumActivity.this);
        pDialog.setMessage("Loading Information");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void...arg0) {
        updateJSONdata();
        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        updateList();
    }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String discuss = discussId[position];
    try {
        Bundle passing = new Bundle();
        passing.putString("discuss", discuss);
        Class openClass = Class.forName("com.example.zcas.ForumActivity");
        Intent newPage = new Intent(EventForumActivity.this, openClass);
        newPage.putExtras(passing);
        startActivity(newPage);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

我的日志猫显示:

04-19 20:23:09.381: E/AndroidRuntime(814): FATAL EXCEPTION: AsyncTask #3
04-19 20:23:09.381: E/AndroidRuntime(814): java.lang.RuntimeException: An error occured while executing doInBackground()
04-19 20:23:09.381: E/AndroidRuntime(814): Caused by: java.lang.NullPointerException
04-19 20:23:09.381: E/AndroidRuntime(814):  at com.example.zcas.EventForumActivity.updateJSONdata(EventForumActivity.java:113)

2 个答案:

答案 0 :(得分:2)

永远不会实例化discussId数组:

discussId = new String[whateverLength];

答案 1 :(得分:1)

在你的for循环之前

for (int i = 0; i < Details.length(); i++)

添加:

discussId = new String[Details.length()];