可以指出空指针异常1

时间:2013-05-04 16:13:39

标签: android nullpointerexception

我编辑了代码,现在空指针错误指向for循环,它们的错误比之前少,但我仍然不知道为什么调用nullpointer。

 public class searchlist extends ListActivity 
 {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    new loadSomeStuff().execute();
}

public class loadSomeStuff extends AsyncTask<String, Integer, String[]>
{
  ProgressDialog dialog;

  protected void onPreExecute()
  {
     dialog = new ProgressDialog(searchlist.this); 
     dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     dialog.setMax(100);
     dialog.show();
  }
    @Override
    protected String[] doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        for(int i =0; i<20; i++)
        {
          publishProgress(5);
          try
          {
          Thread.sleep(80);
          } catch(InterruptedException e)
          {
              e.printStackTrace();
          }
        }
        dialog.dismiss();
        int loops = search_page.returnlooped();
        int[] teacup = search_page.returnNumArray();
        sqlStuff searching = new sqlStuff(searchlist.this);
        String[] IDSysNames = searching.getIDSysName();
        searching.close();

        String[] resultList = new String[loops];
        for(int i=0; i < loops; i++ )
        {
            if(IDSysNames[teacup[i]] != null)
            {

            resultList[i].equals(IDSysNames[teacup[i]]);

            }
        }
         setListAdapter(new ArrayAdapter<String>(searchlist.this, android.R.layout.simple_list_item_1, resultList));
        return null;
    }

    protected void onProgressUpdate(Integer...progress)
    {
        dialog.incrementProgressBy(progress[0]);
    }
}





@Override
public void onListItemClick(ListView l, View v, int position, long id){
     //TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    //String Pos = resultList[position];

    Intent ourIntent = new Intent("com.MC.ChemPal.RESULT2");
    startActivity(ourIntent);
}
}   

上面代码中使用的方法如下。

public String[] getIDSysName()
{
    String[] result = new String[0];

    try
    {
        String[] columns = new String[] {KEY_SYSNAME};
        Cursor c  =  ChemPal.query(DATABASE_TABLE, columns, null, null, null, null, null);
        Log.d("SqlDStuff", "Cursor count: "+c.getCount());
        int iSysName = c.getColumnIndex(KEY_SYSNAME);

        int i = 0;
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            i++;
        }

        result = new String[i];
        i = 0;
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result[i] = c.getString(iSysName);
            i++;

        }
    }
    catch(Exception e)
    {   

    }

    return result;
}

1 个答案:

答案 0 :(得分:0)

你不能这样做

int loops = search_page.returnlooped();
int[] teacup = search_page.returnNumArray();

onCreate()之前,因为您正在尝试访问仍未初始化的应用程序资源(因此是NPE)。如您所见[{3}},您无法假设活动在调用onCreate()之前启动。

  

活动的整个生命周期都发生在第一次调用之间   onCreate(Bundle)到onDestroy()的最后一次调用。

作为一般规则:在调用onCreate()之前,您不应该任何

此外,您不应该在onCreate()中进行数据库读取,因为这是由UI线程运行的,如果数据库访问太长,您可能会得到ANR。请参阅here在后台执行任务(如果您的GUI需要立即加载数据,则可能会在加载数据时考虑AsyncTasks。)