我在一个由活动IssueActivity
控制的视图寻呼机中有两个片段@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_issue);
viewPager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tab);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
layout = findViewById(R.id.layout);
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleLarge);
// progressBar.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar, params);
// getProjects();
fragment = new issuesFragment();
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText("ISSUES");
tabLayout.getTabAt(1).setText("PROJECT");
}
这是使用接口将项目列表发送到问题或项目片段的代码
private void lookForProjects() {
showProgressBar();
db = FirebaseFirestore.getInstance();
projects = new ArrayList<>();
CollectionReference projectReference = db.collection(getString(R.string.collection_project));
projectReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
// progressBar.setVisibility(View.INVISIBLE);
hideProgressBar();
if (task.isSuccessful()) {
for (QueryDocumentSnapshot snapshot : task.getResult()) {
Project project = snapshot.toObject(Project.class);
//projectNames[i] = project.getName();
projects.add(project);
// i++;
Log.d(TAG, "onComplete: " + project.getName());
}
// setUpAdapterForAutoCompleteTextView(projectNames);
} else {
//if (progressBar != null) {
// hideProgressBar();
//}
//Toast.makeText(IssueActivity.this, "Cannot Load Projects", Toast.LENGTH_LONG).show();
}
sendToFragments(projects);
}
});
}
private void sendToFragments(ArrayList<Project> projects) {
// issuesFragment issueFragment = new issuesFragment();
fragment.addProjects(projects);
// ProjectFragment projectFragment = new ProjectFragment();
// projectFragment.addProjects(projects);
}
但是问题片段
中存在的问题是这个fragment.addProjects(projects);
在连接时被称为
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
issuesInterface = (issuesInterface)getActivity();
// Log.d(TAG, "addProjects: to know if fragment is added + " + isAdded());\
getProjects();
}
catch (ClassCastException e){
e.printStackTrace();
}
}
getActivity()返回一个值,但是当我想用它来创建一个ArrayAdapter时,它是一个嵌套在getProject方法中的方法
ArrayAdapter<String> issueSpinnerAdapter = new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,stuffs);
issueSpinner.setAdapter(issueSpinnerAdapter);
它是null;并且应用程序崩溃时出现此错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:233)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:181)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:139)
at com.example.samuel.firestore.issuesFragment.initSpinner(issuesFragment.java:143)
at com.example.samuel.firestore.issuesFragment.addProjects(issuesFragment.java:128)
at com.example.samuel.firestore.Activities.IssueActivity.sendToFragments(IssueActivity.java:122)
at com.example.samuel.firestore.Activities.IssueActivity.access$000(IssueActivity.java:31)
at com.example.samuel.firestore.Activities.IssueActivity$1.onComplete(IssueActivity.java:112)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
但在一个场景中,没有从控制活动发送到片段的任何内容,我使用一个虚拟字符串数组来构建片段中的适配器,在同一方法中,我没有得到任何错误,适配器工作正好。 任何人都可以帮助解释为什么当我尝试从Controlling活动获取详细信息然后尝试构造一个结果得到的数组适配器时,getActivity返回null?
答案 0 :(得分:0)
我发现了问题,我调用方法sendToFragment的片段对象不是我传递给视图分页器适配器的片段对象...一旦更改了它...并使用了传递给适配器的相同对象在..上调用我的方法开始工作..
答案 1 :(得分:-1)
getActivity()不会返回非null对象。 在您的情况下,如果您想与Activity通信,则必须在onAttach()中执行此操作。见下文:
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
issuesInterface = (issuesInterface) context;
getProjects();
}
catch (ClassCastException e){
e.printStackTrace();
}
}
或者,如果您将此片段附加到另一个片段,您可以使用此:
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
Fragment parent = getParentFragment();
issuesInterface = (issuesInterface) parent;
getProjects();
}
catch (ClassCastException e){
e.printStackTrace();
}
}