0

so I have been doing this project, that when you click the button , it displays the date , and the button goes invisible, the TextView that will display the data is currently invisible behind the button. The button happens to be in a tab .Well the problem is when I run the app it gives me this error.It isn't a duplicate because I think that I have initialized the Button before starting the onClickListener, so I don't get why it is null.Maybe because its from a tabbed activity=

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

well here is the code: 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" />

Here is a image of the layout: Layout image

  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Andrew Sun Jul 07 '17 at 18:36
  • In this case it isn't because I referred it and it isn't null but idk why it says it is – Emanuel Sobreiro Jul 07 '17 at 18:42

1 Answers1

0

you have a problem at you click listener change , you need to use View.OnClickListener()

from

date_button.setOnClickListener(
            new Button.OnClickListener(){

to

 date_button.setOnClickListener(
                new View.OnClickListener(){
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44