【问题标题】:Null pointer exception on setOnClickListener()setOnClickListener() 上的空指针异常
【发布时间】:2019-07-18 17:41:10
【问题描述】:

我想从这个片段访问来自另一个布局的视图,该布局不是片段的一部分。这就是为什么我得到NullPointerException。

我该如何解决这个问题?

片段代码

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;

    }

这是我放置按钮的另一个 xml

<?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>   

我不知道如何从片段访问其他 xml。这个 xml 是单独的文件,因为它与我编写的特定适配器一起进入 recyclerview。并且这个按钮需要在回收器视图中,因为每个按钮都应该从 db 传递每个人的 id。

【问题讨论】:

  • 为什么要从另一个 xml 访问按钮?
  • 我写是因为这个 xml 是 recyclerview 的一部分,我用适配器从 db 中放入表,然后这个 xml 转到位于这个片段视图内的 recyclerview
  • 所以 xml 是 RecyclerView 项目的一部分,对吧?
  • 是的,RecyclerView 在片段的视图上,然后在这个 RecyclerView 里面我有这个 xml
  • 发布您的Adapter 课程。

标签: android android-layout android-fragments nullpointerexception


【解决方案1】:

好的,我明白了你的意思,所以基本上你要做的是从你的回收器视图中的另一个 xml 访问一个按钮,因为它只需引用此代码并在你的 oncreate 视图持有者内的适配器类中进行所有更改指定您将从中访问按钮的 xml 文件:

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);
        }
    }

}

【讨论】:

  • 所以我应该在适配器类中处理按钮,谢谢:)
  • 是的,这是最好的方法
【解决方案2】:

只需更改一行 findviewById。

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

到

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

【讨论】:

  • 我试过了,但如果这个 xml 是片段的一部分,那将是合理的,但这不是片段的一部分。这是单独的 xml
  • 我有 xml,它是片段的一部分,我可以通过调用视图访问该 xml 的一部分。
  • 其实我没有得到你
  • 这个片段的视图只有一个元素recyclerview。我创建了另一个 xml,它进入了这个回收器视图,它有视图元素和按钮
猜你喜欢
  • 2015-03-27
  • 2012-03-09
  • 1970-01-01
  • 2015-10-29
  • 2021-01-19
  • 2013-08-12
  • 2019-12-09
  • 1970-01-01
相关资源
最近更新 更多