gpt4 book ai didi

java - setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上

转载 作者:行者123 更新时间:2023-12-02 01:48:05 26 4
gpt4 key购买 nike

我遇到了提交模式提交按钮的点击监听器问题。

这是错误。

FATAL EXCEPTION: main
Process: com.kuryetaxi.omerkaptan.kuryetaxi, PID: 6564
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.button.MaterialButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

我对空指针异常是什么有一个合理的理解,并且我已经彻底搜索了与我类似的问题。我尝试以多种方式重新格式化点击监听器,确保我有正确的 View ID 等。

fragment 代码

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

}
});
}

layout.xml代码

xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/siparisdetaylayout">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
card_view:cardBackgroundColor="#f8b518"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="1dp">

<LinearLayout
android:layout_width="match_parent"
android:clickable="true"
android:id="@+id/siparisdetaylayout"
android:layout_margin="6dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical">




<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_margin="15dp"
android:id="@+id/logo"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
<TextView
android:layout_gravity="left"
android:layout_width="125dp"
android:id="@+id/firmatext"

android:textStyle="bold"
android:textSize="17dp"
android:gravity="left"
android:textColor="#FF000000"
android:layout_height="wrap_content" />



<TextView
android:layout_gravity="left"
android:id="@+id/tarihtext"
android:layout_width="125dp"

android:textStyle="bold"
android:textSize="17dp"
android:gravity="left"
android:textColor="#FF000000"
android:layout_height="wrap_content" />
<TextView
android:layout_gravity="left"
android:id="@+id/saattext"
android:layout_width="125dp"

android:textStyle="bold"
android:textSize="17dp"
android:gravity="left"
android:textColor="#FF000000"
android:layout_height="wrap_content" />

<android.support.design.button.MaterialButton

android:id="@+id/ekleButton"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:text="Ekle"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_height="wrap_content" />

</LinearLayout>

</android.support.v7.widget.CardView>

最佳答案

重写类中的 onViewCreated() 方法,并将此方法 tanimla() gerek() getSip("0") 放入其中,并尝试在其中调用 getView()你的方法就像这样:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_kurye_siparis, container, false);
return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
//
tanimla();
gerek();
getSip("0");
}

public void gerek()
{
MaterialButton ekle = (MaterialButton)getView().findViewById(R.id.ekleButton);
...
}

阅读此内容doc !

关于java - setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57444554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com