R.layout.main cannot be resolved解决办法

今天敲的代码

[java] view plaincopy
 
  1. package com.sharpandroid.activity;  
  2.   
  3. import android.R;  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.content.DialogInterface;  
  7. import android.content.Intent;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12.   
  13. public class AlertDialogActivity extends Activity {  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         Button button = (Button)findViewById(R.id.button);  
  20.         button.setOnClickListener(new View.OnClickListener() {  
  21.               
  22.         @Override  
  23.         public void onClick(View v) {  
  24.                 AlertDialog.Builder builder =   
  25.         new AlertDialog.Builder(AlertDialogActivity.this);  
  26.                 builder.setTitle("sharpandroid")  
  27.                         .setMessage("你确定要访问Android网站吗?")  
  28.                         .setCancelable(false)  
  29.                         .setPositiveButton("确定",  
  30.         new DialogInterface.OnClickListener() {  
  31.                         public void onClick(DialogInterface dialog,int id){  
  32.                             Intent intent =  
  33.         new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.android.com"));  
  34.                                 startActivity(intent);  
  35.                             }})  
  36.                             .setNegativeButton("取消",  
  37.         new DialogInterface.OnClickListener(){  
  38.                                 public void onClick(DialogInterface dialog,  
  39.                         int id){  
  40.                                     dialog.cancel();  
  41.                                 }  
  42.                             });  
  43.                 AlertDialog alert = builder.create();  
  44.                 alert.show();  
  45.                   
  46.             }  
  47.         });  
  48.     }  
  49. }  

出现如下问题:

鼠标放到出代码上面:

分析问题:

1、查看R文件是否被生成,如果没有生成,则勾选build Automatically,然后Clean:

2、如果R文件已生成,则删除去掉代码中:

[java] view plaincopy
 
  1. import android.R;  

重新build问题解决。

备注: 删除"import android.R"之后工程就是从/res文件夹下自动生成的资源文件里去解析了,否则它会从Android的资源类里去找。   

原文地址:https://www.cnblogs.com/xunbu7/p/4195474.html