ListView适配器:无法启动活动组件信息{..}:java.lang.NullPointerException

ListView适配器:无法启动活动组件信息{..}:java.lang.NullPointerException,java,android,nullpointerexception,Java,Android,Nullpointerexception,我正在尝试开始一个新的活动,我得到了一个NullPointerException。 我不太确定从哪里得到NullPointerException。在调试期间,应用程序在创建适配器时崩溃,但我看不出在代码中获取NullPointerException的哪里出错 错误日志: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.app/com.app.APP.SpaActivity}: java.l

我正在尝试开始一个新的活动,我得到了一个NullPointerException。 我不太确定从哪里得到NullPointerException。在调试期间,应用程序在创建适配器时崩溃,但我看不出在代码中获取NullPointerException的哪里出错

错误日志:

   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.app/com.app.APP.SpaActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2446)
        at android.app.ActivityThread.access$600(ActivityThread.java:165)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5434)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.app.adapter.SpaListAdapter.<init>(SpaListAdapter.java:25)
        at com.app.APP.SpaActivity.onCreate(SpaActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:5122)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2446)
        at android.app.ActivityThread.access$600(ActivityThread.java:165)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5434)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
SpaActivity.java

public class SpaActivity extends Activity {

    private ProgressDialog pDialog;
    private List<Spa> SpaList = new ArrayList<Spa>();
    private ListView listView;
    private SpaListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spa);

        listView = (ListView) findViewById(R.id.list);
        adapter = new SpaListAdapter(this, SpaList);

        listView.setAdapter(adapter);
activity_spa.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@layout/list_row_selector" />

</RelativeLayout>
listview的适配器:

public class SpaListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Spa> SpaItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public SpaListAdapter(Activity activity, List<Spa> SpaItems) {
        this.activity = activity;
        this.SpaItems = SpaItems;
    }

    @Override
    public int getCount() {
        return SpaItems.size();
    }

    @Override
    public Object getItem(int location) {
        return SpaItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView duration = (TextView) convertView.findViewById(R.id.duration);
        TextView description = (TextView) convertView.findViewById(R.id.description);
        TextView price = (TextView) convertView.findViewById(R.id.price);

        // getting spa data for the row
        Spa s = SpaItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(s.getThumbnailUrl(), imageLoader);

        // title
        title.setText(s.getTitle());

        // duration
        duration.setText("Time: " + String.valueOf(s.getDuration()));

        // description
        description.setText(String.valueOf(s.getDescription()));

        // price
        price.setText("Price: " + String.valueOf(s.getPrice()));

        return convertView;
    }

}
最后是适配器的list_row.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"
    android:background="@layout/list_row_selector"
    android:padding="8dp" >

    <!-- Thumbnail Image -->
    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/thumbnail"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="8dp" />

    <!-- Spa Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/title"
        android:textStyle="bold" />

    <!-- Duration -->
    <TextView
        android:id="@+id/duration"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/rating" />

    <!-- Description -->
    <TextView
        android:id="@+id/description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/duration"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="@color/genre"
        android:textSize="@dimen/genre" />

    <!-- Price -->
    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:textColor="@color/year"
        android:textSize="@dimen/year" />

</RelativeLayout>

错误在spalistadatper类的第25行。 您的方法getItemId需要修改

 @Override
    public long getItemId(int position) {
        return SpaItems.indexOf(getItem(position));
    }

什么是SpaListAdapter.java:25???这是一行:ImageLoader ImageLoader=AppController.getInstance.getImageLoader;SpaActivity上的43个选项是:adapter=new spalistadapter此,SpaList;进行了此更改,但在相同的行中仍然出现相同的错误。您的spalist中是否有元素?我看不到您正在执行任何SpaItems。添加spalist将发送到构造函数中的适配器。私有列表spalist=new ArrayList;->此时,列表中没有任何元素,然后执行adapter=new spalistadapter此,SpaList;SpaList在我看来是空的。