简体   繁体   English

“方法从未使用过”,尽管它正在使用 - Android studio

[英]'Method is never used' although it is being used- Android studio

As it is said, it gives this message although it is being used.据说,虽然它正在被使用,但它给出了这个消息。 It is somehow not being identified.它不知何故未被识别。 Here is the java class这是java class

package com.example.exploringlayouts;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_menu);


    }
    void loadConstraintLayout(View v){
        setContentView(R.layout.activity_main);
    }

    void loadTableLayout(View v){
       // setContentView(R.layout.my_table_layout);
    }
    void loadMenuLayout(View v){
        setContentView(R.layout.main_menu);
    }
}

and here part of the OnClick segment that involves that method said that is not used.此处涉及该方法的部分OnClick部分表示未使用。

<Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:onClick="loadConstraintLayout"       #<-----Here it is.
            android:text="LOAD"
            android:textSize="15sp" />

What can possibly be the problem?可能是什么问题?

只需将 public 修饰符添加到您的方法中。

Your methods will need to be set to public void.您的方法需要设置为 public void。 This is important in OOP languages as your function cannot be called outside of the class now.这在 OOP 语言中很重要,因为现在不能在类之外调用您的函数。

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

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