自定义ArrayAdapter上未捕获的处理程序NullPointerException

时间:2011-07-26 12:08:04

标签: android nullpointerexception android-arrayadapter

这是我设置自定义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()不为空。

第二次编辑: 这是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>

2 个答案:

答案 0 :(得分:1)

那里只有一个错误(其余的是堆栈跟踪),它位于getView方法内的源代码的第41行(我认为您发布的代码不包含import语句等等所以我无法确切地知道它是哪条线)。

您正在尝试使用null对象。

修改 如果matches[position].getOpponentTeam().getTeamName()不是null,那么您确定opponent不为空吗?您确定R.id.MatchOpponentTextView

吗?

答案 1 :(得分:1)

您确定'MatchOpponent'控件是在matchlistview布局中定义的,并且未在任何其他布局中定义?也许这种控制并没有成功。