gpt4 book ai didi

java - 子 fragment 的 getView 返回 null

转载 作者:行者123 更新时间:2023-11-30 09:15:07 26 4
gpt4 key购买 nike

我正在创建并替换另一个 fragment 内的 fragment onViewCreated

public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
//add searchFragment to this fragment
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
if(searchFragment==null)
searchFragment=new SearchFragment();
fragmentTransaction.replace(R.id.updateFragmentContainer, searchFragment).commit();

if(searchFragment.getView()==null){
Log.d("getView","of search view is null");//the control always reaches here
}
listView=(ListView)getActivity().findViewById(R.id.listView1);
if(listView!=null)
listView.setOnItemClickListener(onItemClickListener);//the control never reaches here
else
searchFragment.setUpdateItemClickListener(onItemClickListener);//this is a function in the saerchFragment class; there too the listview obtained by findViewbyId is null
}

现在 searchFragment 中有一个 listView,我想在父 fragment 中设置其 onItemClickListener。我已经尝试了很多方法,如上所示,但它们都不起作用,因为 findViewById 返回的 listView 始终为 null。

此外,当我删除所有这些由 listView 引起的空指针异常时,searchFragment 功能也能正常工作。我还尝试覆盖 searchFragment 的 getView 并返回在 onCreateView 期间被破坏的 View 的引用,但它也返回 null。

编辑SearchFragment 类

import android.content.ContentProviderClient;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class SearchFragment extends Fragment {
int rollNo;
Button searchButton=null;
EditText rollNoEditText=null;
ListView listView=null;
public SearchFragment() {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_search, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
rollNoEditText=(EditText)getView().findViewById(R.id.rollNoEditText1);
searchButton=(Button)getView().findViewById(R.id.searchbutton1);
if(searchButton!=null)
searchButton.setOnClickListener(onClickListener);
else
Log.d("Search Button", "null");
listView=(ListView)getView().findViewById(R.id.listView1);
}
View.OnClickListener onClickListener=new View.OnClickListener() {

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rollNoEditText.getText().length()>0){
try{
rollNo=Integer.parseInt(rollNoEditText.getText().toString());
if(rollNo>0){
listView.setAdapter(
new SimpleCursorAdapter(getActivity(), // Context.
R.layout.item_parent_student,
getStudentCurosr(),
new String[] { "name","rollno","class","marks" },
// Parallel array of which template objects to bind to those
// columns.
new int[] {R.id.nameTextView2,R.id.rollNoTextView2,R.id.classTextView2,R.id.marksTextView2 }
));
}
//displayStudentInfo(rollNo);
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(), "Please type a valid integer value", Toast.LENGTH_SHORT).show();
}
}

}
};

private Cursor getStudentCurosr() {
// Run query
Uri uri = Uri.parse("content://com.example.students.provider.studentcontentprovider/student/"+rollNo);
Log.d("RollNO passed",""+rollNo);
ContentProviderClient contentProviderClient = getActivity().getContentResolver().acquireContentProviderClient(uri );
Cursor cur = null;
try {
cur = contentProviderClient.query(uri,null,null, null, null);
return cur;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cur;
}


}

最佳答案

getView of Child Fragment returns null

fragment 交易是异步的,因此您的新 SearchFragment您将在该替换事务中使用的 View 尚不可用(它将在 onViewCreated() 方法完成后发生)。您可以将监听器实例传递给新创建的 SearchFragment实例并将其存储在私有(private)字段中(在该 setter 中,您还可以检查并查看 ListView 此时是否可用,如果可用,则还设置传入的监听器)。然后在 SearchFragment 之一中的生命周期方法(如 onViewCreated() )检查 OnItemClickListener字段,如果它是 null然后将其设置为 ListView小部件。

但是,有些东西告诉我你正在以错误的方式接近它,你应该让 SearchFragmentListView 上设置自己的监听器并通过 getParentFragment() 将任何事件传递给父 fragment 方法。

关于java - 子 fragment 的 getView 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20072946/

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