简体   繁体   English

Android中的R库错误

[英]R library in Android error

I have been using this Android Guide 我一直在使用此Android指南

While it has been a pleasant experience so far, I am experiencing my first problem. 到目前为止,虽然这是令人愉快的经历,但我遇到了第一个问题。 I copied all the code from the source that is in the link, and pasted it to the project folder, replacing all old files. 我从链接中的源代码中复制了所有代码,并将其粘贴到项目文件夹中,替换了所有旧文件。

Before starting to understand what I had pasted, I thought it would be logical to run the code first to check for problems. 在开始理解我粘贴的内容之前,我认为先运行代码以检查问题是合乎逻辑的。 The project wouldn't run because of an R object missing. 由于缺少R对象,因此该项目无法运行。 After importing it (Eclipse's solution to the problem), more errors popped up. 导入它(Eclipse的问题解决方案)后,弹出了更多错误。 I tried searching for an answer, both on the Internet and in the book, but to no avail. 我尝试在Internet和书中都寻找答案,但无济于事。

Since my software is up to date, I doubt this is a problem on the software's side. 由于我的软件是最新的,因此我怀疑这是软件方面的问题。 And since the code is available online, I think the problem would have popped up and been fixed. 而且由于该代码可以在线获得,所以我认为问题会弹出并得到解决。

Thank you in advance for the help. 预先感谢您的帮助。 For extra details please ask in the comments. 有关更多详细信息,请在评论中提出。

The code:

    MainActivity.java

        package com.dummies.android.silentmodetoggle;

        import android.app.Activity;
        import android.graphics.drawable.Drawable;
        import android.media.AudioManager;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.ImageView;

        public class MainActivity extends Activity {

        private AudioManager mAudioManager; 
        private boolean mPhoneIsSilent;

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

            mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);

            checkIfPhoneIsSilent(); 

            setButtonClickListener();

            Log.d("SilentModeApp", "This is a test");
        }    

        private void setButtonClickListener() {
         Button toggleButton = (Button)findViewById(R.id.toggleButton); 
         toggleButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mPhoneIsSilent) {
                    // Change back to normal mode
                    mAudioManager
                            .setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    mPhoneIsSilent = false;
                } else {
                    // Change to silent mode
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    mPhoneIsSilent = true;
                }

                // Now toggle the UI again
                toggleUi();
            }
        }); 
       }

    /**
     * Checks to see if the phone is currently in silent mode. 
     */
    private void checkIfPhoneIsSilent() {
        int ringerMode = mAudioManager.getRingerMode();
        if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
            mPhoneIsSilent = true;
        } else {
            mPhoneIsSilent = false;
        }

    }

    /**
     * Toggles the UI images from silent
     * to normal and vice versa.
     */
    private void toggleUi() {

        ImageView imageView = 
            (ImageView) findViewById(R.id.phone_icon);
        Drawable newPhoneImage;

        if (mPhoneIsSilent) {
            newPhoneImage = 
                getResources().getDrawable(R.drawable.phone_silent);

        } else {
            newPhoneImage = 
                getResources().getDrawable(R.drawable.phone_on);
        }

        imageView.setImageDrawable(newPhoneImage);
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkIfPhoneIsSilent();
        toggleUi();
    };
}

Try cleaning your project, this will rebuild your R file. 尝试清理您的项目,这将重建您的R文件。 If there is still no R file in your file-tree then you may have an error in one your xml layout files. 如果您的文件树中仍然没有R文件,则您的xml布局文件中可能有一个错误。 Eclipse may not tell you this so be vigilant and check through all the files in the /res folder. Eclipse可能不会告诉您这一点,因此请保持警惕,并仔细检查/ res文件夹中的所有文件。

Also, never import R when this happens. 同样,在这种情况下切勿导入R。

您是否检查/res/values/string.xml中是否存在名为action_settings的变量(如果不存在),请创建一个变量,然后使用项目进行清理->清理自动生成的 makeure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM