max
max

Reputation: 6187

setContentView(R.layout.Main); Main cannot be resolved or is not a field

This is a simple program for beginning (simple for you, not for me) eclipse. I want to change a text by clicking on a button, but it's not working.

Here is the code:

package com.example.androidcourse;  

import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
import android.R.layout;  

public class MainActivity extends Activity {  

@Override 
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.Main);  


Button btn = (Button) findViewById(R.id.but_action); 
final TextView text = (TextView) findViewById(R.id.txt_caption);  
btn.setOnClickListener(new OnClickListener() {  

@Override  
public void onClick(View v) {
text.setText("New bingo...");  
}  
});  
}  
}  

Upvotes: 4

Views: 31152

Answers (5)

Saddam Hussain
Saddam Hussain

Reputation: 1

Remove all imports and regenerate, by alt+ Enter on every class that require import , this worked for me.

Upvotes: -1

Sneha Bhalerao
Sneha Bhalerao

Reputation: 1

just type: import android.R.layout; This solved my problem hope it solves your problem too.

Upvotes: 0

jack7en
jack7en

Reputation: 151

First check the name of your layout XML (YourProject/res/layout/your_main.xml of your project folder).

replace:

setContentView(R.layout.**Main**);

with:

setContentView(R.layout.**your_main**);   // 'your_main' Your layout xml file name.

Mine was activity_main.xml, which I replaced with "main".

I am just a beginner. I may be wrong but this solved my problem.

Upvotes: 4

ania
ania

Reputation: 2350

First just build your project. If it will not work, then delete from imports android.R.layout and use layout from your package.

Upvotes: 9

Sunny Kumar Aditya
Sunny Kumar Aditya

Reputation: 2846

setContentView(R.layout.Main);

DO you have layout named Main ? as far as I know Capital letter do not mix well with layout names. If you use it you will get some error like this

File-based resource names must start with a lowercase letter.

Please check and rename your layout file .

Upvotes: 4

Related Questions