SetContentView (R.layout.main); Home cannot be resolved or not field

This is a simple program to start (just for you, not for me) eclipse. I want to change the text by clicking on the button, but it does not work.

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..."); } }); } } 
+4
source share
4 answers

First, just create your own project. If this does not work, remove android.R.layout from import and use the layout from your package.

+9
source

First check the name of your XML layout ( YourProject/res/layout/your_main.xml in 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'm just a beginner. Maybe I'm wrong, but that solved my problem.

+4
source
 setContentView(R.layout.Main); 

Do you have a layout called Main? as far as I know, a letter in Capital does not fit well with the names of the layouts. If you use it, you will get some error like this

File-based file names must begin with a lowercase letter.

Check and rename the layout file.

+3
source

just type: import android.R.layout; This solved my problem, hoping that it will also solve your problem.

0
source

Source: https://habr.com/ru/post/1437665/


All Articles