0

This is the full error - java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sercinformationapp/com.example.sercinformationapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

this is the MainActivity where the method is

package com.example.sercinformationapp;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
//imports
import android.net.Uri;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    TextView timetable_monday = (TextView)findViewById(R.id.timetable_monday);
    TextView timetable_tuesday = (TextView)findViewById(R.id.timetable_monday);
    TextView timetable_wednesday = (TextView)findViewById(R.id.timetable_monday);
    TextView timetable_thursday = (TextView)findViewById(R.id.timetable_monday);
    TextView timetable_friday = (TextView)findViewById(R.id.timetable_monday);
    TextView timetable_monday_label = (TextView)findViewById(R.id.timetable_monday_label);
    TextView timetable_tuesday_label = (TextView)findViewById(R.id.timetable_monday_label);
    TextView timetable_wednesday_label = (TextView)findViewById(R.id.timetable_monday_label);
    TextView timetable_thursday_label = (TextView)findViewById(R.id.timetable_monday_label);
    TextView timetable_friday_label = (TextView)findViewById(R.id.timetable_monday_label);



    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //attendance dropdown code
        setContentView(R.layout.fragment_attendance);

        List<String> attendancedropdownArray = new ArrayList<String>();
        attendancedropdownArray.add("COM Mobile App");
        attendancedropdownArray.add("COM Web Development");
        attendancedropdownArray.add("COM Games Development");
        attendancedropdownArray.add("COM Work Based Learning");

        ArrayAdapter<String> attendanceadapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, attendancedropdownArray);
        attendanceadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner attendance_class_dropdown = (Spinner)findViewById(R.id.attendance_class_dropdown);
        attendance_class_dropdown.setAdapter(attendanceadapter);

        attendance_class_dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int position, long id){
                Object item = parent.getItemAtPosition(position);
                if(item != null){
                    switch (position){
                        case 0:
                            TextView class1_label1 = (TextView)findViewById(R.id.attendance_class1_label);
                            class1_label1.setText("Monday");
                            TextView class2_label1 = (TextView)findViewById(R.id.attendance_class2_label);
                            class2_label1.setText("Wednesday");
                            TextView class3_label1 = (TextView)findViewById(R.id.attendance_class3_label);
                            class3_label1.setText("Thursday");
                            break;
                        case 1:
                            TextView class1_label2 = (TextView)findViewById(R.id.attendance_class1_label);
                            class1_label2.setText("Wednesday");
                            TextView class2_label2 = (TextView)findViewById(R.id.attendance_class2_label);
                            class2_label2.setText("Thursday");
                            TextView class3_label2 = (TextView)findViewById(R.id.attendance_class3_label);
                            class3_label2.setText("Friday");
                            break;
                        case 2:
                            TextView class1_label3 = (TextView)findViewById(R.id.attendance_class1_label);
                            class1_label3.setText("Monday");
                            TextView class2_label3 = (TextView)findViewById(R.id.attendance_class2_label);
                            class2_label3.setText("Wednesday");
                            TextView class3_label3 = (TextView)findViewById(R.id.attendance_class3_label);
                            class3_label3.setText("Friday");
                            break;
                        case 3:
                            TextView class1_label4 = (TextView)findViewById(R.id.attendance_class1_label);
                            class1_label4.setText("Monday");
                            TextView class2_label4 = (TextView)findViewById(R.id.attendance_class2_label);
                            class2_label4.setText("Thursday");
                            TextView class3_label4 = (TextView)findViewById(R.id.attendance_class3_label);
                            class3_label4.setText("Friday");
                            break;
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //attendance checkbox code
        CheckBox attendance_checkbox_1 = (CheckBox)findViewById(R.id.attendance_class1_checkbox);
        CheckBox attendance_checkbox_2 = (CheckBox)findViewById(R.id.attendance_class2_checkbox);
        CheckBox attendance_checkbox_3 = (CheckBox)findViewById(R.id.attendance_class3_checkbox);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_timetable, R.id.nav_attendance,
                R.id.nav_external, R.id.nav_event, R.id.nav_share)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    //code for buttons on page External
    public void btnOutlook (View view){
        goToURL("https://outlook.office.com/mail/inbox");
    }
    public void btnMoodle (View view) { goToURL("https://moodle2.serc.ac.uk/my/"); }
    public void btnOneDrive (View view) { goToURL("https://onedrive.live.com/about/en-gb/signin/");}
    public void btnEilp (View view) { goToURL("https://apps.serc.ac.uk/eilp/dashboard/");}

    private void goToURL(String url) {
        Uri uriUrl = Uri.parse(url);
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
        startActivity(launchBrowser);
    }

    public void timetabledropdown(){
        //timetable dropdown code
        setContentView(R.layout.fragment_timetable);

        List<String> timetabledropdownArray = new ArrayList<String>();
        timetabledropdownArray.add("COM Mobile App");
        timetabledropdownArray.add("COM Web Development");
        timetabledropdownArray.add("COM Games Development");
        timetabledropdownArray.add("COM Work Based Learning");

        ArrayAdapter<String> timetableadapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, timetabledropdownArray);
        timetableadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner timetable_subject_dropdown = (Spinner)findViewById(R.id.timetable_subject_dropdown);
        timetable_subject_dropdown.setAdapter(timetableadapter);

        timetable_subject_dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int position, long id){
                Object item = parent.getItemAtPosition(position);
                if(item != null){
                    switch (position){
                        case 0:
                            timetable_monday.setText("9:00 - 11:00");
                            timetable_tuesday.setText("N/A");
                            timetable_wednesday.setText("13:00 - 15:00");
                            timetable_thursday.setText("11:00 - 13:00");
                            timetable_friday.setText("N/A");
                            break;
                        case 1:
                            timetable_monday.setText("N/A");
                            timetable_tuesday.setText("N/A");
                            timetable_wednesday.setText("11:00 - 13:00");
                            timetable_thursday.setText("9:00 - 11:00");
                            timetable_friday.setText("9:00 - 11:00");
                            break;
                        case 2:
                            timetable_monday.setText("12:00 - 14:00");
                            timetable_tuesday.setText("N/A ");
                            timetable_wednesday.setText("9:00 - 11:00");
                            timetable_thursday.setText("N/A");
                            timetable_friday.setText("12:00 - 14:00");
                            break;
                        case 3:
                            timetable_monday.setText("13:00 - 15:00");
                            timetable_tuesday.setText("N/A");
                            timetable_wednesday.setText("N/A");
                            timetable_thursday.setText("14:00 - 16:00");
                            timetable_friday.setText("14:00 - 16:00");
                            break;
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                timetable_monday_label.setText("No Subject Selected");
                timetable_tuesday_label.setText("");
                timetable_wednesday_label.setText("");
                timetable_thursday_label.setText("");
                timetable_friday_label.setText("");

                timetable_monday.setText("");
                timetable_tuesday.setText("");
                timetable_wednesday.setText("");
                timetable_thursday.setText("");
                timetable_friday.setText("");
            }
        });
    }
}

and this is where the method is being called within a fragment

package com.example.sercinformationapp.ui.timetable;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.sercinformationapp.MainActivity;
import com.example.sercinformationapp.R;

public class TimetableFragment extends Fragment {

    private TimetableViewModel timetableViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        timetableViewModel =
                ViewModelProviders.of(this).get(TimetableViewModel.class);
        View root = inflater.inflate(R.layout.fragment_timetable, container, false);

        MainActivity mainActivity = new MainActivity();

        mainActivity.timetabledropdown();

        return root;
    }

    @Override
    public void setUserVisibleHint(boolean userVisibleToUser){
        super.setUserVisibleHint(userVisibleToUser);
        MainActivity mainActivity = new MainActivity();

        mainActivity.timetabledropdown();
    }
}

Thanks for help in advance!

Look_
  • 13
  • 2
  • That stack trace isn't coming from `TimetableFragment`. The current issue is that you cannot call `findViewById()` in a field initializer; i.e., outside of a class method. You'll need to move all of those calls to inside `onCreate()`, after the `setContentView()` call. – Mike M. May 25 '20 at 03:35
  • when i move them after the onCreate i am no longer able to use them within the timetabledropdown method. how do i access them within it? – Look_ May 25 '20 at 03:41
  • You can leave the declarations where they are. You just need to move the `findViewById()` calls into `onCreate()`. For example, leave `TextView timetable_monday;` where it is, and then do `timetable_monday = (TextView)findViewById(R.id.timetable_monday);` in `onCreate()`, after `setContentView()`. Note that there is no `TextView` at the beginning of that line. – Mike M. May 25 '20 at 03:42
  • I will also mention that the code in `TimetableFragment` is problematic, too, and will crash, when it's eventually run. You cannot instantiate `Activity` classes yourself, and have them work correctly. The system must handle `Activity` instantiation. That new instance wouldn't be what you're seeing on-screen, anyway. If `TimetableFragment` is hosted by `MainActivity`, then you just need to call out to the current instance, rather than creating a new one. – Mike M. May 25 '20 at 03:44
  • i still get the callback error when i do it this way, do i need an onCreate within my timetabledropdown method? how do i call out a currant instance? – Look_ May 25 '20 at 03:49
  • You need to move all of those `findViewById()` calls; not just the one I showed. – Mike M. May 25 '20 at 03:51
  • As for calling the current instance, that's a little ahead of where you're at. The Exception you've shown is happening before `MainActivity` even starts. Let's get `MainActivity` on-screen first, and then you can tackle further issues. – Mike M. May 25 '20 at 03:53
  • i have moved them all, i can run every other fragment of the app it is only crashing when i enter the TimetableFragment – Look_ May 25 '20 at 03:55
  • OK, that's the other issue I was describing above. You can't do `new MainActivity()`. If `TimetableFragment` is hosted by `MainActivity`, then you can get the current instance with `getActivity()`. You'll have to cast that to `MainActivity`, though, to access the `thetimetabledropdown()` method; e.g., `MainActivity mainActivity = (MainActivity) getActivity();`. I must say, though, that might not work as you're expecting. – Mike M. May 25 '20 at 04:00
  • your correct, it stops the app from crashing and populates the drop down but doesnt provide any of the other functionality, thanks for your help, ill go and see if i can find a solution to this new problem lol. also removes the nav bar and other styling elements – Look_ May 25 '20 at 04:08
  • Yeah, I'm not sure what the desired design is, but that `setContentView()` call completely replaces everything that was created from the first one. Just FYI. Glad you got it to stop crashing, at least. Cheers! – Mike M. May 25 '20 at 04:11

0 Answers0