这是我的代码,你可以在我向onclick listener声明了id的类中看到,每个代码似乎都没有问题,只有0个错误,但当我运行应用程序时,它崩溃并说“尝试调用虚拟方法'void android.view.view.setonclicklistener(android.view.view$onclick)”,在使用它之前,我是否需要写下任何东西才能在mainactivity类中使用fragment元素??任何捐款都是有益的。
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));
}
});
}
}
1条答案
按热度按时间baubqpgj1#
这就是你想打电话的地方
setOnClickListener
在空对象上。我猜你的碎片还没有膨胀。我将在片段中设置onclicklistener,并从片段中调用活动中的方法: