Error message is: planets_array cannot be resolved or is not a field. I have it declared in my strings.xml file
package com.android.hellospinner1;
import android.app.*;
import android.os.*;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import android.*;
import java.util.*;
public class hellospinner1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(ne开发者_Go百科w MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
Whenever I hit an error message like that, I will just hit Ctrl+Shift+ O
(provided you're using eclipse). It's the short cut to organize imports.
Is there any special reason you import every android package? I'm not 100% sure but I think that this gets you into trouble as it also imports android.R
which is definitely not what you want.
So try to remove import android.*
.
Not sure but the tutorial says create a new file called "arrays.xml" so go with that. I had this same error because I created the file "arrays.xml" inside the folder "layout" instead of in the folder "values". Moved it and problem solved.
Incidentally the tutorial doesn't tell you to put this at the top of your file, but if you don't you'll get an error:
<?xml version="1.0" encoding="utf-8"?>
精彩评论