我正在尝试使用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();
}
});
}
}
'
1条答案
按热度按时间l7wslrjt1#