空对象引用上的java setOnClickListener(安卓.view.view$OnClickListener)
我对提交模式提交按钮的点击侦听器有问题
这就是错误所在
FATAL EXCEPTION: main
Process: com.kuryetaxi.omerkaptan.kuryetaxi, PID: 6564
java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.support.design.button.MaterialButton.setOnClickListener(安卓.view.View$OnClickListener)' on a null object reference
我对空指针异常有一个合理的理解,并且我已经彻底搜索了一个类似于我的问题。我试着用几种方法重新设置点击监听器的格式,确保我有正确的视图ID等等
片段代码
View view;
private RecyclerView siparislerrecyclerview;
private SiparisAdapter siparisAdapter;
private List<SiparisModel> siparisList;
private ChangeFragments changeFragments;
private String siparis_id,kurye_id;
private GetSharedPreferences getSharedPreferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_kurye_siparis, container, false);
tanimla();
gerek();
getSip("0");
return view;
}
public void tanimla()
{
siparisList = new ArrayList<>();
siparislerrecyclerview = view.findViewById(R.id.siparislerrecyclerview);
RecyclerView.LayoutManager mng = new GridLayoutManager(getContext(),1);
siparislerrecyclerview.setLayoutManager(mng);
changeFragments = new ChangeFragments(getContext());
getSharedPreferences = new GetSharedPreferences(getActivity());
siparis_id=getSharedPreferences.getSession().getString("id",null);
}
public void getSip(String kur_durum)
{
Call<List<SiparisModel>> req = getInstance().getSip(kur_durum);
req.enqueue(new Callback<List<SiparisModel>>() {
@Override
public void onResponse(Call<List<SiparisModel>> call, Response<List<SiparisModel>> response) {
Log.i("listem",response.body().toString());
if (response.body().get(0).isTf())
{
siparisList = response.body();
siparisAdapter = new SiparisAdapter(siparisList,getContext());
siparislerrecyclerview.setAdapter(siparisAdapter);
}
else
{
Toast.makeText(getContext(),"FİRMA SİPARİSİ BULUNMAMAKTADIR",Toast.LENGTH_LONG).show();
changeFragments.change(new HomeFragment());
}
}
@Override
public void onFailure(Call<List<SiparisModel>> call, Throwable t) {
Toast.makeText(getContext(), Warnings.internetProblemText,Toast.LENGTH_LONG).show();
}
});
}
public void gerek()
{
MaterialButton ekle = (MaterialButton)view.findViewById(R.id.ekleButton);
ekle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
siparisAl(siparis_id);
}
});
}
public void siparisAl(String siparis_id)
{
Call<SiparisAlModel> req = ManagerAll.getInstance().siparisAl(siparis_id);
req.enqueue(new Callback<SiparisAlModel>() {
@Override
public void onResponse(Call<SiparisAlModel> call, Response<SiparisAlModel> response) {
Log.i("hatas",response.body().toString());
Toast.makeText(getContext(),response.body().getText(),Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<SiparisAlModel> call, Throwable t) {
Toast.makeText(getContext(),Warnings.internetProblemText,Toast.LENGTH_LONG).show();
}
});
}
布局。xml代码
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/siparisdetaylayout">
<安卓.support.v7.widget.CardView xmlns:card_view="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
card_view:cardBackgroundColor="#f8b518"
安卓:layout_height="wrap_content"
安卓:layout_margin="5dp"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="1dp">
<LinearLayout
安卓:layout_width="match_parent"
安卓:clickable="true"
安卓:id="@+id/siparisdetaylayout"
安卓:layout_margin="6dp"
安卓:layout_height="wrap_content"
安卓:gravity="center"
安卓:layout_gravity="center"
安卓:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="55dp"
安卓:layout_height="55dp"
安卓:layout_margin="15dp"
安卓:id="@+id/logo"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
<TextView
安卓:layout_gravity="left"
安卓:layout_width="125dp"
安卓:id="@+id/firmatext"
安卓:textStyle="bold"
安卓:textSize="17dp"
安卓:gravity="left"
安卓:textColor="#FF000000"
安卓:layout_height="wrap_content" />
<TextView
安卓:layout_gravity="left"
安卓:id="@+id/tarihtext"
安卓:layout_width="125dp"
安卓:textStyle="bold"
安卓:textSize="17dp"
安卓:gravity="left"
安卓:textColor="#FF000000"
安卓:layout_height="wrap_content" />
<TextView
安卓:layout_gravity="left"
安卓:id="@+id/saattext"
安卓:layout_width="125dp"
安卓:textStyle="bold"
安卓:textSize="17dp"
安卓:gravity="left"
安卓:textColor="#FF000000"
安卓:layout_height="wrap_content" />
<安卓.support.design.button.MaterialButton
安卓:id="@+id/ekleButton"
安卓:layout_width="wrap_content"
安卓:layout_gravity="center"
安卓:layout_marginTop="15dp"
安卓:text="Ekle"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
安卓:layout_height="wrap_content" />
</LinearLayout>
</安卓.support.v7.widget.CardView>
# 1 楼答案
尝试在
onViewCreated()
中调用gerek()
,以确保在尝试使用它之前整个布局都已充气