简体   繁体   English

如何从匿名内部类方法访问资源 ID?

[英]How to access resource id from anonymous inner class method?

  • I created a new project with empty activity selected.我创建了一个新项目,并选择了空活动。
  • Then I added a new layout file called layout_extra.xml with id of layout_extra and it just contains one switch with id of switch1 .然后,我添加一个新的名为布局文件layout_extra.xmlidlayout_extra ,它只是包含一个交换机idswitch1
  • The activity_main.xml contains a button with id of button to navigate to the layout_extra . activity_main.xml包含一个按钮, idbutton以导航到layout_extra
  • The content of MainActivity.java is as follows: MainActivity.java的内容如下:
public class MainActivity extends AppCompatActivity {

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

        findViewById(R.id.button).setOnClickListener(v->{
            setContentView(R.layout.layout_extra);
        });

        findViewById(R.id.switch1).setOnClickListener(v->{
            findViewById(R.layout.layout_extra).setBackgroundColor(Color.RED);
        });
    }
}

Question问题

I got the following error on the Android Studio.我在 Android Studio 上收到以下错误。

在此处输入图片说明

The tooltip gives the following info.工具提示提供以下信息。

在此处输入图片说明

I did all the given suggestions but none works (the application crashes).我做了所有给定的建议,但没有奏效(应用程序崩溃)。

How to properly call resource id from within an anonymous class method?如何从匿名类方法中正确调用资源 ID?

Hum, the method "findViewById()" is asking the ID of the Object not the Resource.嗯,方法“findViewById()”正在询问对象的 ID 而不是资源。

Basically what i believe you should do is set the "ID" in the "XML" of your main layout, than you can use the method "findViewById()", any object has an id in "XML" even the layout can be set an ID.基本上我认为你应该做的是在你的主布局的“XML”中设置“ID”,而不是你可以使用方法“findViewById()”,任何对象在“XML”中都有一个 id,甚至可以设置布局一个身份证。

Ex: Set in the XML the id of you main object:例如:在 XML 中设置主对象的 id:

android:id="@+id/layout_extra" android:id="@+id/layout_extra"

in the code:在代码中:

findViewById(R.id.layout_extra).setBackgroundColor(Color.RED); findViewById(R.id.layout_extra).setBackgroundColor(Color.RED);

I think I have got the error.我想我有错误。 You are calling setOnClickListener() on a null object.您正在对空对象调用setOnClickListener() Hence, the updated code should be:因此,更新后的代码应该是:

public class MainActivity extends AppCompatActivity {

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

        // The following does not set the layout 'layout_extra' as root view until the button press
        findViewById(R.id.button).setOnClickListener(v->{
            // This block executes on button press
            setContentView(R.layout.layout_extra);
            final View layout_extra = findViewById(R.id.layout_extra);
            
            findViewById(R.id.switch1).setOnClickListener(v2->{
                layout_extra.setBackgroundColor(Color.RED);
            });
        });
    }
}

where R.id.layout_extra refers to the id of the root element of layout_extra.xml .其中R.id.layout_extraid的根元素的layout_extra.xml

The setOnClickListener() use a new view so findViewById(R.layout.layout_extra).setBackgroundColor(Color.RED); setOnClickListener() 使用新视图,因此findViewById(R.layout.layout_extra).setBackgroundColor(Color.RED); didn't work, you should try :没有用,你应该尝试:

public class MainActivity extends AppCompatActivity {

    private Layout layout_extra; // TextView, Button ...

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

        findViewById(R.id.button).setOnClickListener(v->{
            setContentView(R.layout.layout_extra);
        });

        layout_extra = findViewById(R.layout.layout_extra);

        findViewById(R.id.switch1).setOnClickListener(v->{
            layout_extra.setBackgroundColor(Color.RED);
        });
    }
}

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

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