Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I solve R.layout.main cannot be resolved in my android apps?

Tags:

android

Im new as a developer of Android apps. I find a problem. that is R.layout.main cannot be resolved. How can I solve my problem. my code is here. please solve my problem.

 package com.android;

import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */

    Button bt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        bt=(Button)findViewById(R.id.ButtonOk);
        bt.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //Toast.makeText(getBaseContext(), "Welcome Android World", 3000).show();
            Intent intent = new Intent(HelloActivity.this, DisplayActivity.class);
            startActivity(intent);
            }
        });
    }
}




*

and my xml code is here:

*

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" android:id="@+id/ButtonOk" android:height="50dp" android:width="100dp"></Button>
</LinearLayout>
like image 478
Pothik Avatar asked Jan 07 '13 16:01

Pothik


3 Answers

Step 1 : Remove import android.R;

Step 2 : Clean And Rebuild (It should work)

If not

Then close the project exits eclipse and open again. Follow the steps it should work.

If not

Change your package

From

import android.R;

To

import yourpackage.R;

It Should work

like image 89
vinothp Avatar answered Nov 15 '22 04:11

vinothp


Remove the line import android.R from the header.

and do a Ctrl + Shift + O

If android.R appears again, then manually write

import <yourpackagename>.R

Looks like you used the package name as com.android.R. Ideally speaking, avoid using package names like com.android. Try to maintain it like com.companyname.appname

So you get into that habit, and don't have the pain of changing the package name in all the folders once you are about to publish.

like image 30
nithinreddy Avatar answered Nov 15 '22 05:11

nithinreddy


Import com.android.R not android.R because your package name is com.android

like image 34
Tamás Cseh Avatar answered Nov 15 '22 06:11

Tamás Cseh