Johncbell Johncbell - 4 months ago 46
Java Question

Android: Class 'MainActivity2' must either be declared abstract or implement abstract method 'onNothingSelected(AdapterView<?>'

I am getting Two Errors:
First On the Class

public class MainActivity2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {



"Android: Class 'MainActivity2' must either be declared abstract or
implement abstract method 'onNothingSelected(AdapterView' in
'"OnItemSelectedListener'"


The Second one is on the setOnClickListener(this);

//Adding listener to button
buttonRegister.setOnClickListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}


Error: In View cannot be applied

Code from MainActivity2.java

//Class for our main activity with OnClickListener
public class MainActivity2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

private WebView webView;
private EditText trucksID;
private EditText tripReportNumber;
public EditText enteredDate;
private EditText emptyMilage;
private EditText loadedMilage;
private Button buttonRegister = (Button) findViewById(R.id.buttonRegister);
private TextView estMilage;
public String startState;
public String startTerminal;
public String endState;
public String endTerminal;

Spinner s1,s2,s3,s4;
public String sp1t;
//Declaring views
//This is our root url
public static final String ROOT_URL = "http:...wp-content/php/";
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
s1 = (Spinner) findViewById(R.id.spinner1);
s2 = (Spinner) findViewById(R.id.spinner2);
s3 = (Spinner) findViewById(R.id.spinner3);
s4 = (Spinner) findViewById(R.id.spinner4);
s1.setOnItemSelectedListener(this);
s3.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1 = String.valueOf(s1.getSelectedItem());
startState = sp1;
//The spinners for State and Each Start Terminal broken down by State Code
if (sp1.contentEquals("AL")) {
List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.ALTerminalCodes));

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myArrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
startTerminal = String.valueOf(s2.getSelectedItem());
}
if (sp1.contentEquals("AZ")) {
List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.AZTerminalCodes));
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myArrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
startTerminal = String.valueOf(s2.getSelectedItem());
}


// This is the Code for the Third Spinner State / End Terminal coding and selection

String sp3 = String.valueOf(s3.getSelectedItem());
endState = sp3;
//The spinners for State and Each Start Terminal broken down by State Code
if (sp3.contentEquals("AL")) {
List<String> myArrayList2 = Arrays.asList(getResources().getStringArray(R.array.ALTerminalCodes));
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myArrayList2);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s4.setAdapter(dataAdapter4);
endTerminal = String.valueOf(s4.getSelectedItem());
}
if (sp3.contentEquals("AZ")) {
List<String> myArrayList2 = Arrays.asList(getResources().getStringArray(R.array.AZTerminalCodes));
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myArrayList2);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s4.setAdapter(dataAdapter4);
endTerminal = String.valueOf(s4.getSelectedItem());
}

//Initializing Views
trucksID = (EditText) findViewById(R.id.trucksID);
tripReportNumber = (EditText) findViewById(R.id.tripReportNumber);
enteredDate = (EditText) findViewById(R.id.enteredDate);
emptyMilage = (EditText) findViewById(R.id.emptyMilage);
loadedMilage = (EditText) findViewById(R.id.loadedMilage);
estMilage = (TextView) findViewById(R.id.estMilage);

//Reset Button for Form
Button reset= (Button) findViewById(R.id.btnclear);

assert reset != null;
reset.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
trucksID.setText("");
tripReportNumber.setText("");
enteredDate.setText("");
emptyMilage.setText("");
loadedMilage.setText("");
estMilage.setText("");

}
});

//Adding listener to button
buttonRegister.setOnClickListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.openWebsite:
Intent website = new Intent(this, ourWebsite.class);
this.startActivity(website);
return true;

case R.id.about_us:
Intent about = new Intent(this, aboutUs.class);
this.startActivity(about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

private void insertUser() {
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL) //Setting the Root URL
.build(); //Finally building the adapter

//Creating object for our interface
RegisterAPITest api = adapter.create(RegisterAPITest.class);

//Defining the method insertuser of our interface

int value1 = Integer.parseInt(emptyMilage.getText().toString());
int value2 = Integer.parseInt(loadedMilage.getText().toString());
int estMilage = value2 + value1;

api.insertUser(
//Passing the values by getting it from editTexts
trucksID.getText().toString(),
tripReportNumber.getText().toString(),
enteredDate.getText().toString(),
emptyMilage.getText().toString(),
loadedMilage.getText().toString(),

// String emptyLocation = spinner.getSelectedItem().toString(),

//can take this out below
Integer.toString(estMilage),
// Addign in Locaiton Spinners and assigning to DB
startState,
startTerminal,
endState,
endTerminal,

//Creating an anonymous callback
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
//On success we will read the server's output using bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;

//An string to store output from the server
String output = "";

try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(result.getBody().in()));

//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}

//Displaying the output as a toast
Toast.makeText(MainActivity2.this, output, Toast.LENGTH_LONG).show();
}

@Override
public void failure(RetrofitError error) {
//If any error occured displaying the error as toast
Toast.makeText(MainActivity2.this, error.toString(), Toast.LENGTH_LONG).show();
}
}
);
}

//Overriding onclick method
@Override
public void onClick(View v) {
//Calling insertUser on button click
insertUser();

}

@Override
public void onStart() {
super.onStart();

// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.johncbell.atrixtrucking/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
super.onStop();

// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.johncbell.atrixtrucking/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}

Answer

Error 1 is almost pretty clear.. the message asking you to implement/override the method onNothingSelected