自定义 ArrayAdapter 上未捕获的处理程序 NullPointerException
这是我设置自定义 ArrayAdapter:
lv.setAdapter(new MatchArrayAdapter(this, android.R.layout.simple_list_item_1, g.dm.getMatchesOnTeam(teamId), getWindowManager().getDefaultDisplay().getWidth(),getWindowManager().getDefaultDisplay().getHeight()));
这是我的 MatchArrayAdapter:
public class MatchArrayAdapter extends ArrayAdapter<Match> {
private Match[] matches;
private int height;
private int width;
private Context context;
public MatchArrayAdapter(Context context, int textViewResourceId,
Match[] objects, int width, int height) {
super(context, textViewResourceId, objects);
this.matches = objects;
this.width = width;
this.height = height;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.matchlistview, null);
}
//Get all the views
TextView opponent = (TextView) convertView.findViewById(R.id.MatchOpponent);
TextView month = (TextView) convertView.findViewById(R.id.MatchMonth);
TextView day = (TextView) convertView.findViewById(R.id.MatchDay);
ImageView matchMoreInformation = (ImageView) convertView.findViewById(R.id.MatchMoreInformation);
opponent.setText("vs. "+matches[position].getOpponentTeam().getTeamName());
LayoutParams lpo = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpo.setMargins((int)(width*0.15), (int)(height*0.04), 0, 0);
opponent.setLayoutParams(lpo);
month.setText(String.valueOf(matches[position].getDate().get(Calendar.MONTH)));
LayoutParams lpm = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpm.setMargins((int)(width*0.02), 0, 0, 0);
month.setLayoutParams(lpm);
day.setText(String.valueOf(matches[position].getDate().get(Calendar.DAY_OF_MONTH)));
LayoutParams lpd = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpd.setMargins((int)(width*0.07), (int)(height*0.05), 0, 0);
day.setLayoutParams(lpd);
//"Next" arrow, arrow for more information of the match
matchMoreInformation.setImageResource(android.R.drawable.ic_media_play);
LayoutParams lpmmi = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpmmi.setMargins((int)(width*0.81), (int)(height*0.044), 0, 0);
matchMoreInformation.setLayoutParams(lpmmi);
return convertView;
}
}
我总是收到以下错误:
07-26 13:47:24.034: ERROR/AndroidRuntime(625): Uncaught handler: thread main exiting due to uncaught exception
07-26 13:47:24.044: ERROR/AndroidRuntime(625): java.lang.NullPointerException
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at data.MatchArrayAdapter.getView(MatchArrayAdapter.java:41)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.makeAndAddView(ListView.java:1668)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.fillDown(ListView.java:637)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.fillFromTop(ListView.java:694)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.layoutChildren(ListView.java:1521)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.os.Looper.loop(Looper.java:123)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at java.lang.reflect.Method.invoke(Method.java:521)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at dalvik.system.NativeStart.main(Native Method)
我一直在尝试我找到的所有方法。但没有什么可以解决它。
编辑: 第 41 行的代码是:
opponent.setText("vs. "+matches[position].getOpponentTeam().getTeamName());
我已在该行中设置了断点,并且 matches[position].getOpponentTeam().getTeamName()
不为 null。
第二次编辑: 这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/MatchOpponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/MatchMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/MatchDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<ImageView
android:id="@+id/MatchMoreInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
</ImageView>
</RelativeLayout>
This is where I set my custom ArrayAdapter:
lv.setAdapter(new MatchArrayAdapter(this, android.R.layout.simple_list_item_1, g.dm.getMatchesOnTeam(teamId), getWindowManager().getDefaultDisplay().getWidth(),getWindowManager().getDefaultDisplay().getHeight()));
This is my MatchArrayAdapter:
public class MatchArrayAdapter extends ArrayAdapter<Match> {
private Match[] matches;
private int height;
private int width;
private Context context;
public MatchArrayAdapter(Context context, int textViewResourceId,
Match[] objects, int width, int height) {
super(context, textViewResourceId, objects);
this.matches = objects;
this.width = width;
this.height = height;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.matchlistview, null);
}
//Get all the views
TextView opponent = (TextView) convertView.findViewById(R.id.MatchOpponent);
TextView month = (TextView) convertView.findViewById(R.id.MatchMonth);
TextView day = (TextView) convertView.findViewById(R.id.MatchDay);
ImageView matchMoreInformation = (ImageView) convertView.findViewById(R.id.MatchMoreInformation);
opponent.setText("vs. "+matches[position].getOpponentTeam().getTeamName());
LayoutParams lpo = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpo.setMargins((int)(width*0.15), (int)(height*0.04), 0, 0);
opponent.setLayoutParams(lpo);
month.setText(String.valueOf(matches[position].getDate().get(Calendar.MONTH)));
LayoutParams lpm = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpm.setMargins((int)(width*0.02), 0, 0, 0);
month.setLayoutParams(lpm);
day.setText(String.valueOf(matches[position].getDate().get(Calendar.DAY_OF_MONTH)));
LayoutParams lpd = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpd.setMargins((int)(width*0.07), (int)(height*0.05), 0, 0);
day.setLayoutParams(lpd);
//"Next" arrow, arrow for more information of the match
matchMoreInformation.setImageResource(android.R.drawable.ic_media_play);
LayoutParams lpmmi = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpmmi.setMargins((int)(width*0.81), (int)(height*0.044), 0, 0);
matchMoreInformation.setLayoutParams(lpmmi);
return convertView;
}
}
And I'm always getting the following errors:
07-26 13:47:24.034: ERROR/AndroidRuntime(625): Uncaught handler: thread main exiting due to uncaught exception
07-26 13:47:24.044: ERROR/AndroidRuntime(625): java.lang.NullPointerException
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at data.MatchArrayAdapter.getView(MatchArrayAdapter.java:41)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.makeAndAddView(ListView.java:1668)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.fillDown(ListView.java:637)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.fillFromTop(ListView.java:694)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.layoutChildren(ListView.java:1521)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.os.Looper.loop(Looper.java:123)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at java.lang.reflect.Method.invoke(Method.java:521)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at dalvik.system.NativeStart.main(Native Method)
I've been trying all I've found. But nothing fix it out.
Edit:
The code of line 41 is:
opponent.setText("vs. "+matches[position].getOpponentTeam().getTeamName());
I've set a breakpoint in that line and, matches[position].getOpponentTeam().getTeamName()
is not null.
2nd edit:
This is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/MatchOpponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/MatchMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/MatchDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<ImageView
android:id="@+id/MatchMoreInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
</ImageView>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定“MatchOpponent”控件是在 matchlistview 布局中定义的,并且未在任何其他布局中定义吗?也许这个控制没有膨胀成功。
Are you sure that 'MatchOpponent' control was defined in matchlistview layout and was not defined in any other layout? Maybe this control was not inflated successful.
那里只有一个错误(其余的是堆栈跟踪),它位于
getView
方法内源代码的第 41 行(我认为您发布的代码不包括 import 语句等,所以我无法确切知道是哪条线)。您正在尝试使用空对象。
编辑:
如果
matches[position].getOpponentTeam().getTeamName()
不是null
那么您确定opponent
不为 null 吗?你确定R.id.MatchOpponent
是一个TextView
There is only one error there(the rest is stacktrace)and it is on line 41 of your source inside
getView
method(I think the code you posted does not include import statements etc. so I cannot know exactly which line it is).You are trying to use a null object.
EDIT:
If
matches[position].getOpponentTeam().getTeamName()
is notnull
then are you sure thatopponent
is not null? And are you sureR.id.MatchOpponent
is aTextView