I am new to the mobile development world and I am currently trying to create an application that allows someone to look up an acronym and see what it means as well as add an acronym to the library if it is nonexistent. To do this, I was trying to use a HashMap. I noticed that my application would create a new instance of the HashMap every time it was opened so some values needed to be predefined. I followed the recommendation given in this answer but now my application crashes every time it is opened.
Also, in the process of trying to troubleshoot, my startActivity has stopped working. It tells me "cannot resolve method 'startActivity(android.content.Intent)'" when I hover over startActivity and "cannot resolve constructor 'Intent(com.private.CSRenA.acronymlookup.Search, java.lang.Class)'" so now all of my java files have stopped working.
Here is my code:
package com.private.CSRenA.acronymlookup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import java.util.HashMap;
public class Search extends AppCompatActivity {
public EditText mEdit;
public HashMap<String,String> acros = new HashMap<String,String>(){{
//add all currently known thingies.
acros.put("ASAP","As Soon As Possible");
}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mEdit = (EditText)findViewById(R.id.acroterm);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setImageResource(R.drawable.ic_search);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(acros.containsKey(mEdit.getText().toString())){
startActivity(new Intent(Search.this, ValidAcronym.class));
}
else{
startActivity(new Intent(Search.this, InvalidAcronym.class));
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public HashMap<String,String> getMap(){
return acros;
}
public void addAcro(String i, String s) {
if(!acros.containsKey(i)){
acros.put(i,s);
}
else{
startActivity(new Intent(Search.this, AlreadyExists.class));
}
}
}
Please help if you can! I'm completely lost with what I'm doing wrong.
NOTE: Package name has been changed to protect my identity and profession.