带有自定义 ArrayAdapter 的 NullPointerException

NullPointerException with Custom ArrayAdapter

对不起,如果我听起来很愚蠢;我是 Android 的新手。我检查了其他有类似问题的 SO 帖子,但没有任何帮助。我正在制作自定义 ArrayAdapter,但是当适配器设置为列表时抛出 NullPointerException。这是带有列表的 XML 布局 (fragment_lines) 片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <ListView
        android:id="@+id/lines_list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

这是列表项布局 (lines_list_item):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<View
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_margin="16dp"
    android:id="@+id/line_icon"
    android:background="@drawable/red_circle"
    />

<TextView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:id="@+id/line_letter"
    android:text = "R"
    android:textColor="#FFFFFF"
    android:gravity="center"
    android:textSize="25sp"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="-56dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:id="@+id/line_name"
    android:text = "Red Line"
    android:textSize="16sp"
    android:textColor="#000000"
    android:gravity="center"
    android:layout_marginTop="13dp"
    android:layout_marginLeft="16dp"
    />
</LinearLayout>

这是自定义的 ArrayAdapter class(LineAdapter):

package com.example.android.testapp;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * Created by Shubhang on 2/16/2015.
 */
public class LineAdapter extends ArrayAdapter<String> {
    Activity context;
    int[] icons;
    String[] letters;
    String[]lines;
    String[] letterColors;

    public LineAdapter(Activity context, int[] icons, String[] letters, String[] lines, String[] letterColors) {
        super(context, R.layout.lines_list_item, letters);
        this.context = context;
        this.icons = icons;
        this.letters = letters;
        this.lines = lines;
        this.letterColors = letterColors;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.lines_list_item, null);
        }
        View icon = rowView.findViewById(R.id.line_icon);
        TextView letter = (TextView) rowView.findViewById(R.id.line_letter);
        TextView line = (TextView) rowView.findViewById(R.id.line_name);

        icon.setBackgroundResource(icons[position]);
        letter.setText(letters[position]);
        line.setText(lines[position]);
        letter.setTextColor(Color.parseColor(letterColors[position]));

        return rowView;
    }
}

最后,这是 MainActivity:

package com.example.android.testapp;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.example.android.testapp.data.SubwayContract;
import com.example.android.testapp.data.SubwayProvider;
import com.example.android.testapp.sync.CommuterSyncAdapter;
import com.google.transit.realtime.GtfsRealtime;

import java.io.IOException;
import java.net.URL;


public class MainActivity extends ActionBarActivity {
SubwayProvider provider;
Account mAccount;
private static final String LOG_TAG = CommuterSyncAdapter.class.getSimpleName();

int[] icons = {R.drawable.red_circle, R.drawable.blue_circle, R.drawable.brown_circle, R.drawable.green_circle,
        R.drawable.orange_circle, R.drawable.purple_circle, R.drawable.pink_circle, R.drawable.yellow_circle};

String[] letters = {"R", "B", "B", "G", "O", "P", "P", "Y"};

String[] lines = {"Red Line", "Blue Line", "Brown Line", "Green Line", "Orange Line", "Purple Line", "Pink Line", "Yellow Line"};

String[] letterColors = {"#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF", "#000000"};

ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new LinesFragment())
                .commit();
    }

    LineAdapter adapter = new LineAdapter(MainActivity.this, icons, letters, lines, letterColors);
    list = (ListView) findViewById(R.id.lines_list_view);
    list.setAdapter(adapter);
    ...
}
...
}

堆栈跟踪告诉我在 MainActivity 的第 51 行(这一行:list.setAdapter(adapter);)有一个 NullPointerException。我梳理了 LineAdapter,似乎找不到任何错误。有人可以帮忙吗?

提前致谢。

LineAdapter adapter = new LineAdapter(MainActivity.this, icons, letters, lines, letterColors);
list = (ListView) findViewById(R.id.lines_list_view);
list.setAdapter(adapter);

尝试将此代码移至片段的 onCreateView() 而不是此处,如果可行,请阅读 fragment lifecycle

我认为您混淆了 fragments 和布局。

您的 setContentView 正在加载此 XML 文件 activity_main

似乎 ListView lines_list_viewfragment_lines

所以,修正那部分,将 ListView 添加到你的 activity_main 或将 fragment_lines 设置为你的 ContentView 并再次检查。