android 如何在其他类中使用Activity函数

wtlkbnrh  于 2023-02-14  发布在  Android
关注(0)|答案(1)|浏览(148)

我正在尝试使用findViewById来定义类中的视图和布局。按照网上的建议,我创建了一个活动上下文,但它仍然返回空对象。我仍然是一个编码初学者,所以请耐心等待
这里我的类:

public class team{
    //defining activity class to use
    Activity activity;
    Context context;

    //definging text box
    LinearLayout lay = activity.findViewById(R.id.Scontainer);
    View Tbox = activity.getLayoutInflater().inflate(R.layout.text_box_output, null);

    //mebers Deffing
    String teamname;
    String teacher;
    ArrayList<Players> members = new ArrayList<Players>();

    public team(Context context, Activity activity, String teamname,String teacher) {
        //to be able to use activity
        this.context=context;
        this.activity=activity;

        //normal things to define
        this.teamname = teamname;
        this.teacher= teacher;
    }
//mainactivity

public class MainActivity extends AppCompatActivity {

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

        Activity act= this;

        //creating the list that is on screen
        FloatingActionButton addClass = findViewById(R.id.Add_class_button);


        // input
        addClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                team sportsteam = new team(MainActivity.this,act,"","");
                sportsteam.addtext();
            }
        });
    }

}

'

l7wslrjt

l7wslrjt1#

It looks like the issue is that the activity variable in your team class is not initialized, and thus activity.findViewById and activity.getLayoutInflater() are returning null.

To fix this issue, you can pass the Activity instance to the team class in the constructor, and then use that instance to call findViewById and getLayoutInflater.

Here's an updated version of your code:

// My class
public class team {
    // Defining Activity class to use
    private Activity activity;

    // Defining text box
    LinearLayout lay;
    View Tbox;

    // Members Defining
    String teamname;
    String teacher;
    ArrayList<Players> members = new ArrayList<Players>();

    public team(Activity activity, String teamname, String teacher) {
        // To be able to use activity
        this.activity = activity;

        // Normal things to define
        this.teamname = teamname;
        this.teacher = teacher;
        
        // initialize the lay and Tbox variables
        lay = activity.findViewById(R.id.Scontainer);
        Tbox = activity.getLayoutInflater().inflate(R.layout.text_box_output, null);
    }

    public void addtext() {
        // add text to the layout and view
    }
}

// MainActivity
public class MainActivity extends AppCompatActivity {

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

        // Creating the list that is on screen
        FloatingActionButton addClass = findViewById(R.id.Add_class_button);

        // Input
        addClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                team sportsteam = new team(MainActivity.this, "", "");
                sportsteam.addtext();
            }
        });
    }
}

相关问题