Reputation: 1895
I'm trying to follow this tutorial from Google to create your own android app with Android Studio. But when I follow the 4th step on this page: http://developer.android.com/training/basics/firstapp/starting-activity.html Android Studio ends up with this error:
Cannot resolve symbol 'View'
This is what my code looks like at the moment:
public class MainActivity extends ActionBarActivity {
/** Called when the user clicks the Send button */
public void sendMessage(View view) { <--- (This line ends up with the error)
// Do something in response to button
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
What's wrong with this code? I'm not experienced in java and after looking at other questions I still couldn't find the right solution.
Thanks for helping!
Upvotes: 24
Views: 63317
Reputation: 11
This problem can be resolved easily either by pressing alt + enter on the error to import android.view.View or by cross-checking that your method is outside protected void onCreate(Bundle savedInstanceState)
and in the class parenthesis.
Upvotes: 1
Reputation: 31
I am doing the same tutorial and ran into the same problem (that's why I found this question).
I see they explain this issue in the next paragraph named "Build an Intent":
Android Studio will display Cannot resolve symbol errors because this code references classes that are not imported. You can solve some of these with Android Studio's "import class" functionality by pressing Alt + Enter (or Option + Return on Mac). Your imports should end up as the following:
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText;
Upvotes: 3
Reputation: 1385
I think you forget to include the import statement for View. Add the following import in your code
import android.view.View;
Upvotes: 60