1

I want to access from this fragment a view from another layout which is not part of the fragment. That is why i get NullPointerException.

How can i solve this?

Fragment code

public class Pacijenti_fragment extends Fragment  {
    //this is the JSON Data URL


    public final static String MESSAGE_KEY= "com.example.android.app";
    //a list to store all the products
    List<Pacijent> pacijentList;
    String id="";
    String ID="";
    //the recyclerview
    RecyclerView recyclerView;
    private SearchView searchView;
    private PacijentiAdapter adapter;
    Button btnPogled;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view=inflater.inflate(R.layout.pacijenti_fragment, container, false);

        //getting the recyclerview from xml
        recyclerView = (RecyclerView)view.findViewById(R.id.recylcerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new MyDividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL, 36));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        //initializing the productlist
        pacijentList = new ArrayList<>();
        btnPogled=(Button)findViewById(R.id.btnDodaj); //here i make my button
        Bundle b=getArguments();
        this.id=b.getString("id");
        System.out.println(id);
        ID = this.id;
        btnPogled.setOnClickListener(new View.OnClickListener(){ //here i get null pointer
            @Override
            public void onClick(View v) {
                OnPogled();
            }
        });

        //this method will fetch and parse json
        //to display it in recyclerview
        loadPacijenti();



        return view;

    }

This is another xml where I located my button

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


    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="8dp">
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/GornjiLayout"
        >
            <TextView
                android:id="@+id/textViewName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Jane"
             android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/textViewSurname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_marginLeft="5dp"

                android:layout_toRightOf="@id/textViewName"
                android:text="Doe"
             android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                android:textColor="#000000"/>
        </LinearLayout>

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/SredjiLayout"
        android:layout_below="@id/GornjiLayout"
        >
            <TextView
                android:id="@+id/textViewDateOfBirth"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textViewName"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="150dp"
                android:layout_marginTop="5dp"



                android:text="21.05.1989"
             android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                android:textStyle="bold" />

            <Button
                android:id="@+id/buttonDodaj"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="Otvori" />


        </LinearLayout>
        <TextView
          android:id="@+id/textViewAdress"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/SredjiLayout"
          android:layout_marginLeft="5dp"
          android:layout_marginTop="5dp"

          android:text="Some street 45, UnknownVille"
         android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
          android:textStyle="bold" />
    </RelativeLayout>

</LinearLayout>   

I don't know how to access other xml from fragment. And this xml is separate file because it goes inside recyclerview with specific adapter that i wrote. and this button needs to be inside that recycler view because every button should pass id of every person from db.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
Petar
  • 15
  • 1
  • 1
  • 7

2 Answers2

0

Just change one line of findviewById.

btnPogled=(Button)findViewById(R.id.btnDodaj)

to

btnPogled=(Button)view.findViewById(R.id.btnDodaj)
Pratik Satani
  • 1,115
  • 1
  • 9
  • 25
  • i tried that but that would be plausible if this xml was part of the fragment, but this is not part of the fragment.This is separate xml – Petar Feb 25 '19 at 10:35
  • i have xml which is part of the fragment and i can acceess part of that xml with call of view. – Petar Feb 25 '19 at 10:36
  • Actually, I am not getting you – Pratik Satani Feb 25 '19 at 10:40
  • view of this fragment has only one element recyclerview. and i created another xml which goes inside this recycler view and it has view elements and button i populate this view elements with db infromations and i want with click on a button to access another fragment which contains more detailed informations about that person – Petar Feb 25 '19 at 10:42
0

Okay i got your point so basically what you're trying to do is access a button from another xml in your recycler view for that simply refer this code and do all changes in your adapter class here inside your oncreate view holder specify the xml file from which you'll be accessing the button:

public class ManagerTaskListAdapter extends RecyclerView.Adapter<ManagerTaskListAdapter.ProjectViewHolder> {

    private ArrayList<Taskmsg> dataList;
    private ArrayList<Project> projects;

    Context mContext;


    public ManagerTaskListAdapter(ArrayList<Taskmsg> dataList, Context mContext) {
        this.dataList = dataList;
        this.mContext=mContext;
           }

    @Override
    public ProjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.man_task_list_item, parent, false);
        return new ProjectViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ProjectViewHolder holder, int position) {
        String fname=dataList.get(position).getEmpFirstName();
        String lname=dataList.get(position).getEmpLastName();
        String prdesc=dataList.get(position).getTaskDescription();
        String pid= dataList.get(position).getProjectId().toString();
        String em=dataList.get(position).getTaskCreatedBy();
        String taskid=dataList.get(position).getTaskId().toString();
        prdesc = prdesc.replaceAll("\\<.*?\\>", "");
        holder.txttname.setText(dataList.get(position).getTaskName());
        holder.txttdesc.setText(prdesc);
        holder.txttaskcrt.setText(dataList.get(position).getTaskCreatedBy());
        holder.txtempname.setText(fname +" "+ lname);
        holder.itemView.setOnClickListener(v -> {
            Intent in=new Intent(mContext,TaskDetails.class);
            in.putExtra("tid",taskid);
            in.putExtra("pid",pid);
            in.putExtra("email",em);
            mContext.startActivity(in);



        });

    }



    @Override
    public int getItemCount() {

        return dataList.size();

    }

    class ProjectViewHolder extends RecyclerView.ViewHolder {

        TextView txttname,txttdesc,txtempname,txttaskcrt;
        ProjectViewHolder(View itemView) {
            super(itemView);
            txttname = (TextView) itemView.findViewById(R.id.tv_taskname);
            txttaskcrt=itemView.findViewById(R.id.tv_taskcreatedby);
            txttdesc = (TextView) itemView.findViewById(R.id.tv_taskdescription);
            txtempname = (TextView) itemView.findViewById(R.id.tv_empname);
        }
    }

}
Mr. Patel
  • 1,379
  • 8
  • 21