初始化 LayoutInflater 期间上下文出现 NullPointerException
NullPointerException on Context during Initializing LayoutInflater
我正在通过 API 获取数据并尝试将它们放在 ListView 中(我先将它们包装在一个包中),我正在使用 BaseAdapter。问题是 Context/LayoutInflater 我在一个片段中传递给适配器 returns 一个 NullPointer Exception
//AsyncTask中的onPostExecute
@Override
protected void onPostExecute(Bundle bundle) {
if(bundle != null){
ResultFragment resultFragment = new ResultFragment();
resultFragment.asyncExecuted(bundle);
}
}
//Fragment里面的方法
public void asyncExecuted(Bundle bundle) {
listResult.setAdapter(new ResultAdapter(bundle,getActivity()));
}
//Adapter Class, Adapter class 错误,特别是初始化 inflater.
public class ResultAdapter extends BaseAdapter {
String title, isbn, author;
String[] s_title, s_isbn, s_rating, s_image, s_author;
LayoutInflater inflater;
public ResultAdapter(Bundle bundle, Context context){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
title = bundle.getString("M_TITLE");
isbn = bundle.getString("M_ISBN_13");
author = bundle.getString("M_AUTHOR_NAME");
int size = bundle.getInt("M_SIMILAR_SIZE");
s_title = new String[size];
s_isbn = new String[size];
s_rating = new String[size];
s_image = new String[size];
s_author = new String[size];
s_title = bundle.getStringArray("M_SIMILAR_TITLE");
s_isbn = bundle.getStringArray("M_SIMILAR_ISBN_13");
s_rating = bundle.getStringArray("M_SIMILAR_RATING");
s_image = bundle.getStringArray("M_SIMILAR_IMAGE_URL");
s_author = bundle.getStringArray("M_SIMILAR_AUTHOR_NAME");
}catch (NullPointerException e){
Log.e(getClass().getSimpleName(),"Nothing is Inside!");
}
}
@Override
public int getCount() {
return s_title.length;
}
@Override
public Object getItem(int position) {
return s_isbn[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.list_item_result, parent, false);
TextView txtTitle = (TextView)row.findViewById(R.id.txtTitle);
TextView txtAuthor = (TextView)row.findViewById(R.id.txtAuthor);
TextView txtRating = (TextView)row.findViewById(R.id.txtRating);
txtTitle.setText(s_title[position]);
txtAuthor.setText(s_author[position]);
txtRating.setText(s_rating[position]);
return row;
}
}
LOGCAT 错误条目
检查标题和其他变量是否存在于Api
使用 try 和 catch 方法添加代码。
尝试{<br>
标题 = bundle.getString("M_TITLE");
isbn = bundle.getString("M_ISBN_13");
作者 = bundle.getString("M_AUTHOR_NAME");
}赶上(...){}
当您创建 Fragment
时,它尚未附加到 Activity
直到 onAttach(Activity) 被调用 - 直到那时,getActivity()
将 return null
.
您应该通过 setArguments() and then retrieve it via getArguments(), and execute your asyncExecuted
method in onAttach()
or later in the fragment lifecycle 将您的 Bundle
传递到您的 Fragment
。
1) 您应该通过检查 getActivity() 是否为 null 来验证在调用 onPostExecute 时您的片段是否仍附加到 activity。如果它为 null,则该片段已分离。在这种情况下你通常想退出。
2) 如果它是 getActivity 的有效实例,试试这个:
View row = LayoutInflater.from(getContext()).inflate(R.layout.list_item_result, parent, false);
我正在通过 API 获取数据并尝试将它们放在 ListView 中(我先将它们包装在一个包中),我正在使用 BaseAdapter。问题是 Context/LayoutInflater 我在一个片段中传递给适配器 returns 一个 NullPointer Exception
//AsyncTask中的onPostExecute
@Override
protected void onPostExecute(Bundle bundle) {
if(bundle != null){
ResultFragment resultFragment = new ResultFragment();
resultFragment.asyncExecuted(bundle);
}
}
//Fragment里面的方法
public void asyncExecuted(Bundle bundle) {
listResult.setAdapter(new ResultAdapter(bundle,getActivity()));
}
//Adapter Class, Adapter class 错误,特别是初始化 inflater.
public class ResultAdapter extends BaseAdapter {
String title, isbn, author;
String[] s_title, s_isbn, s_rating, s_image, s_author;
LayoutInflater inflater;
public ResultAdapter(Bundle bundle, Context context){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
title = bundle.getString("M_TITLE");
isbn = bundle.getString("M_ISBN_13");
author = bundle.getString("M_AUTHOR_NAME");
int size = bundle.getInt("M_SIMILAR_SIZE");
s_title = new String[size];
s_isbn = new String[size];
s_rating = new String[size];
s_image = new String[size];
s_author = new String[size];
s_title = bundle.getStringArray("M_SIMILAR_TITLE");
s_isbn = bundle.getStringArray("M_SIMILAR_ISBN_13");
s_rating = bundle.getStringArray("M_SIMILAR_RATING");
s_image = bundle.getStringArray("M_SIMILAR_IMAGE_URL");
s_author = bundle.getStringArray("M_SIMILAR_AUTHOR_NAME");
}catch (NullPointerException e){
Log.e(getClass().getSimpleName(),"Nothing is Inside!");
}
}
@Override
public int getCount() {
return s_title.length;
}
@Override
public Object getItem(int position) {
return s_isbn[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.list_item_result, parent, false);
TextView txtTitle = (TextView)row.findViewById(R.id.txtTitle);
TextView txtAuthor = (TextView)row.findViewById(R.id.txtAuthor);
TextView txtRating = (TextView)row.findViewById(R.id.txtRating);
txtTitle.setText(s_title[position]);
txtAuthor.setText(s_author[position]);
txtRating.setText(s_rating[position]);
return row;
}
}
LOGCAT 错误条目
检查标题和其他变量是否存在于Api
使用 try 和 catch 方法添加代码。
尝试{<br>
标题 = bundle.getString("M_TITLE");
isbn = bundle.getString("M_ISBN_13");
作者 = bundle.getString("M_AUTHOR_NAME");
}赶上(...){}
当您创建 Fragment
时,它尚未附加到 Activity
直到 onAttach(Activity) 被调用 - 直到那时,getActivity()
将 return null
.
您应该通过 setArguments() and then retrieve it via getArguments(), and execute your asyncExecuted
method in onAttach()
or later in the fragment lifecycle 将您的 Bundle
传递到您的 Fragment
。
1) 您应该通过检查 getActivity() 是否为 null 来验证在调用 onPostExecute 时您的片段是否仍附加到 activity。如果它为 null,则该片段已分离。在这种情况下你通常想退出。
2) 如果它是 getActivity 的有效实例,试试这个:
View row = LayoutInflater.from(getContext()).inflate(R.layout.list_item_result, parent, false);