[java] setContentView(R.layout.main); error

package com.elfapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private Button btn_Login;
    private EditText et_UserName;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_Login = (Button) findViewById(R.id.button_login);
        btn_Login.setOnClickListener(this);

        et_UserName = (EditText) findViewById(R.id.editText_userName);
    }

    public void onClick(View v) {
        if (v.equals(btn_Login)) {
                // skriver ut en toast när man klickar på knappen
            Toast.makeText(MainActivity.this, "Ansluter till server...", Toast.LENGTH_SHORT).show();

                // används i debuggern för att påvisa att programmet exekverat hit
            Log.v("ThisApp", "onClick Successful");

                // TODO skickar det som står i et_UserName till controller (genom TCP/IP), som ska kolla om användaren finns
            // send et_UserName.getText().toString() to controller

                // if(username exists)
            Intent intent = new Intent(this, RoomActivity.class);
            this.startActivity(intent);
        }
    }

}

I'm getting an error on the line containing setContentView(R.layout.main); Not sure about what the error/exception is because I'm not used to working in Eclipse..

This question is related to java android eclipse

The answer is


is this already solved?

i also had this problem. I solved it just by cleaning the project.

Project>Clean>Clean projects selected below>Check [your project's name]


Simply:

  1. Right click on your project.

  2. Go to properties.

  3. Select android (second option in the Left panel).

  4. Click "add..." (in library), select your project.

  5. Click ok.

  6. And finally, clean your project.

If this doesn't work, make sure that "android-support-v7-appcompat" is in your Project Explorer.

If it isn't there, you can add it by importing a simple project from: C:/android-sdks\extras\android\support\v7\appcompat


This just happend to me a minute ago, but after researching a while, and read this post I notice this.

There is a custom R class with you app name, so when you try to import the missing class (in Eclipse, press Ctrl + Shift + O to import missing classes (Cmd + Shift + O on Mac)), you should see two posible classes the normal:

import android.R;

And a custom class with your project namespace:

import com.yourname.yourapp.R;

If you choose the custom class, problem solved!


Step 1 : import android.*;

Step 2 : clean your project

Step 3 : Enjoy !!!


use code : setContentView(R.layout.activity_main); instead ofsetContentView(R.layout.main);


if you have multiple packages with different classes then it will be confusing: try this:

import package_name_from_AndroidManifest.R;


This problem usually happen if eclipse accidentally compile the main.xml incorrectly. The easiest solution is to delete R.java inside gen directory. Once we delete, than eclipse will generate the new R.java base on the latest main.xml


Using NetBeans 7.0:

If you fix imports before R.java has been generated for your project (before building it the first time) it will add the line:

import android.R;

which will override the local R.java that you are trying to reference.

Deleting that line resolved the errors for me.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat