【问题标题】:R can not resolve to variableR无法解析为变量
【发布时间】:2013-07-04 18:24:32
【问题描述】:

我是 Android 新手。我正在开发一个简单的注册登录系统。在每个类中,每个 R 出现都有许多相同的错误。错误是 R 无法解析为变量。我在这里发布一个类代码

这是我的代码

package com.UserAccounts;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class SignUPActivity extends Activity
{
    EditText editTextUserName,editTextPassword,editTextConfirmPassword;
    Button btnCreateAccount;

    LoginDataBaseAdapter loginDataBaseAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        // get Instance  of Database Adapter
        loginDataBaseAdapter=new LoginDataBaseAdapter(this);
        loginDataBaseAdapter=loginDataBaseAdapter.open();

        // Get Refferences of Views
        editTextUserName=(EditText)findViewById(R.id.editTextUserName);
        editTextPassword=(EditText)findViewById(R.id.editTextPassword);
        editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);

        btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
        btnCreateAccount.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            String userName=editTextUserName.getText().toString();
            String password=editTextPassword.getText().toString();
            String confirmPassword=editTextConfirmPassword.getText().toString();

            // check if any of the fields are vaccant
            if(userName.equals("")||password.equals("")||confirmPassword.equals(""))
            {
                    Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                    return;
            }
            // check if both password matches
            if(!password.equals(confirmPassword))
            {
                Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
                return;
            }
            else
            {
                // Save the Data in Database
                loginDataBaseAdapter.insertEntry(userName, password);
                Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
            }
        }
    });
}
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        loginDataBaseAdapter.close();
    }
}

【问题讨论】:

标签: android android-layout android-emulator


【解决方案1】:

如果您的类与清单中声明的​​应用包位于不同的包中,则需要导入应用的 R 类。表格应该是

import your_app_package_name_as_defined_in_the_manifest.R;

请注意,如果您在项目的任何资源文件中出现错误,则不会生成 R.java 文件并且不会存在 R 类。如果是这种情况,请解决资源文件错误,未定义的R 类错误将消失。

【讨论】:

    【解决方案2】:

    检查您的 res 文件夹中没有错误,然后对您的项目进行清理(Project-->Clean)

    【讨论】:

      【解决方案3】:

      您需要导入 yourpackage.R 然后错误将得到解决...我认为这将类似于 import com.UserAccounts.R

      【讨论】:

        【解决方案4】:

        首先我是“import your_app_package_name_as_defined_in_the_manifest.R;”之后,我按 Project -> Clean,然后完成。谢谢!

        【讨论】:

          【解决方案5】:

          将自己的资源文件导入自己的包是我最不想做的事情。你不应该 求助于这个IMO! (不过,我以前也用过这个!伪君子!)

          我已经多次遇到这个问题,它是由与我的 xml 文件相关的不同因素引起的。

          检查: * 您在调用 Inflator.inflate...R.id.my_resource.xml 的任何 onCreatView() 方法中是否有拼写错误。

          *查看布局编辑器中的“设计”选项卡。检查它们是否都指向相同的 android 版本。像 23 等。

          *在布局编辑器中检查您正在使用的主题。

          *有时您可能在 xml 中有一个 Include 标记,但它并不指向要包含的另一个 xml 文件。

          *继续查看 xml 和 inflators,有时给定的布局似乎甚至没有 问题,用红线表示。

          *如果您仍然无法解决问题,请在其他资源文件中查找拼写错误和错误。

          *您可能还必须在每次进行更改时运行“清理”。

          *休息一下,稍后再回来。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-01-12
            相关资源
            最近更新 更多