所以我一直在做这个项目,当你点击按钮,它显示日期,按钮变得不可见,显示数据的TextView目前在按钮后面是不可见的。该按钮恰好位于选项卡中。问题是当我运行应用程序时它给了我这个错误。它不是重复的,因为我认为我在启动onClickListener之前初始化了Button,所以我没有得到为什么它是null.Maybe因为它来自一个标签式活动=
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pedrogouveia.averagemaker/com.pedrogouveia.averagemaker.Testes}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
这里是代码: Java:
public class Testes extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private DatePickerDialog.OnDateSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_picker;
private TextView date_text;
private TextView hora_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
date_button=(Button) findViewById(R.id.date_button);
date_button.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog date_dialog = new DatePickerDialog(
Testes.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
hourSetListener,
year, month, day);
date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
date_dialog.show();
};
}
);
dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month=month+1;
date_button.setVisibility(View.GONE);
date_text = (TextView) findViewById(R.id.date_text);
date_text.setVisibility(View.VISIBLE);
String dt = day+"-"+month+"-"+year;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try {
sdf.parse(dt);
date_text.setText(dt);
} catch (ParseException e) {
e.printStackTrace();
}
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_testes, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case go_to_fazer_media:{
if (item.isChecked())
item.setChecked(false);
else{
Intent i = new Intent(Testes.this,Formas.class);
startActivity(i);
return true;
}
}
case definicoes:{
if (item.isChecked())
item.setChecked(false);
else{
return true;
}
}
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
Tab1tests tab1 = new Tab1tests();
return tab1;
case 1:
Tab2calendar tab2 = new Tab2calendar();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Adicionar Testes";
case 1:
return "Mapa de Testes";
}
return null;
}
}}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/disciplina"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/section_label"
android:layout_margin="20dp"
android:layout_toEndOf="@+id/section_label"
android:text="Disciplina:"
android:textSize="35sp" />
<TextView
android:id="@+id/sala"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/disciplina"
android:layout_margin="20dp"
android:layout_toEndOf="@+id/section_label"
android:text="Sala:"
android:textSize="35sp"/>
<TextView
android:id="@+id/Dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/sala"
android:layout_margin="20dp"
android:layout_toEndOf="@+id/section_label"
android:text="Dia:"
android:textSize="35sp" />
<TextView
android:id="@+id/Hora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Dia"
android:layout_margin="20dp"
android:layout_toEndOf="@+id/section_label"
android:text="Hora:"
android:textSize="35sp" />
<Button
android:id="@+id/add_test"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="95dp"
android:text="Adicionar o teste"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/date_text"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/hora_picker"
android:layout_alignTop="@+id/Dia"
android:text="ESCOLHER DATA"
android:textSize="25sp"
android:textStyle="bold"
android:visibility="invisible"/>
<Button
android:id="@+id/date_button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/hora_picker"
android:layout_alignTop="@+id/Dia"
android:text="ESCOLHER DATA"
android:textSize="25sp"
android:textStyle="bold"
android:onClick="setDate"/>
<TextView
android:id="@+id/hora_text"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="ESCOLHER HORA"
android:textStyle="bold"
android:textSize="25sp"
android:layout_alignBottom="@+id/Hora"
android:layout_toEndOf="@+id/Hora"
android:visibility="invisible"/>
<Button
android:id="@+id/hora_picker"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="ESCOLHER HORA"
android:textStyle="bold"
android:textSize="25sp"
android:layout_alignBaseline="@+id/Hora"
android:layout_alignBottom="@+id/Hora"
android:layout_toEndOf="@+id/sala" />
<EditText
android:id="@+id/sala_text"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/sala"
android:layout_toEndOf="@+id/Hora"
android:ems="10"
android:hint="Nª/Nome da sala"
android:textSize="25sp"
android:inputType="textPersonName" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/disciplina"
android:layout_toEndOf="@+id/disciplina"
android:scrollbarSize="25sp"
android:layout_alignTop="@+id/disciplina" />
这是布局的图像: Layout image
答案 0 :(得分:0)
您在点击监听器更改时遇到问题,您需要使用View.OnClickListener()
这
date_button.setOnClickListener(
new Button.OnClickListener(){
到
date_button.setOnClickListener(
new View.OnClickListener(){