0

I am new to android and I am in a learning phase.I searched a lot but couldn't find an appropriate solution for this. I am getting a null pointer exception at notes.setOnClickListener though I have added findViewById statement

       package com.example.hp.newcalendar;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class PreviouslyCalledFragment extends Fragment {

    ListView students;
    TextView test;
    ImageView notes;
    SimpleDateFormat formatter = new SimpleDateFormat("dd M yyyy hh:mm a");
    String dateInString="23 Dec 2015 18:00 pm";
    View view;
    Context mContext;
    ArrayList<FieldDetails> searchResults;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.activity_previously_called_fragment, container, false);
        students = (ListView) view.findViewById(R.id.alreadyCalled);
        notes=(ImageView)view.findViewById(R.id.notesView);
        searchResults = GetSearchResults();


        students.setAdapter(new CustomAdapter_Notes(mContext, searchResults));
        notes.setOnClickListener(new View.OnClickListener() {       //null pointer exception
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"hello", Toast.LENGTH_LONG).show();
            }
        });

        return view;
    }

    @Override
    public void onAttach(final Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }
    private ArrayList<FieldDetails> GetSearchResults() {
        ArrayList<FieldDetails> results = new ArrayList<FieldDetails>();

        FieldDetails fieldDetails = new FieldDetails();
        fieldDetails.setName("Aman Jain");
        fieldDetails.setTime("Dec 23,18:00 pm");
        results.add(fieldDetails);

        fieldDetails = new FieldDetails();
        fieldDetails.setName("Keshav Sharma");
        fieldDetails.setTime("Dec 23,19:00 pm");
        results.add(fieldDetails);
        return results;
    }


}

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"
tools:context=".MainActivity"
android:padding="5dp"
android:background="#ffffff">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/alreadyCalled"
    android:layout_centerHorizontal="true"
    android:clickable="false"
    android:layout_alignParentTop="true" />

Custom_layout xml :

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"

>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:textSize="15dp"
    android:id="@+id/nameOfInterviewee"
    android:textColor="#008080"
    android:layout_marginTop="5dp"
    android:paddingBottom="5dp"
    android:layout_marginLeft="2dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:textSize="12dp"
    android:id="@+id/dateAndTime"
    android:textColor="#008080"
    android:paddingBottom="10dp"
    android:layout_below="@+id/nameOfInterviewee"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="-5dp"
    />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#848482"
    android:id="@+id/view"
    android:layout_below="@+id/dateAndTime"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageView
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:id="@+id/notesView"
    android:clickable="true"
    android:layout_marginTop="16dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:src="@drawable/notes"/>

 </RelativeLayout>

PLEASE HELP !!

swapnil
  • 13
  • 8
  • There is no method call `notes.setOnClickListener()` in your code sample. Have you included everything? Also please share layout `R.layout.activity_previously_called_fragment`. – Tautvydas Dec 27 '15 at 11:32
  • There is no R.id.notesView in your layout file. You probably wanted to add the click listener for listview item. Just use students.setOnItemClickListener() or add the onClickListener in the adapter for the respective item – aelimill Dec 27 '15 at 11:40
  • @aelimill I do have R.id.notesView in my custom_layout xml – swapnil Dec 27 '15 at 11:42
  • You inflate fragment with R.layout.activity_previously_called_fragment and there is no R.id.notesView in it. I suppose you intented to display listview items with Custom_layout xml – aelimill Dec 27 '15 at 11:45

1 Answers1

0

This is most likely because R.id.notesView id is not present in your xml file R.layout.activity_previously_called_fragment. Please check the xml file or share it here.

Tautvydas
  • 2,027
  • 3
  • 25
  • 38