java.lang.NullPointerException:storage == null

时间:2015-03-02 03:06:59

标签: java android

之前的错误是“你的内容必须有一个listview,其id属性为”Android.R.id.list“我做了一些谷歌搜索,然后结果是这个

Caused by: java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at edu.drexel.weatherundergroundapi.CustomListAdapter.<init>(CustomListAdapter.java:35)
at edu.drexel.weatherundergroundapi.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)



      

这些是CustomListActivity中的代码。

public CustomListAdapter(Activity context, String[] time, String[] condition, String[] temp, String[] humidity, String[] url)
{
    super(context, R.layout.listview, time);
    this.context = context;
    this.time = time;
    this.condition = condition;
    this.temp = temp;
    this.humidity = humidity;
    this.url = url;
}

这是mainActivity中的代码

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

    MyBgTask asyncTask = new MyBgTask();

    CustomListAdapter adapter = new CustomListAdapter(this, asyncTask.getTimeArray(), asyncTask.getConditionArray(),
                                asyncTask.getTempArray(), asyncTask.getHumidityArray(), asyncTask.getUrls());
    setListAdapter(adapter);

    asyncTask.execute();
}

这是activity_main.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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".main">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:clickable="false"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" >


</ListView>

这是listview.xml

<?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">

<ImageView
    android:id="@+id/icon"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:padding="5dp"
    android:contentDescription="nothing" />

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dateTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:padding="2dp"
        android:textColor="#33CC33"
        android:text="@string/dateTime" />
    <TextView
        android:id="@+id/condition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/condition"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:id="@+id/temp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/temp"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/humidity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/humidity"
        android:layout_marginLeft="10dp" />
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

我认为您希望从asyncTask获取适配器数据,但是,当您使用setListAdapter时,asyncTask尚未启动。所以它将为空。

在MyBgTask.onPostExecute()方法中执行setListAdapter会有所帮助;

答案 1 :(得分:0)

有几件事情需要考虑:

  1. 因为您使用setListAdapter(adapter)所以我认为您的活动已延长[ListActivity][1]。但是,如果您使用ListActivity,则不要需要使用方法setContentView(..)
  2. 您创建adapter的代码不完整或不正确。你在这里粘贴的只是constructor。通常,您需要扩展类似ArrayAdapter ...的内容,然后覆盖getView方法。 Here是详细说明。
  3. 有关列表教程的更多信息:这非常有用link