-1

So,I'm doing 2 fragments in a activity and 1 fragment uses a listview+array adapter. but when i try to create a instance of the adapter it is null. i saw olther people with the same problem and what worked for them didn't work for me. ALSO THIS IS THE FIRST APP THAT I MADE ON MY OWN SO DON'T GET MAD IF I AM DUMB

MyFragment.onCreateView

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView =inflater.inflate(R.layout.fragment_todo, container, false);
    ListView listView = rootView.findViewById(R.id.list_view);


    ArrayList<Task> arrayList = new ArrayList<>();
    arrayList.add(new Task("this is a placeholder todo",Task.PRIORITY_LOW, new Date()));
    arrayList.add(new Task("this is a placeholder todo",Task.PRIORITY_MEDIUM, new Date()));
    arrayList.add(new Task("this is a placeholder todo",Task.PRIORITY_HIGH, new Date()));


    listView.setAdapter(new HomeActivity.TaskAdapter(getActivity(), arrayList,rootView));

    ViewModel viewModel = ViewModelProviders.of(this).get(ViewModel.class);
    viewModel.getTasks().observe(this, new Observer<List<Task>>() {
        @Override
        public void onChanged(@Nullable List<Task> taskEntries) {
            //TODO SET THE ADAPTER
        }
    });

    return rootView;
}

MyMainActivity wich contains the adapter aswell :

public class HomeActivity extends FragmentActivity {


static int colorGreen ;
static int colorOrange;
static int colorRed;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    colorGreen=HomeActivity.this.getResources().getColor(R.color.colorGreen);
    colorOrange=HomeActivity.this.getResources().getColor(R.color.colorOrange);
    colorRed=HomeActivity.this.getResources().getColor(R.color.colorPrimaryDark);


    //FAB
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    initializeAds();
    DataBase Db = DataBase.getInstance(getApplicationContext());

   ViewPager vp = findViewById(R.id.view_pager);
    CustomFragmentPageAdapter myAdapter = new CustomFragmentPageAdapter(getSupportFragmentManager(),this);
   vp.setAdapter(myAdapter);

  TabLayout mTabLayout = findViewById(R.id.tabLayout_Home);
   mTabLayout.setupWithViewPager(vp);


}



@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_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public static class TaskAdapter extends ArrayAdapter<Task> {

    private View rootView;

    public TaskAdapter(Activity context, ArrayList<Task> tasks ,View view) {
        super(context, 0, tasks );
        rootView=view;


    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view

        if (convertView == null) {
            convertView = rootView;

        }
        Task currentTask = getItem(position);

        TextView description = convertView.findViewById(R.id.description_todo);
        if (currentTask != null) {
            description.setText(currentTask.getDescription());
        }

        TextView date = convertView.findViewById(R.id.date_todo);
        if (currentTask != null) {
            date.setText(currentTask.getUpdatedAt().toString());
        }

        ConstraintLayout constraintLayout = convertView.findViewById(R.id.constraint_layout_todo);
        if (currentTask != null) {
            switch (currentTask.getPriority()){
                case Task.PRIORITY_LOW:constraintLayout.setBackgroundColor(colorGreen);
                case Task.PRIORITY_MEDIUM:constraintLayout.setBackgroundColor(colorOrange);
                case Task.PRIORITY_HIGH:constraintLayout.setBackgroundColor(colorRed);
            }
        }

        return convertView;
    }
}
Alex Totolici
  • 318
  • 2
  • 14
  • 1
    You might like https://stackoverflow.com/q/218384/3166697 – Dima Kozhevin Jul 01 '18 at 17:24
  • The linked NPE question is useful, but the militancy with which any question relating to a `NullPointerException` is flagged as duplicate is frustrating, particularly for new users. In this case, the OP does not appear to simply be using an uninitialized variable, it looks likely to be an XML id error (as posted in the answer below), which is not at all addressed by the question about "What is a NullPointerException". – Tyler V Jul 01 '18 at 18:32

1 Answers1

2

To me, it looks like your code is fine. But, the error might be occurring because there could be a problem in your fragment_todo.

I've ran into this same kind of error a lot of times before. So, check that out.

Understand that the error is telling you that the ListView is null. Most of the times, that means there possibly have been a typo or something similar in your code.

Make sure the id in your XML matches with the one in your Fragment class.

Hope this helped.