在分析另一个活动中的列表时调用虚方法时的空对象引用

时间:2017-02-15 07:42:47

标签: java android nullpointerexception

我在activity_main_one中保留了一个列表视图,列表由一个数组填充。现在我在activity_main_page中保留了一个搜索视图来搜索该列表。但相反,我得到一个错误:

Attempt to invoke virtual method 'void com.example.abc.hotelomania.MainActivity1.initList()' on a null object reference

activity_main_one的代码位于MainActivity1.class

MainActivity1.class

public class MainActivity1 extends ActionBarActivity {

String[] items;
ArrayList<String> listItems;
ArrayAdapter<String> adapter;
ListView listView;
EditText editText;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_one);
    initList();
    items=new String[]{"Canada","China","Japan","USA"};
        listItems=new ArrayList<>(Arrays.asList(items));
}

public void initList(){
    adapter=new ArrayAdapter<String>(this,
            R.layout.list_item, R.id.txtitem, listItems);
    listView.setAdapter(adapter);
}
}

MainPage.class

   public class MainPage extends Activity {
   MainActivity1 mn;
   String[] items;
   ArrayList<String> listItems;
   ArrayAdapter<String> adapter;
   ListView listView;
   EditText editText;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_page);
      items=new String[]{"Canada","China","Japan","USA"};

      listItems=new ArrayList<>(Arrays.asList(items));
      listView=(ListView)findViewById(R.id.listview);
    editText=(EditText)findViewById(R.id.txtsearch);
    mn.initList();
    editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
       if(s.toString().equals("")){
                // reset listview
                mn.initList();
       }
       else {
                // perform search
                searchItem(s.toString());
       }
     }
     @Override
     public void afterTextChanged(Editable s) {
     }
  });
}
  public void searchItem(String textToSearch){
    for(String item:items){
        if(!item.contains(textToSearch)){
            listItems.remove(item);
        }
    }
    adapter.notifyDataSetChanged();
    }

1 个答案:

答案 0 :(得分:0)

MainActivity.java看起来像

public class MainActivity1 extends ActionBarActivity {

String[] items;
ArrayList<String> listItems;
ArrayAdapter<String> adapter;
ListView listView;
EditText editText;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_one);
    initList();
}

public void initList() {
    items = new String[]{"Canada", "China", "Japan", "USA"};
    listItems = new ArrayList<>(Arrays.asList(items));
    adapter = new ArrayAdapter<String>(this,
            R.layout.list_item, R.id.txtitem, listItems);
    listView.setAdapter(adapter);
}

}