gpt4 book ai didi

java - 从一个 fragment 移动到另一个 fragment

转载 作者:行者123 更新时间:2023-12-02 00:11:16 24 4
gpt4 key购买 nike

当我从一个 fragment 切换到另一个 fragment 时,应用程序崩溃。我不知道如何解决它。我不知道该怎么做,因为我是 android 新手。我正在等待您的帮助

调试控制台:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.a5starcompany.mybank1/com.a5starcompany.mybank1.MainActivity}: java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:29)
at com.bumptech.glide.Glide.getRetriever(Glide.java:709)
at com.bumptech.glide.Glide.with(Glide.java:741)
at com.a5starcompany.mybank1.Adapter.LoanAdapter.<init>(LoanAdapter.java:28)
at com.a5starcompany.mybank1.MainActivity.onCreate(MainActivity.java:90)
at android.app.Activity.performCreate(Activity.java:6691)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:203) 
at android.app.ActivityThread.main(ActivityThread.java:6251) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 
I/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod(`enter code here`) returned Gms: com.google.firebase.auth.api.internal.zzao@638cec8
I/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzao@638cec8
I/art: Enter while loop.

java类是这样的

        if (mActivity == null) {
return;
}
final LoanModel loanModel = (LoanModel) this.list.get(i);
myClass.loanName.setText(loanModel.getLoanName());
myClass.loanPercentage.setText(loanModel.getPercentage());
myClass.loanStarting.setText(String.valueOf(loanModel.getLoanStaring()));
myClass.emiStarting.setText(String.valueOf(loanModel.getEmiStaring()));
myClass.loanForm.setText(loanModel.getLoanForm());
this.glide.load(loanModel.getImage()).into(myClass.loanImage);
}
``

最佳答案

谢谢 zovakk 的回复,但我不太明白你在说什么,但这是我该 Activity 的完整代码

    public Activity mActivity;

Context context;
RequestManager glide = Glide.with(this.context);
public ArrayList<LoanModel> list;

public class MyClass extends ViewHolder {
LinearLayout cardview;
TextView emiStarting;
TextView loanForm;
ImageView loanImage;
TextView loanName;
TextView loanPercentage;
TextView loanStarting;


public MyClass(View view) {
super(view);
this.cardview = (LinearLayout) view.findViewById(R.id.cardview);
this.loanImage = (ImageView) view.findViewById(R.id.imge_loan);
this.loanName = (TextView) view.findViewById(R.id.loan_name);
this.loanPercentage = (TextView) view.findViewById(R.id.loan_perc);
this.loanStarting = (TextView) view.findViewById(R.id.loan_loanStarting);
this.emiStarting = (TextView) view.findViewById(R.id.emi_Staring);
this.loanForm = (TextView) view.findViewById(R.id.loan_form_name);

}
}


public LoanAdapter(MainActivity mainActivity, ArrayList<LoanModel> arrayList) {
this.context = mainActivity;
this.list = arrayList;

}

public MyClass onCreateViewHolder(ViewGroup viewGroup, int i) {
return new MyClass(LayoutInflater.from(this.context).inflate(R.layout.loan_layout, null));
}

public void onBindViewHolder(MyClass myClass, int i) {
if (mActivity == null) {
return;
}
final LoanModel loanModel = (LoanModel) this.list.get(i);
myClass.loanName.setText(loanModel.getLoanName());
myClass.loanPercentage.setText(loanModel.getPercentage());
myClass.loanStarting.setText(String.valueOf(loanModel.getLoanStaring()));
myClass.emiStarting.setText(String.valueOf(loanModel.getEmiStaring()));
myClass.loanForm.setText(loanModel.getLoanForm());
this.glide.load(loanModel.getImage()).into(myClass.loanImage);
myClass.cardview.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(LoanAdapter.this.context, HomeLoanActiviy.class);
intent.putExtra("LoanName", loanModel.getLoanName());
intent.putExtra("ImageName", loanModel.getImage());
intent.putExtra("Percentage", loanModel.getPercentage());
intent.putExtra("LoanStarting", loanModel.getLoanStaring());
intent.putExtra("EMIStarting", loanModel.getEmiStaring());
intent.putExtra("LoanForm", loanModel.getLoanForm());
LoanAdapter.this.context.startActivity(intent);
}
});
}

关于java - 从一个 fragment 移动到另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58119476/

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