[英]I Want to use fragment UI element id in mainactivity java class to accomplish on click listener event on mainactivity class instead of fragment class
here is my code You can see down in the class that i have declared the id to onclick listener , and every seems okay with 0 error, but when i run the app it crashes and says "Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClick" is there anything that i have to write it down to be able to use fragment element in my mainactivity class before using it ?? any contribution would be helpfull.这是我的代码您可以在类中看到我已将 id 声明为 onclick 侦听器,并且每个似乎都没有问题,错误为 0,但是当我运行该应用程序时,它崩溃并显示“尝试调用虚拟方法 'void android.view .View.setOnClickListener(android.view.View$OnClick" 有什么我必须写下来才能在使用它之前在我的主要活动类中使用片段元素的吗??任何贡献都会有所帮助。
package com.login.mobi.loginapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import com.google.android.material.tabs.TabLayout;
import java.util.Calendar;
public class MainActivates extends AppCompatActivity {
EditText et1;
TabLayout tabLayout;
ViewPager2 pager2;
FragmentAdapter adapter;
Calendar calendar=Calendar.getInstance();
final int year=calendar.get(Calendar.YEAR);
final int month=calendar.get(Calendar.MONTH);
final int day=calendar.get(Calendar.DAY_OF_MONTH);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mains);
EditText txt10=findViewById(R.id.txtnum10);
tabLayout = findViewById(R.id.tab_layout);
pager2 = findViewById(R.id.view_pager2);
FragmentManager fm = getSupportFragmentManager();
adapter = new FragmentAdapter(fm, getLifecycle());
pager2.setAdapter(adapter);
tabLayout.addTab(tabLayout.newTab().setText("Primary info"));
tabLayout.addTab(tabLayout.newTab().setText("Next"));
tabLayout.addTab(tabLayout.newTab().setText("Finish-up"));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager2.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
et1=(EditText) findViewById(R.id.et1);
et1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog datePickerDialog=new DatePickerDialog(
MainActivates.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
month =month+1;
String date=day+"/"+month+"/"+year;
et1.setText(date);
}
},year,month,day);
datePickerDialog.show();
}
});
pager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
}
}
````
It seams that you're trying to call setOnClickListener
on a null object.看来您正在尝试在空对象上调用
setOnClickListener
。 I guess that your fragment is not yet inflated.我猜你的片段还没有膨胀。
I would set OnClickListener in the fragment and call a method in the activity from the fragment :我会在片段中设置 OnClickListener 并从片段中调用活动中的方法:
if (requireActivity() instanceof MainActivity) {
((MainActivity) requireActivity()).magicMethod(); // implement magicMethod in your Activity
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.