使用自定义ArrayAdapter的NullPointerException

时间:2015-02-17 04:42:38

标签: java android nullpointerexception android-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类(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行有一个NullPointerException(这一行:list.setAdapter(adapter);)。我已经梳理了LineAdapter并且似乎找不到任何错误。有人可以帮忙吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

似乎ListView lines_list_view位于fragment_lines

因此,请修复该部分,将ListView添加到activity_main或将fragment_lines设置为ContentView并再次检查。