Android 自定义adatper getView返回空指针
在我的应用程序中,我有一个带有自定义适配器的listview,该适配器在发送消息后重新加载。该应用程序在某些安装中运行。我在应用程序控制台中有堆栈跟踪。代码如下:Android 自定义adatper getView返回空指针,android,custom-adapter,getview,Android,Custom Adapter,Getview,在我的应用程序中,我有一个带有自定义适配器的listview,该适配器在发送消息后重新加载。该应用程序在某些安装中运行。我在应用程序控制台中有堆栈跟踪。代码如下: class IconicAdapter extends ArrayAdapter<Integer> { Activity context; IconicAdapter(Activity context) { super(context, R.layout.messa
class IconicAdapter extends ArrayAdapter<Integer> {
Activity context;
IconicAdapter(Activity context) {
super(context, R.layout.message_layout, listItems);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
//you can access layout inflater by accessing hosting activity
convertView = getActivity().getLayoutInflater().inflate(R.layout.message_layout, parent, false);
JSONObject json_data = null;
try {
json_data = jArray.getJSONObject(position);
} catch (JSONException e) {
}
try {
tv = (TextView) convertView.findViewById(R.id.username);
tv.setText(json_data.getString("username"));
第945行:tv.setText(json_data.getString(“用户名”) 类IconicAdapter扩展了ArrayAdapter{
活动语境;
图标适配器(活动上下文){
super(context,R.layout.activity_main,新整数[]{1,2});
this.context=上下文;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
convertView=LayoutInflater.from(parent.getContext()).flate(R.layout.activity\u main,null);
JSONObject json_data=null;
试一试{
json_data=jArray.getJSONObject(位置);
tv=(TextView)convertView.findViewById(R.id.username);
tv.setText(json_data.getString(“用户名”));
}捕获(JSONException e){
e、 printStackTrace();
}
返回视图;
}
}类图标适配器扩展了ArrayAdapter{
活动语境;
图标适配器(活动上下文){
super(context,R.layout.activity_main,新整数[]{1,2});
this.context=上下文;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
convertView=LayoutInflater.from(parent.getContext()).flate(R.layout.activity\u main,null);
JSONObject json_data=null;
试一试{
json_data=jArray.getJSONObject(位置);
tv=(TextView)convertView.findViewById(R.id.username);
tv.setText(json_data.getString(“用户名”));
}捕获(JSONException e){
e、 printStackTrace();
}
返回视图;
}
}使用以下代码:
类IconicAdapter扩展了ArrayAdapter{
活动语境
IconicAdapter(Activity context) {
super(context, R.layout.message_layout, listItems);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView=LayoutInflater.inflate(R.layout.message_layout, null);
JSONObject json_data = null;
try {
json_data = jArray.getJSONObject(position);
} catch (JSONException e) {
e.printStackTrace();
}
tv = (TextView) convertView.findViewById(R.id.username);
if(json_data!=null)
tv.setText(json_data.getString("username"));
else
tv.setText("")
使用以下代码:
类IconicAdapter扩展了ArrayAdapter{
活动语境
IconicAdapter(Activity context) {
super(context, R.layout.message_layout, listItems);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView=LayoutInflater.inflate(R.layout.message_layout, null);
JSONObject json_data = null;
try {
json_data = jArray.getJSONObject(position);
} catch (JSONException e) {
e.printStackTrace();
}
tv = (TextView) convertView.findViewById(R.id.username);
if(json_data!=null)
tv.setText(json_data.getString("username"));
else
tv.setText("")
可能您的json_数据为空,请打印stacktrace:catch(JSONException e){}不,stacktrace中没有关于json的内容打印json_数据,也在catch中打印e.printstacktrace。并放置日志问题是我无法重现我的问题。我只在应用程序控制台中看到它,在那里看到跟踪可能您的json_数据为空,打印stacktrace:catch(JSONException e){}不,StackTrace中没有关于json的任何内容打印json_数据,也在catch中打印e.printstacktrace。并放置日志。问题是我无法重现我的问题。我只在应用程序控制台中看到它,在那里看到traceIs它在片段中运行很重要吗?如果您使用适配器处理片段,则不重要不是。非静态方法充气不能从静态上下文引用我已经修改了代码,知道它会工作。也别忘了投票给我。它在片段中运行很重要吗?如果您使用的是片段适配器,则不是。非静态方法充气不能从静态上下文引用我已经修改了代码,知道它w我会的。也别忘了给我投票你是对的json_数据是空的。但我不明白为什么。它有时有效,有时无效。嗯。让我们继续给我发代码吧ankitpatidar030@gmail.com我会帮你检查你是对的json_数据是空的。但我不明白为什么。它有时有效,有时无效。嗯。让我们给我发送关于它的代码patidar030@gmail.com我帮你查一下
IconicAdapter(Activity context) {
super(context, R.layout.message_layout, listItems);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView=LayoutInflater.inflate(R.layout.message_layout, null);
JSONObject json_data = null;
try {
json_data = jArray.getJSONObject(position);
} catch (JSONException e) {
e.printStackTrace();
}
tv = (TextView) convertView.findViewById(R.id.username);
if(json_data!=null)
tv.setText(json_data.getString("username"));
else
tv.setText("")